@@ -181,6 +181,50 @@ if (forcetk.Client === undefined) {
181181 } ) ;
182182 }
183183
184+ /*
185+ * Low level utility function to call the Salesforce endpoint specific for Apex REST API.
186+ * @param path resource path relative to /services/apexrest
187+ * @param callback function to which response will be passed
188+ * @param [error=null] function to which jqXHR will be passed in case of error
189+ * @param [method="GET"] HTTP method for call
190+ * @param [payload=null] payload for POST/PATCH etc
191+ */
192+ forcetk . Client . prototype . apexrest = function ( path , callback , error , method , payload , retry ) {
193+ var that = this ;
194+ var url = this . instanceUrl + '/services/apexrest' + path ;
195+
196+ $j . ajax ( {
197+ type : method || "GET" ,
198+ async : this . asyncAjax ,
199+ url : ( this . proxyUrl !== null ) ? this . proxyUrl : url ,
200+ contentType : 'application/json' ,
201+ cache : false ,
202+ processData : false ,
203+ data : payload ,
204+ success : callback ,
205+ error : ( ! this . refreshToken || retry ) ? error : function ( jqXHR , textStatus , errorThrown ) {
206+ if ( jqXHR . status === 401 ) {
207+ that . refreshAccessToken ( function ( oauthResponse ) {
208+ that . setSessionToken ( oauthResponse . access_token , null ,
209+ oauthResponse . instance_url ) ;
210+ that . ajax ( path , callback , error , method , payload , true ) ;
211+ } ,
212+ error ) ;
213+ } else {
214+ error ( jqXHR , textStatus , errorThrown ) ;
215+ }
216+ } ,
217+ dataType : "json" ,
218+ beforeSend : function ( xhr ) {
219+ if ( that . proxyUrl !== null ) {
220+ xhr . setRequestHeader ( 'SalesforceProxy-Endpoint' , url ) ;
221+ }
222+ xhr . setRequestHeader ( that . authzHeader , "OAuth " + that . sessionId ) ;
223+ xhr . setRequestHeader ( 'X-User-Agent' , 'salesforce-toolkit-rest-javascript/' + that . apiVersion ) ;
224+ }
225+ } ) ;
226+ }
227+
184228 /*
185229 * Lists summary information about each Salesforce.com version currently
186230 * available, including the version, label, and a link to each version's
0 commit comments