Skip to content
This repository has been archived by the owner on Jan 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from zooniverse/default-params
Browse files Browse the repository at this point in the history
Allow setting default params
  • Loading branch information
edpaget committed Jun 29, 2015
2 parents 26a3d45 + a6caa41 commit a530394
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions json-api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ JSONAPIClient = (function(superClass) {

JSONAPIClient.prototype.headers = null;

JSONAPIClient.prototype.params = null;

JSONAPIClient.prototype.reads = 0;

JSONAPIClient.prototype.writes = 0;
Expand All @@ -170,14 +172,16 @@ JSONAPIClient = (function(superClass) {
function JSONAPIClient(root, headers1) {
this.root = root;
this.headers = headers1 != null ? headers1 : {};
this.params = {};
JSONAPIClient.__super__.constructor.call(this, null);
this._typesCache = {};
}

JSONAPIClient.prototype.request = function(method, url, payload, headers) {
var allHeaders, fullURL, request;
var allHeaders, fullPayload, fullURL, request;
method = method.toUpperCase();
fullURL = this.root + url;
fullPayload = mergeInto({}, this.params, payload);
allHeaders = mergeInto({}, DEFAULT_TYPE_AND_ACCEPT, this.headers, headers);
if (indexOf.call(READ_OPS, method) >= 0) {
this.update({
Expand All @@ -188,7 +192,7 @@ JSONAPIClient = (function(superClass) {
writes: this.writes + 1
});
}
request = makeHTTPRequest(method, fullURL, payload, allHeaders);
request = makeHTTPRequest(method, fullURL, fullPayload, allHeaders);
request["catch"]((function(_this) {
return function() {
return null;
Expand Down
5 changes: 4 additions & 1 deletion src/json-api-client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@ WRITE_OPS = ['POST', 'PUT', 'DELETE']
class JSONAPIClient extends Model
root: '/'
headers: null
params: null
reads: 0
writes: 0

_typesCache: null # Types that have been defined

constructor: (@root, @headers = {}) ->
@params = {}
super null
@_typesCache = {}

request: (method, url, payload, headers) ->
method = method.toUpperCase()
fullURL = @root + url
fullPayload = mergeInto {}, @params, payload
allHeaders = mergeInto {}, DEFAULT_TYPE_AND_ACCEPT, @headers, headers

if method in READ_OPS
@update reads: @reads + 1
else if method in WRITE_OPS
@update writes: @writes + 1

request = makeHTTPRequest method, fullURL, payload, allHeaders
request = makeHTTPRequest method, fullURL, fullPayload, allHeaders

request
.catch =>
Expand Down

0 comments on commit a530394

Please sign in to comment.