Skip to content

Instantly share code, notes, and snippets.

@kamranzafar
Last active December 13, 2015 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kamranzafar/4977499 to your computer and use it in GitHub Desktop.
Save kamranzafar/4977499 to your computer and use it in GitHub Desktop.
JQuery REST Client. A utility class to consume REST services from client side using JQuery.
/*
* JQuery REST Client
* Author: Kamran
* Email: xeus.man@gmail.com
*
* Copyright (C) 2013 Kamran
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
RestClient = function(goptions) {
this.goptions = goptions;
var constants = {
dataType : 'json',
contentType : 'application/json',
processData : true,
timeout : 120000,
synchronous : false,
errorRedirect : false
};
var doAjax = function(method, gopts, opts) {
var dt = opts.dataType ? opts.dataType : (gopts.dataType ? gopts.dataType : constants.dataType);
// redirect on error if HTTP status code matches
var errorRedirect = opts.errorRedirect ? opts.errorRedirect : (gopts.errorRedirect ? gopts.errorRedirect
: constants.errorRedirect);
var errorRedirectCodes = opts.errorRedirectCodes ? opts.errorRedirectCodes : gopts.errorRedirectCodes;
var errorRedirectUrl = opts.errorRedirectUrl ? opts.errorRedirectUrl : gopts.errorRedirectUrl;
var errorCallback = opts.error ? opts.error : (gopts.error ? gopts.error : function(req, status, ex) {
});
var errorFunction = errorCallback;
if (errorRedirect) {
errorFunction = function(req, status, ex) {
if (jQuery.inArray(req.status, errorRedirectCodes)) {
window.location.href = errorRedirectUrl;
} else {
errorCallback(req);
}
};
}
$.ajax({
type : method,
url : gopts.url.lastIndexOf('/') == gopts.url.length - 1 ? gopts.url + opts.path : gopts.url + '/'
+ opts.path,
headers : opts.headers ? opts.headers : (gopts.headers ? gopts.headers : {}),
data : opts.model ? /^json/i.test(dt) ? JSON.stringify(opts.model) : opts.model : '',
dataType : dt,
contentType : opts.contentType ? opts.contentType : (gopts.contentType ? gopts.contentType
: constants.contentType),
processData : opts.processData ? opts.processData : (gopts.processData ? gopts.processData
: constants.processData),
timeout : opts.timeout ? opts.timeout : (gopts.timeout ? gopts.timeout : constants.timeout),
success : opts.success ? opts.success : (gopts.success ? gopts.success : function(data) {
}),
error : errorFunction,
complete : opts.complete ? opts.complete : (gopts.complete ? gopts.complete : function() {
}),
async : opts.synchronous ? !opts.synchronous : (gopts.synchronous ? !gopts.synchronous
: !constants.synchronous)
});
};
this.post = function(opts) {
doAjax('POST', this.goptions, opts);
};
this.put = function(opts) {
doAjax('PUT', this.goptions, opts);
};
this.get = function(opts) {
doAjax('GET', this.goptions, opts);
};
this.remove = function(opts) {
doAjax('DELETE', this.goptions, opts);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment