Created
August 5, 2014 07:32
-
-
Save gbougeard/c11be958bcfc3c76402e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
"use strict"; | |
var tokenUrl = "http://localhost/token"; | |
var token = ""; | |
// Static method default options. | |
var _opts = { | |
clientId : "", | |
clientSecret : "" | |
}; | |
// Static method. | |
$.itApi = function (options) { | |
// Override default options with passed-in options. | |
_opts = $.extend({}, _opts, options); | |
// console.log("options", options, _opts); | |
init(); | |
// Return something awesome. | |
return { | |
POST: $.fn.POST, | |
GET : $.fn.GET | |
}; | |
}; | |
var init = function() { | |
// console.info("init"); | |
if (!_opts.clientId) throw "Could not init itApi because no clientId was found."; | |
if (!_opts.clientSecret) throw "Could not init itApi because no clientSecret was found."; | |
var headers = { | |
"Authorization": btoa(_opts.clientId + ":" + _opts.clientSecret), | |
"Access-Control-Request-Headers": "x-requested-with" | |
}; | |
// console.log(headers); | |
$.ajax | |
({ | |
type: "POST", | |
url: tokenUrl, | |
dataType: "json", | |
async: false, | |
crossDomain: true, | |
data : "grant_type=client_credentials&scope=read", | |
headers: headers, | |
success: function (data, textStatus, jqXHR){ | |
// console.log(data); | |
if((data instanceof Object)){ | |
token = data.access_token; | |
} else { | |
console.error("Could not init itApi because token response is not an Object.", data); | |
} | |
}, | |
error : function(error){ | |
console.error("Could not init itApi because no token retrieved.", error); | |
} | |
}); | |
}; | |
$.fn.POST = function (url, body, dataType) { | |
if (!token) throw "Could not perform AJAX call because no valid tokens was found."; | |
var _settings = { | |
type : "POST", | |
url : url, | |
data : body, | |
crossDomain : true, | |
dataType : "json", | |
headers : { "Authorization" : "Bearer " + token } | |
}; | |
if (dataType) _settings.dataType = dataType; | |
return $.ajax(_settings); | |
}; | |
$.fn.GET = function (url, dataType) { | |
if (!token) throw "Could not perform AJAX call because no valid tokens was found."; | |
var _settings = { | |
type : "GET", | |
url : url, | |
crossDomain : true, | |
dataType : "json", | |
headers : { "Authorization" : "Bearer " + token } | |
}; | |
if (dataType) _settings.dataType = dataType; | |
return $.ajax(_settings); | |
}; | |
}(jQuery)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="dist/itApi.min.js"></script> | |
<script> | |
jQuery(function($) { | |
var itApi = $.itApi({clientId : "91e68295-527d-49a4-94c7-5726c0fe2dc2", | |
clientSecret : "5baa4885-4385-4193-abcb-ed0d225bc45b" }); | |
itApi.GET("http://localhost/rest-public/rs/dummy/test", "text") | |
.done(function(data, textStatus, jqXHR) { | |
console.info("success", data, textStatus, jqXHR ); | |
$("#msg" ).text(data); | |
}) | |
.fail(function(data, textStatus, errorThrown) { | |
console.error("error", data, textStatus ); | |
$("#msg" ).text(data.responseText); | |
}) | |
.always(function(data, textStatus, errorThrown) { | |
console.log( "complete", data, textStatus, errorThrown ); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment