Add headers option to halRequest requests

pull/4734/head
Alex Dik 8 years ago
parent 0658ccaec0
commit b764095348
  1. 21
      frontend/app/components/api/api-v3/hal-request/hal-request.service.test.ts
  2. 31
      frontend/app/components/api/api-v3/hal-request/hal-request.service.ts

@ -29,10 +29,13 @@
import {opApiModule} from '../../../../angular-modules';
import {HalRequestService} from './hal-request.service';
import {HalResource} from '../hal-resources/hal-resource.service';
import IPromise = angular.IPromise;
import IRootScopeService = angular.IRootScopeService;
import IHttpBackendService = angular.IHttpBackendService;
describe('halRequest service', () => {
var $httpBackend:ng.IHttpBackendService;
var $rootScope:ng.IRootScopeService;
var $httpBackend:IHttpBackendService;
var $rootScope:IRootScopeService;
var halRequest:HalRequestService;
beforeEach(angular.mock.module(opApiModule.name));
@ -46,9 +49,11 @@ describe('halRequest service', () => {
describe('when requesting data', () => {
var resource:HalResource;
var promise:ng.IPromise<HalResource>;
var promise:IPromise<HalResource>;
var method:string;
var data:any;
var headers = {Accept: 'foo'};
const methods = ['get', 'put', 'post', 'patch', 'delete'];
const runExpectations = () => {
it('should return a HalResource', () => {
@ -56,7 +61,11 @@ describe('halRequest service', () => {
});
};
const respond = status => {
$httpBackend.expect(method.toUpperCase(), 'href', data).respond(status, {});
$httpBackend.expect(method.toUpperCase(), 'href', data, (headers:any) => {
return headers.Accept === 'foo';
})
.respond(status, {});
promise
.then(res => resource = res)
.catch(res => resource = res);
@ -91,13 +100,13 @@ describe('halRequest service', () => {
describe('when calling the http methods of the service', () => {
runRequests(() => {
promise = halRequest[method]('href', data);
promise = halRequest[method]('href', data, headers);
});
});
describe('when calling request()', () => {
runRequests(() => {
promise = halRequest.request(method, 'href', data);
promise = halRequest.request(method, 'href', data, headers);
});
});

@ -29,6 +29,7 @@
import {opApiModule} from '../../../../angular-modules';
import {HalResource} from '../hal-resources/hal-resource.service';
import {HalResourceFactoryService} from '../hal-resource-factory/hal-resource-factory.service';
import IPromise = angular.IPromise;
export class HalRequestService {
constructor(protected $q:ng.IQService,
@ -42,14 +43,15 @@ export class HalRequestService {
* @param method
* @param href
* @param data
* @param headers
* @returns {IPromise<HalResource>}
*/
public request(method:string, href:string, data?:any):ng.IPromise<HalResource> {
public request(method:string, href:string, data?:any, headers:any = {}):IPromise<HalResource> {
if (!href) {
return this.$q.when(null);
}
const config:any = {method: method, url: href, data: data};
const config:any = {method: method, url: href, data: data, headers: headers};
const createResource =
response => this.halResourceFactory.createHalResource(response.data);
@ -66,20 +68,22 @@ export class HalRequestService {
*
* @param href
* @param params
* @param headers
* @returns {ng.IPromise<HalResource>}
*/
public get(href:string, params?:any):ng.IPromise<HalResource> {
return this.request('get', href, params);
public get(href:string, params?:any, headers?:any):IPromise<HalResource> {
return this.request('get', href, params, headers);
}
/**
* Perform a PUT request and return a resource promise.
* @param href
* @param data
* @param headers
* @returns {ng.IPromise<HalResource>}
*/
public put(href:string, data?:any):ng.IPromise<HalResource> {
return this.request('put', href, data);
public put(href:string, data?:any, headers?:any):IPromise<HalResource> {
return this.request('put', href, data, headers);
}
/**
@ -87,10 +91,11 @@ export class HalRequestService {
*
* @param href
* @param data
* @param headers
* @returns {ng.IPromise<HalResource>}
*/
public post(href:string, data?:any):ng.IPromise<HalResource> {
return this.request('post', href, data);
public post(href:string, data?:any, headers?:any):IPromise<HalResource> {
return this.request('post', href, data, headers);
}
/**
@ -98,10 +103,11 @@ export class HalRequestService {
*
* @param href
* @param data
* @param headers
* @returns {ng.IPromise<HalResource>}
*/
public patch(href:string, data?:any):ng.IPromise<HalResource> {
return this.request('patch', href, data);
public patch(href:string, data?:any, headers?:any):IPromise<HalResource> {
return this.request('patch', href, data, headers);
}
/**
@ -109,10 +115,11 @@ export class HalRequestService {
*
* @param href
* @param data
* @param headers
* @returns {ng.IPromise<HalResource>}
*/
public delete(href:string, data?:any):ng.IPromise<HalResource> {
return this.request('delete', href, data);
public delete(href:string, data?:any, headers?:any):IPromise<HalResource> {
return this.request('delete', href, data, headers);
}
}

Loading…
Cancel
Save