Skip to content

Instantly share code, notes, and snippets.

@bob-sims
Last active October 1, 2015 09:47
Show Gist options
  • Save bob-sims/1966524 to your computer and use it in GitHub Desktop.
Save bob-sims/1966524 to your computer and use it in GitHub Desktop.
Demo CommonJS module for Titanium Mobile interaction with Drupal Services 3.x REST + JSON interface
This is a simple CommonJS module to wrap Titanium Ti.Network.HttpClient calls to interact with Drupal Services.
NOTE: Code updated at @TiConf using callback functions.
Tested using:
Drupal 6.24
Services 3.1
Titanium Mobile SDK 1.8.2 (Android 2.2 SDK)
Services module configured as this example:
http://www.tylerfrankenstein.com/code/android-app-with-drupal-7-services-phonegap-and-jquery-mobile
Special thanks to @grzegorzbartman (www.openbit.pl) for coaching and inspiration.
// NOTE: Below code is deprecated and I need to update using callback functions instead of
// app-level events
/*
Ti.API.info('starting up...');
var drupalServices = require('drupalServices');
var REST_URL = 'http://192.168.1.102:8888/drupal/api/rest/';
Ti.App.addEventListener('net:userLogoutReturned', function(data) {
Ti.API.info('userLogoutReturned: '+JSON.stringify(data));
})
Ti.App.addEventListener('net:userLoginReturned', function(data) {
Ti.API.info('userLoginReturned: '+JSON.stringify(data));
})
Ti.App.addEventListener('net:systemConnectReturned', function(data) {
Ti.API.info('systemConnectReturned: '+JSON.stringify(data));
})
Ti.App.addEventListener('net:nodeListReturned', function(data) {
Ti.API.info('nodeList returned: ');
for (var i = 0; i < data.length; i++) {
Ti.API.info('node '+i+':'+JSON.stringify(data[i]));
}
});
drupalServices.userLogout(REST_URL);
drupalServices.userLogin('<drupal username>','<drupal password>',REST_URL);
drupalServices.systemConnect(REST_URL);
drupalServices.nodeList(REST_URL);
// give userLogin time to complete, then attempt createNode
setTimeout(function() {
Ti.API.info('3 seconds, create node!');
// use Date() to create some pseudo-random numbers for title and body text tests
var date = new Date();
drupalServices.createNode(
{node:{
title: 'Titanium ' + date.getSeconds(),
type:'story',
body: 'body text, minutes: '+date.getMinutes(),
}
},
REST_URL)
}, 3000);
*/
/*
* @desc Retrieve list of nodes
* @param {String} url Service endpoint URL
* @param {Object} opts Utility payload, should have opts.success() and opts.error() callback functions
*
*/
var nodeList = function(url, opts) {
var xhr = Titanium.Network.createHTTPClient();
xhr.open('GET', url+'node.json');
xhr.send();
xhr.onload = function() {
var jsonObject = JSON.parse(this.responseText);
opts.success && opts.success(jsonObject);
};
xhr.onerror = function(e) {
console.info("nodeList error: "+JSON.stringify(this));
opts.error && opts.error({
"status":xhr.status,
"statusText":xhr.statusText
});
};
};
/*
* @desc Check current session status
* @param {String} url Service endpoint URL
* @param {Object} opts Utility payload, should have opts.success() and opts.error() callback functions
*
*/
var systemConnect = function(url, opts) {
var xhr = Titanium.Network.createHTTPClient();
url = url + 'system/connect.json';
Ti.API.info('url: '+url);
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send();
xhr.onload = function() {
var jsonObject = JSON.parse(this.responseText);
opts.success && opts.success(jsonObject);
};
xhr.onerror = function(e) {
Titanium.API.info(e.error);
opts.error && opts.error({
"status":xhr.status,
"statusText":xhr.statusText
});
};
};
/*
* @desc Login user given username and password
* @param {String} url Service endpoint URL
* @param {Object} opts Utility payload, should have properties: username, password, success(), and error() callback functions
*
*/
var userLogin = function(url, opts) {
var xhr = Titanium.Network.createHTTPClient();
url = url + 'user/login.json';
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type','application/json; charset=utf-8');
xhr.send({"username":opts.username,"password":opts.password});
xhr.onload = function() {
var jsonObject = JSON.parse(this.responseText);
opts.success && opts.success(jsonObject);
};
xhr.onerror = function(e) {
Titanium.API.info('userLogin error: '+e.error);
opts.error && opts.error({
"status":xhr.status,
"statusText":xhr.statusText
});
};
};
/*
* @desc Logout user
* @param {String} url Service endpoint URL
* @param {Object} opts Utility payload, should have properties: success() and error() callback functions
*
*/
var userLogout = function(url, opts) {
var xhr = Titanium.Network.createHTTPClient();
url = url + 'user/logout.json';
Ti.API.info('url: '+url);
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type','application/json; charset=utf-8');
xhr.send();
xhr.onload = function() {
var jsonObject = JSON.parse(this.responseText);
opts.success && opts.success(jsonObject);
};
xhr.onerror = function(e) {
Titanium.API.info('userLogout error: '+e.error);
opts.error && opts.error({
"status":xhr.status,
"statusText":xhr.statusText
});
};
};
/*
* @desc Create Drupal node
* @param {String} url Service endpoint URL
* @param {Object} opts Utility payload, should have properties: node property (JSON object), success() and error() callback functions
*
*/
var createNode = function(url, opts) {
//console.info(JSON.stringify(opts.node));
var xhr = Titanium.Network.createHTTPClient();
url = url + 'node';
Ti.API.info('creating node, url: '+url);
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type','application/json');
var obj = JSON.stringify(opts.node);
//Ti.API.info('node object: '+ obj);
xhr.send(obj);
xhr.onload = function(e) {
//var nodeObject = JSON.parse(this.responseText);
Ti.API.info('nodeObject: '+JSON.stringify(e));
var jsonObject = JSON.parse(this.responseText);
opts.success && opts.success(jsonObject);
};
xhr.onerror = function(e) {
// should do something more robust
Titanium.API.info('createNode error: '+xhr.statusText);
opts.error && opts.error({
"status":xhr.status,
"statusText":xhr.statusText
});
};
};
/*
* @desc Retrieve Drupal node creation form structure, including CCK fields
* @desc Requires custom Drupal module to expose node form to Services, D6 version: https://gist.github.com/1966524
* @param {String} url Service endpoint URL
* @param {Object} opts Utility payload, should have properties: success() and error() callback functions
*
*/
var nodeForm = function(url, opts) {
var xhr = Titanium.Network.createHTTPClient();
xhr.open('GET', url+'form/' + opts.node);
xhr.send();
xhr.onload = function() {
var jsonObject = JSON.parse(this.responseText);
opts.success && opts.success(jsonObject);
};
xhr.onerror = function(e) {
opts.error && opts.error({
"status":xhr.status,
"statusText":xhr.statusText
});
};
};
exports.createNode = createNode;
exports.userLogout = userLogout;
exports.userLogin = userLogin;
exports.nodeList = nodeList;
exports.systemConnect = systemConnect;
exports.nodeForm = nodeForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment