Skip to content

Commit b814b20

Browse files
author
Pat Patterson
committed
Merge pull request developerforce#9 from tomgersic/master
Utility function to query the Chatter API and download a file.
2 parents 8fee54b + 192e640 commit b814b20

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

forcetk.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,66 @@ if (forcetk.Client === undefined) {
181181
});
182182
}
183183

184+
/**
185+
* Utility function to query the Chatter API and download a file
186+
* Note, raw XMLHttpRequest because JQuery mangles the arraybuffer
187+
* This should work on any browser that supports XMLHttpRequest 2 because arraybuffer is required.
188+
* For mobile, that means iOS >= 5 and Android >= Honeycomb
189+
* @author Tom Gersic
190+
* @param path resource path relative to /services/data
191+
* @param mimetype of the file
192+
* @param callback function to which response will be passed
193+
* @param [error=null] function to which request will be passed in case of error
194+
* @param rety true if we've already tried refresh token flow once
195+
**/
196+
forcetk.Client.prototype.getChatterFile = function(path,mimeType,callback,error,retry) {
197+
var that = this;
198+
199+
var url = this.instanceUrl + path;
200+
201+
var request = new XMLHttpRequest();
202+
203+
204+
request.open("GET", url, true);
205+
request.responseType = "arraybuffer";
206+
207+
request.setRequestHeader(that.authzHeader, "OAuth " + that.sessionId);
208+
request.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion);
209+
210+
request.onreadystatechange = function() {
211+
// continue if the process is completed
212+
if (request.readyState == 4) {
213+
// continue only if HTTP status is "OK"
214+
if (request.status == 200) {
215+
try {
216+
// retrieve the response
217+
callback(request.response);
218+
}
219+
catch(e) {
220+
// display error message
221+
alert("Error reading the response: " + e.toString());
222+
}
223+
}
224+
//refresh token in 401
225+
else if(request.status == 401 && !retry) {
226+
that.refreshAccessToken(function(oauthResponse) {
227+
that.setSessionToken(oauthResponse.access_token, null,oauthResponse.instance_url);
228+
that.getChatterFile(path, mimeType, callback, error, true);
229+
},
230+
error);
231+
}
232+
else {
233+
// display status message
234+
error(request,request.statusText,request.response);
235+
}
236+
}
237+
238+
}
239+
240+
request.send();
241+
242+
}
243+
184244
/*
185245
* Low level utility function to call the Salesforce endpoint specific for Apex REST API.
186246
* @param path resource path relative to /services/apexrest

0 commit comments

Comments
 (0)