-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCloud.js
161 lines (128 loc) · 3.42 KB
/
Cloud.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var Alloy = require('alloy'),
_ = Alloy._,
Backbone = Alloy.Backbone;
var Cloud = (function() {
// constructor
var cls = function(config) {
_.extend(this, {
/**
* @property {Ti.Network.HttpClient} httpClient The internal httpClient used to perform the request
*/
httpClient: null,
onSuccess: null,
onError: null,
/**
* Constructor
*
* @param {Object} config Class configuration
*/
construct: function(config) {
this.onSuccess = config.success;
this.onError = config.error;
var clientConfig = {
onload: _.partial(onSuccess, this),
onerror: _.partial(onError, this),
ondatastream: config.progress,
onsendstream: config.progress,
onreadystatechange: config.stateChange,
timeout: config.timeout || 10000
};
// Create http client
this.httpClient = Ti.Network.createHTTPClient(clientConfig);
// make sure we have a data object
config.data = config.data || {};
// Prepare the data (if required)
var url = config.url,
method = config.method || 'GET',
data, query;
if (method == 'GET' || method == 'DELETE') {
query = toQueryString(config.data);
if (query)
url = url + (url.indexOf('?') > 0 ? '&' : '?') + query;
} else
data = config.data || {};
// Add base Url if url does not start with http
if(url.substr(0, 4) !== 'http')
url = Alloy.CFG.baseUrl + url;
Ti.API.debug(method + ': ' + url);
// Ti.API.info(data);
// Open connection
this.httpClient.open(method, url);
// Set headers after opening connection
for (var key in config.headers) {
this.httpClient.setRequestHeader(key, config.headers[key]);
}
if(config.json) {
this.httpClient.setRequestHeader('Content-Type', 'application/json');
if(data)
data = JSON.stringify(data);
}
Ti.API.debug(config.headers);
this.httpClient.send(data);
},
/**
* Remove this object from memory gracefully
*/
destruct: function() {
if (this.httpClient) {
this.httpClient.abort();
this.httpClient = null;
}
}
});
// Call construct on object initialization
this.construct(config);
};
function onSuccess(req, result) {
var response;
if (result.success) {
try {
response = JSON.parse(this.responseText);
} catch (e) {
Ti.API.error('Tried to parse response, but it was not valid json.');
Ti.API.error(this.responseText);
response = this.responseText;
}
}
// Call callback
if (req.onSuccess)
req.onSuccess(response);
}
function onError(req, result) {
var response;
try {
response = JSON.parse(this.responseText);
} catch (e) {
Ti.API.error('Tried to parse response, but it was not valid json.');
Ti.API.error(this.responseText);
response = this.responseText;
}
Ti.API.info(response);
// Call callback
if (req.onError)
req.onError(response);
}
/**
* Converts the supplied object into a query string
* @private
*
* @param {Object} data The object to convert
* @return {String} The generated query string
*/
function toQueryString(data) {
var query = [],
queryString = '',
key;
if (data) {
for (key in data) {
if (data.hasOwnProperty(key))
query.push(Ti.Network.encodeURIComponent(key) + '=' + Ti.Network.encodeURIComponent(data[key]));
}
if (query.length)
queryString = query.join('&');
}
return queryString;
}
return cls;
})();
module.exports = Cloud;