Last active
August 29, 2015 14:18
-
-
Save rborn/f40cf26f5d30f9e93ebb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*jslint vars: true, sloppy: true, nomen: true, maxerr: 1000 */ | |
| // This is a parse rest client for Titanium. | |
| // | |
| var config = { | |
| parse: { | |
| appID: 'foo', | |
| clientKey: 'bar', | |
| apiKey: 'baz', | |
| masterKey: 'qux' | |
| } | |
| }; | |
| var baseURL = 'https://api.parse.com/1/'; | |
| function ParseClient() { | |
| } | |
| ParseClient.prototype.createObject = function(args) { | |
| var url = baseURL + 'classes/' + args._class; | |
| var params = { | |
| method: 'POST', | |
| body: args.data | |
| }; | |
| this._request(url, params, args.callback); | |
| }; | |
| ParseClient.prototype.updateObject = function(args) { | |
| // _class, _objectID, data, callback | |
| var url = baseURL + 'classes/' + args._class + '/' + args._objectID; | |
| var params = { | |
| method: 'PUT', | |
| body: args.data | |
| }; | |
| this._request(url, params, args.callback); | |
| }; | |
| ParseClient.prototype.getObjects = function(args) { | |
| var url = baseURL + 'classes/' + args._class; | |
| if (args.parameters) { | |
| url = url + '?' + args.parameters; //Ti.Network.encodeURIComponent(args.parameters); | |
| } | |
| var params = { | |
| method: 'GET' | |
| }; | |
| // Ti.API.error(url); | |
| this._request(url, params, args.callback); | |
| }; | |
| ParseClient.prototype.getObject = function(args) { | |
| var url = baseURL + 'classes/' + args._class + '/' + args._objectID; | |
| this._request(url, args.callback); | |
| }; | |
| ParseClient.prototype.getConfig = function(args) { | |
| var url = baseURL + 'config'; | |
| this._request(url, args.callback); | |
| }; | |
| ParseClient.prototype.deleteObject = function(args) { | |
| var url = baseURL + 'classes/' + args._class + '/' + args._objectID; | |
| var params = { | |
| method: 'DELETE' | |
| }; | |
| this._request(url, params, args.callback); | |
| }; | |
| ParseClient.prototype.createUser = function(args) { | |
| var url = baseURL + 'users'; | |
| var params = { | |
| method: 'POST', | |
| body: args.data | |
| }; | |
| function cb(success, response, code) { | |
| if (success === 1) { | |
| response = JSON.parse(response); | |
| parse.setSessionToken(response.sessionToken); | |
| args.callback(success, response, code); | |
| } else { | |
| args.callback(success, response, code); | |
| } | |
| } | |
| this._request(url, params, cb); | |
| }; | |
| ParseClient.prototype.getUsers = function(args) { | |
| var url = baseURL + 'users'; | |
| this._request(url, args.callback); | |
| }; | |
| ParseClient.prototype.getUser = function(args) { | |
| var url = baseURL + 'users/' + args._userObject; | |
| this._request(url, args.callback); | |
| }; | |
| ParseClient.prototype.loginUser = function(args) { | |
| var url = baseURL + 'login?username=' + args._username + '&password=' + args._password; | |
| function cb(success, response, code) { | |
| if (success === 1) { | |
| response = JSON.parse(response); | |
| parse.setSessionToken(response.sessionToken); | |
| parse.saveUserRecord(response); | |
| args.callback(success, response, code); | |
| } else { | |
| args.callback(success, response, code); | |
| } | |
| } | |
| this._request(url, cb); | |
| }; | |
| ParseClient.prototype.updateUser = function(args) { | |
| var url = baseURL + 'users/' + args._userObject; | |
| var params = { | |
| method: 'PUT', | |
| body: args.data | |
| }; | |
| this._request(url, params, args.callback); | |
| }; | |
| ParseClient.prototype.deleteUser = function(args) { | |
| var url = baseURL + 'users/' + args._userObject; | |
| var params = { | |
| method: 'DELETE' | |
| }; | |
| this._request(url, params, args.callback); | |
| }; | |
| ParseClient.prototype.passwordReset = function(args) { | |
| var url = baseURL + 'requestPasswordReset'; | |
| var email = { | |
| email: args._email | |
| }; | |
| var params = { | |
| method: 'POST', | |
| body: email | |
| }; | |
| this._request(url, params, args.callback); | |
| }; | |
| // -- functions below here not fully functional/tested -- | |
| ParseClient.prototype.uploadImage = function(_contentType, _filename, _blob, callback) { | |
| var url = baseURL + 'files/' + _filename; | |
| var params = { | |
| method: 'POST', | |
| body: _blob, | |
| headers: {} | |
| }; | |
| params.headers['Content-Type'] = _contentType | |
| this._request(url, params, callback); | |
| }; | |
| ParseClient.prototype.saveUserRecord = function(user) { | |
| Ti.App.Properties.setObject('parseUser', user); | |
| }; | |
| ParseClient.prototype.setSessionToken = function(token) { | |
| Ti.App.Properties.setString('parseSessionToken', token); | |
| }; | |
| ParseClient.prototype.getSessionToken = function() { | |
| return Ti.App.Properties.getString('parseSessionToken'); | |
| }; | |
| ParseClient.prototype.registerPush = function(args) { | |
| var url = baseURL + 'installations'; | |
| var params = { | |
| method: 'POST', | |
| body: args.data | |
| }; | |
| this._request(url, params, args.callback); | |
| }; | |
| ParseClient.prototype.updateInstallation = function(args) { | |
| var url = baseURL + 'installations'; | |
| var params = { | |
| method: 'PUT', | |
| body: args.data | |
| }; | |
| this._request(url, params, args.callback); | |
| }; | |
| ParseClient.prototype.sendPush = function(args) { | |
| var url = baseURL + '/push'; | |
| var payload = { | |
| method: 'POST', | |
| body: args.data | |
| }; | |
| this._request(url, payload, args.callback); | |
| }; | |
| ParseClient.prototype._request = function(url, params, callback) { | |
| if (typeof params === 'function') { | |
| callback = params; | |
| params = {}; | |
| } | |
| params = params || {}; | |
| // Clean up the call type, defaulting to GET if no method set | |
| params.method = params.method || 'GET'; | |
| //console.log('Url: ', url); | |
| //console.log('Params: ', params); | |
| params.method = params.method.toUpperCase(); | |
| // If not specified, use a 20 second timeout | |
| params.timeout = ('timeout' in params) ? params.timeout : 15000; | |
| params.body = params.body || {}; | |
| params.query = params.query || {}; | |
| params.url = url || baseURL; | |
| //params.url += url; | |
| params.headers = params.headers || {}; | |
| params.headers['X-Parse-Application-Id'] = config.parse.appID; | |
| params.headers['X-Parse-REST-API-Key'] = config.parse.apiKey; | |
| if (!params.headers['Content-Type']) { | |
| params.headers['Content-Type'] = 'application/json'; | |
| } | |
| params.headers['Accept'] = params.headers['Accept'] || 'application/json'; | |
| if (!('login' in params) || !params.login) { | |
| params.headers['X-Parse-Session-Token'] = this.getSessionToken(); | |
| } | |
| // Need to clear some properties depending on method | |
| if ((params.method === 'GET') || (params.method === 'DELETE')) { | |
| params.body = null; | |
| } else { | |
| params.body = JSON.stringify(params.body); | |
| params.query = null; | |
| } | |
| var xhr = Ti.Network.createHTTPClient(); | |
| xhr.setTimeout(params.timeout); | |
| xhr.onerror = function(e) { | |
| console.log('HXR Error'); | |
| console.log(xhr.status); | |
| console.log(xhr.responseText); | |
| callback(0, xhr.responseText, xhr.status, xhr); | |
| }; | |
| xhr.onload = function() { | |
| //console.log('HXR Success'); | |
| //console.log(this.responseText); | |
| callback(1, this.responseText, this.status); | |
| }; | |
| //params = params.replace(/\./g, '_'); | |
| //console.log(params); | |
| xhr.open(params.method, params.url); | |
| //console.log(params.url); | |
| for (var key in params.headers) { | |
| xhr.setRequestHeader(key, params.headers[key]); | |
| } | |
| if (params.method == 'GET') { | |
| xhr.send(); | |
| } else { | |
| xhr.send(params.body); | |
| } | |
| }; | |
| var parse = new ParseClient(); | |
| module.exports = parse; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment