Skip to content

Adds an adapter for batch-request #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"devDependencies": {
"sinon": "~1.10.3",
"angular-mocks": ">=1.2.23",
"angular-resource": ">=1.2.23"
"angular-resource": ">=1.2.23",
"angular-route": ">=1.2.23"
},
"dependencies": {
"angular": ">= 1.2.23"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"karma-phantomjs-launcher": "^0.1.4",
"karma-sinon": "^1.0.3",
"mocha": "^1.20.1",
"phantomjs-polyfill": "0.*",
"sinon": "^1.10.2"
},
"ignore": [
Expand Down
133 changes: 133 additions & 0 deletions src/services/adapters/npmBatchRequestAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
(function() {
'use strict';

var adapterKey = 'npmBatchRequestAdapter';
var HttpBatchResponseData = window.ahb.HttpBatchResponseData;

/**
* HTTP Adapter for angular-http-batcher for converting multiple requests into a single
* request using the batch-request format.
*/
function NpmBatchRequestAdapter() {
this.key = adapterKey;
}

/**
* Transforms a GET request to the format expected by batch-request and attaches it to
* the httpConfig Object.
*
* @param requestIndex (Integer) - the index of the request in the pool of requests
* @param request (Object) - the Angular $http config Object for the request
* @param httpConfig (Object) - the Angular $http config Object for the batched request
*/
function transformGETRequest(requestIndex, request, httpConfig) {
var paramSerializer;

httpConfig.data[requestIndex] = {
method: request.method,
uri: request.url,
headers: request.headers
};
}

/**
* Transforms any request with a body to the format expected by batch-request and attaches
* it to the httpConfig Object.
*
* @param requestIndex (Integer) - the index of the request in the pool of requests
* @param request (Object) - the Angular $http config Object for the request
* @param httpConfig (Object) - the Angular $http config Object for the batched request
*/
function transformRequestWithBody(requestIndex, request, httpConfig) {
httpConfig.data[requestIndex] = {
method: request.method,
uri: request.url,
headers: request.headers,
body: request.data
};
}

/**
* Builds the single batch request from the given batch of pending requests.
*
* @throws (Error) If the requests do not use the same HTTP method
*
* @param requests (Object[]) - the collection of standard Angular $http config Objects
* that should be bundled into a single batch request
* @param config (Object) - the http-batch configuration Object
*
* @return (Object) a standard Angular $http config Object for a batch request that
* represents all the provided requests
*/
NpmBatchRequestAdapter.prototype.buildRequest = function buildRequest(requests, config) {
var requestIndex;
var transformRequest;
var httpConfig = {
method: 'POST',
url: config.batchEndpointUrl,
headers: config.batchRequestHeaders || {},
data: {}
};

for(requestIndex = 0; requestIndex < requests.length; requestIndex++) {
switch(requests[requestIndex].method) {
case 'GET':
transformRequest = transformGETRequest;

break;
default:
transformRequest = transformRequestWithBody;
}

transformRequest(requestIndex, requests[requestIndex], httpConfig);
}

return httpConfig;
};

/**
* Parses the raw response from the server and maps each response to the request to which
* the server is responding.
*
* @param requests (Object[]) - the collection of standard Angular $http config Objects
* originally provided when generating the batch request
* @param rawResponse (Object) - the raw response returned from the server
*
* @return (HttpBatchResponseData[]) an array of the HttpBatchResponseData generated from
* the rawResponse
*/
NpmBatchRequestAdapter.prototype.parseResponse = function parseResponse(requests, rawResponse) {
var requestIndex;
var batchResponses = [];
var response;

for(requestIndex = 0; requestIndex < requests.length; requestIndex++) {
response = rawResponse.data[requestIndex];

batchResponses.push(new HttpBatchResponseData(
requests[requestIndex],
response.statusCode,
'',
response.body,
response.headers
));
}

return batchResponses;
};

/**
* Guard method. Always returns true.
*
* @param request (Object) - the standard Angular $http config Object for the request that
* might be pooled with other requests
*
* @return (Boolean) true iff the request can be batched with other requests; false
* otherwise
*/
NpmBatchRequestAdapter.prototype.canBatchRequest = function canBatchRequest(request) {
return true;
};

angular.module(window.ahb.name).service(adapterKey, NpmBatchRequestAdapter);
})();
7 changes: 4 additions & 3 deletions src/services/httpBatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ BatchRequestManager.prototype.send = sendFn;
BatchRequestManager.prototype.addRequest = addRequestFn;
BatchRequestManager.prototype.flush = flushFn;

function HttpBatcherFn($injector, $timeout, httpBatchConfig, httpBatchAdapter, nodeJsMultiFetchAdapter) {
function HttpBatcherFn($injector, $timeout, httpBatchConfig, httpBatchAdapter, nodeJsMultiFetchAdapter, npmBatchRequestAdapter) {
var self = this,
currentBatchedRequests = {},
adapters = {
httpBatchAdapter: httpBatchAdapter,
nodeJsMultiFetchAdapter: nodeJsMultiFetchAdapter
httpBatchAdapter: httpBatchAdapter,
nodeJsMultiFetchAdapter: nodeJsMultiFetchAdapter,
npmBatchRequestAdapter: npmBatchRequestAdapter
};

self.canBatchRequest = canBatchRequestFn;
Expand Down
7 changes: 3 additions & 4 deletions tests/karma.conf.shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ shared.files = [
'src/angular-http-batch.js',
'src/providers/httpBatchConfig.js',
'src/services/httpBatcher.js',
'src/services/adapters/httpBatchResponseData.js',
'src/services/adapters/httpAdapter.js',
'src/services/adapters/nodeJsMultiFetchAdapter.js',
'src/config/httpBackendDecorator.js'
'src/services/adapters/*.js',
'src/config/httpBackendDecorator.js',
'./node_modules/phantomjs-polyfill/bind-polyfill.js'
];

module.exports = shared;
Loading