-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasypost.min.js
More file actions
1 lines (1 loc) · 2.07 KB
/
easypost.min.js
File metadata and controls
1 lines (1 loc) · 2.07 KB
1
function Client(e,t){this.apiKey=e;this.baseUrl="https://www.geteasypost.com/api";this.debug=!1;typeof t!="undefined"&&t===!0&&(this.debug=!0);this.debug&&Ti.API.info("EasyPost: Init with key: "+this.apiKey)}exports.Client=Client;Client.prototype.setApiKey=function(e){var t=this;t.apiKey=e;t.debug&&Ti.API.info("EasyPost: Setting API key: "+t.apiKey)};Client.prototype.apiUrl=function(e,t){var n=this,r=n.baseUrl+"/"+e+"/"+t;n.debug&&Ti.API.info("EasyPost: Setting API URL: "+r);return r};Client.prototype.post=function(e,t,n){var r=this;r.debug&&Ti.API.info("EasyPost: Init POST with API Key: "+r.apiKey);var i=Ti.Network.createHTTPClient({onload:function(){var e=JSON.parse(this.responseText);r.debug&&Ti.API.info("EasyPost: Success: "+JSON.stringify(e));n(e)},onerror:function(e){var t=JSON.parse(this.responseText);r.debug&&Ti.API.info("EasyPost: Error: "+JSON.stringify(t));cb(t)}}),s="Basic "+Titanium.Utils.base64encode(r.apiKey+":"),o=r.queryString(t);i.open("POST",e);i.setRequestHeader("Authorization",s);i.send(o)};Client.prototype.verify=function(e,t){var n=this,r={address:e};n.debug&&Ti.API.info("EasyPost: Verify: "+JSON.stringify(e));n.post(n.apiUrl("address","verify"),r,t)};Client.prototype.rates=function(e,t){var n=this;n.debug&&Ti.API.info("EasyPost: Rates: "+JSON.stringify(e));return n.post(n.apiUrl("postage","rates"),e,t)};Client.prototype.compare=function(e,t){var n=this;n.debug&&Ti.API.info("EasyPost: Compare: "+JSON.stringify(e));return n.rates(e,t)};Client.prototype.buy=function(e,t){var n=this;n.debug&&Ti.API.info("EasyPost: Buy: "+JSON.stringify(e));return n.post(n.apiUrl("postage","buy"),e,t)};Client.prototype.get=function(e,t){var n=this,r={label_file_name:e};n.debug&&Ti.API.info("EasyPost: Get: "+e);return n.post(n.apiUrl("postage","get"),r,t)};Client.prototype.listAll=function(e){var t=this;return t.post(t.apiUrl("postage","list"),{},e)};Client.prototype.queryString=function(e){var t=this;for(var n in e)if(typeof e[n]=="object"&&e[n]!==null){var r=e[n];delete e[n];for(var i in r){var s=n+"["+i+"]";e[s]=r[i]}}var o=[];for(n in e)o.push(n+"="+e[n]);return o.join("&")};