XHR is a wrapper around Titanium's HTTPClient. It works perfectly with REST API endpoints and has a built in cache system that you can use for your requests. But it also can be use for any HTTP requests, you can even cache remote images.
In your app.js (or elsewhere), call:
//init xhr.js
var XHR = require("xhr");
var xhr = new XHR();
GET and DELETE calls share the same structure
xhr.GET("http://freegeoip.net/json/", onSuccessCallback, onErrorCallback, options);
xhr.DELETE("http://freegeoip.net/json/", onSuccessCallback, onErrorCallback, options);
POST and PUT also share structure, only 1 added field as opposed to GET/DELETE
xhr.POST("http://freegeoip.net/json/", data, onSuccessCallback, onErrorCallback, options);
xhr.PUT("http://freegeoip.net/json/", data, onSuccessCallback, onErrorCallback, options);
In the 4 API call methods you can set options, but doing this every time might be a bit frustrating. Especially if you want authentication for every API call. (or other options). You can set it globally like this
xhr.setStaticOptions(options);
If you do specify options in an API call, it will not ignore global options. This might be useful if all but 1 API call should be authenticated for example.
async
(defaulttrue
) - If an API call should be async or notttl
(defaultfalse
) - Seconds the API response should be cached (only works withGET()
shouldAuthenticate
(defaultfalse
) - Should the call be made with authentication? BASIC Auth & oAuth supportedoAuthToken
- oAuth token. Only works ifshouldAuthenticate
istrue
username
- Username for BASIC authentication. Only works ifshouldAuthenticate
istrue
andoAuthToken
is not setpassword
- Password for BASIC authentication. Seeusername
contentType
(defaultapplication/json
)- contentType for API call.parseJSON
(defaultfalse
) - Should provided data forPOST()
andPUT()
be stringified and response (for all methods) be parsed.returnXML
(defaultfalse
) - Do you expect XML returned, put this totrue
debug
(defaultfalse
) - Do you wantTi.API.info
to show API calls made
For some examples please check out the examples.js file. Or browse around the xhr.js file. You can find in there support for GET, POST, PUT and DELETE
Apart from the RESTful way of interacting with your API endpoints, this module also includes the following helper methods:
- url: (required) The URL you want removed from the cache manager
Finds the cached document of the given url (if any) and removes it from the cache manager. This method is useful if you are not satisfied with the results you got at the time.
Goes through all the cached documents and delete everything that has been expired (if their TTL timestamp is less than the current time)
This method returns the count of deleted documents
Goes through all the documents and deletes everything
This method returns the count of deleted documents
Previously get
, post
, put
and destroy
methods were used. They still work but are deprecated.
Created by Raul Riera, @raulriera
Contributions by:
- Daniel Tamas, @dan_tamas
- Bob Sims, @2wheelsburning
- Mark Ross @rossman66
- Rene Pot, @Wraldpyk