Skip to content
Extraits de code Groupes Projets
Valider 07291cd4 rédigé par diosmosis's avatar diosmosis
Parcourir les fichiers

Fix bug in setting of POST params in piwikApi angularjs object, add new...

Fix bug in setting of POST params in piwikApi angularjs object, add new bulkFetch method to piwikApi object and add tests for it.
parent bec9ce27
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -125,10 +125,9 @@ angular.module('piwikApp.service').factory('piwikApi', function ($http, $q, $roo ...@@ -125,10 +125,9 @@ angular.module('piwikApp.service').factory('piwikApi', function ($http, $q, $roo
* @return {object} * @return {object}
* @private * @private
*/ */
function getPostParams () { function getPostParams (params) {
return { params.token_auth = piwik.token_auth;
token_auth: piwik.token_auth return params;
};
} }
/** /**
...@@ -212,5 +211,28 @@ angular.module('piwikApp.service').factory('piwikApi', function ($http, $q, $roo ...@@ -212,5 +211,28 @@ angular.module('piwikApp.service').factory('piwikApi', function ($http, $q, $roo
return this.fetch(getParams); return this.fetch(getParams);
}; };
/**
* Convenience method that will perform a bulk request using Piwik's API.getBulkRequest method.
* Bulk requests allow you to execute multiple Piwik requests with one HTTP request.
*
* @param {object[]} requests
* @return {HttpPromise} a promise that is resolved when the request finishes. The argument passed
* to the .then(...) callback will be an array with one element per request
* made.
*/
piwikApi.bulkFetch = function (requests) {
var bulkApiRequestParams = {
urls: requests.map(function (requestObj) { return '?' + $.param(requestObj); })
};
return this.post({method: "API.getBulkRequest"}, bulkApiRequestParams).then(function (response) {
if (!(response instanceof Array)) {
response = [response];
}
return response;
});
};
return piwikApi; return piwikApi;
}); });
\ No newline at end of file
...@@ -19,6 +19,17 @@ describe('piwikApiClient', function () { ...@@ -19,6 +19,17 @@ describe('piwikApiClient', function () {
$httpBackend = $injector.get('$httpBackend'); $httpBackend = $injector.get('$httpBackend');
$httpBackend.when('POST', /.*getBulkRequest.*/).respond(function (method, url, data, headers) {
url = url.replace(/date=[^&]+/, "date=");
var responses = [
"Response #1: " + url + " - " + data,
"Response #2: " + url + " - " + data
];
return [200, JSON.stringify(responses)];
});
$httpBackend.when('POST', /.*/).respond(function (method, url, data, headers) { $httpBackend.when('POST', /.*/).respond(function (method, url, data, headers) {
url = url.replace(/date=[^&]+/, "date="); url = url.replace(/date=[^&]+/, "date=");
return [200, "Request url: " + url]; return [200, "Request url: " + url];
...@@ -185,4 +196,30 @@ describe('piwikApiClient', function () { ...@@ -185,4 +196,30 @@ describe('piwikApiClient', function () {
$httpBackend.flush(); $httpBackend.flush();
}); });
it("should perform a bulk request correctly when bulkFetch is called on the piwikApi", function (done) {
piwikApi.bulkFetch([
{
method: "SomePlugin.action",
param: "value"
},
{
method: "SomeOtherPlugin.action"
}
]).then(function (response) {
var restOfExpected = "index.php?date=&format=JSON2&idSite=1&method=API.getBulkRequest&" +
"module=API&period=day - urls%5B%5D=%3Fmethod%3DSomePlugin.action%26param%3D" +
"value&urls%5B%5D=%3Fmethod%3DSomeOtherPlugin.action&token_auth=100bf5eeeed1468f3f9d93750044d3dd";
expect(response.length).to.equal(2);
expect(response[0]).to.equal("Response #1: " + restOfExpected);
expect(response[1]).to.equal("Response #2: " + restOfExpected);
done();
}).catch(function (ex) {
done(ex);
});
$httpBackend.flush();
});
}); });
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter