Skip to content

Commit 8ff4042

Browse files
author
Hamlet Hakobyan
committed
Added main functianality to build request url and make a call. Added unit tests.
1 parent 4cdb288 commit 8ff4042

12 files changed

+318
-38
lines changed

src/ApiServiceProvider.js

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function streamApiServiceFactory($http, $q) {
1313
factory = {},
1414
_instance;
1515

16+
Api.prototype.http = $http;
17+
Api.prototype.promise = $q;
18+
1619
factory.getInstance = function(config, returnNewInstance) {
1720
if(returnNewInstance) {
1821
return new Api(config);

src/service/Api.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ function Api(config) {
3030
}
3131
}
3232

33-
this.config = {
34-
url: parsedUrl.protocol + '//' + parsedUrl.host + path
35-
};
36-
37-
this.Constants = require('./ApiConstants');
38-
this.Methods = require('./ApiMethods');
33+
this.baseUrl = parsedUrl.protocol + '//' + parsedUrl.host + path;
3934
}
4035

36+
Api.prototype.Constants = require('./ApiConstants');
37+
Api.prototype.Methods = require('./ApiMethods');
38+
39+
require('./request')(Api);
40+
require('./get')(Api);
41+
require('./copy')(Api);
42+
require('./count')(Api);
43+
4144
module.exports = Api;

src/service/ApiServiceFactory.js

-27
This file was deleted.

src/service/copy.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
module.exports = function(Api) {
4+
Api.prototype.copy = function (objCode, objID, updates, fields) {
5+
var params = {
6+
copySourceID: objID
7+
};
8+
9+
return this.request(objCode, updates, params, fields, this.Methods.POST);
10+
};
11+
};

src/service/count.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
module.exports = function(Api) {
4+
Api.prototype.count = function(objCode, query) {
5+
var path = objCode + '/count';
6+
return this.request(path, null, query, null, this.Methods.GET)
7+
.then(function(response) {
8+
return response.data.count;
9+
});
10+
};
11+
};

src/service/get.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
module.exports = function(Api) {
4+
Api.prototype.get = function(objCode, IDs, fields) {
5+
var params;
6+
if(typeof IDs === 'string') {
7+
IDs = [IDs];
8+
}
9+
10+
if(IDs.length !== 0) {
11+
params = {
12+
ID: IDs
13+
};
14+
}
15+
16+
return this.request(objCode, null, params, fields, this.Methods.GET);
17+
};
18+
};

src/service/request.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
11
'use strict';
22

3+
var angular = require('angular');
4+
35
module.exports = function(Api) {
4-
Api.prototype.request = function(path, params, fields, method) {
6+
Api.prototype.request = function(path, data, params, fields, method) {
7+
this.config = {};
8+
this.config.url = this.baseUrl;
9+
path = path.indexOf('/') === 0 ? path : '/' + path;
10+
this.config.url = this.config.url + path;
11+
12+
fields = fields || [];
13+
if(typeof fields === 'string') {
14+
fields = [fields];
15+
}
16+
17+
this.config.method = method;
18+
if(method === this.Methods.POST || method === this.Methods.PUT) {
19+
if(!data) {
20+
throw new Error('You must specify \'data\' in case you have used PUT or POST');
21+
}
22+
23+
this.config.data = data;
24+
this.config.headers = {};
25+
this.config.headers['Content-Type'] = 'application/json;charset=utf-8';
26+
} else {
27+
if(data) {
28+
throw new Error('You must specify method PUT or POST in case have passed \'data\'');
29+
}
30+
}
31+
32+
params = params || {};
33+
if(fields.length !== 0) {
34+
params.fields = fields.join();
35+
}
36+
37+
this.config.params = params;
538

39+
return this.http(this.config);
640
};
741
};

src/spec/Api.spec.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,39 @@ describe('Api', function() {
5555

5656
it('should set it\'s config.url correctly when passed correct url and not passed version', function() {
5757
var api = streamApiService.getInstance({url: 'https://foo'});
58-
expect(api.config.url)
58+
expect(api.baseUrl)
5959
.toBe('https://foo/attask/api');
6060
});
6161

6262
it('should set it\'s config.url when passed valid url and version unsupported', function() {
6363
var api = streamApiService.getInstance({url: 'https://foo', version: 'unsupported'});
64-
expect(api.config.url)
64+
expect(api.baseUrl)
6565
.toBe('https://foo/attask/api-unsupported');
6666
});
6767

6868
it('should set it\'s config.url when passed valid url and version internal', function() {
6969
var api = streamApiService.getInstance({url: 'https://foo', version: 'internal'});
70-
expect(api.config.url)
70+
expect(api.baseUrl)
7171
.toBe('https://foo/attask/api-internal');
7272
});
7373

7474
it('should set it\'s config.url when passed valid url and version number', function() {
7575
var api = streamApiService.getInstance({url: 'https://foo', version: '5.0'});
76-
expect(api.config.url)
76+
expect(api.baseUrl)
7777
.toBe('https://foo/attask/api/v5.0');
7878
});
79+
80+
it('should return the same object on each getInstance regardless of passed config and version', function() {
81+
var api1 = streamApiService.getInstance({url: 'https://foo', version: '5.0'});
82+
var api2 = streamApiService.getInstance({url: 'https://bar', version: '4.0'});
83+
expect(api1).toBe(api2);
84+
});
85+
86+
it('should return new Api object when requested new', function() {
87+
var api1 = streamApiService.getInstance({url: 'https://foo', version: '5.0'});
88+
var api2 = streamApiService.getInstance({url: 'https://bar', version: '4.0'}, true);
89+
expect(api1).not.toBe(api2);
90+
});
91+
92+
7993
});

src/spec/copy.spec.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint-env jasmine */
2+
3+
'use strict';
4+
5+
var angular = require('angular');
6+
7+
describe('get', function() {
8+
beforeEach(function() {
9+
var ngModule = angular.module('mockModule', []);
10+
require('./../ApiServiceProvider')(ngModule);
11+
});
12+
13+
beforeEach(angular.mock.module('mockModule'));
14+
15+
var streamApiService,
16+
$httpBackend;
17+
beforeEach(angular.mock.inject(function(_streamApiService_, _$httpBackend_) {
18+
streamApiService = _streamApiService_;
19+
$httpBackend = _$httpBackend_;
20+
}));
21+
22+
var streamApi;
23+
beforeEach(function() {
24+
streamApi = streamApiService.getInstance({url: 'https://foo'});
25+
});
26+
27+
it('should make call to requested object', function() {
28+
var requestedUrl = 'https://foo/attask/api/task?copySourceID=12345678&fields=*';
29+
var headerData = function(headers) {
30+
return headers['Content-Type'] === 'application/json;charset=utf-8';
31+
};
32+
33+
$httpBackend.expectPOST(requestedUrl, {name:'copied task'}, headerData)
34+
.respond(200);
35+
36+
streamApi.copy('task', '12345678', {name:'copied task'}, '*');
37+
38+
$httpBackend.flush();
39+
});
40+
});

src/spec/count.spec.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* eslint-env jasmine */
2+
/* global dump */
3+
4+
'use strict';
5+
6+
var angular = require('angular');
7+
8+
describe('count', function() {
9+
beforeEach(function() {
10+
var ngModule = angular.module('mockModule', []);
11+
require('./../ApiServiceProvider')(ngModule);
12+
});
13+
14+
beforeEach(angular.mock.module('mockModule'));
15+
16+
var streamApiService,
17+
$httpBackend;
18+
beforeEach(angular.mock.inject(function(_streamApiService_, _$httpBackend_) {
19+
streamApiService = _streamApiService_;
20+
$httpBackend = _$httpBackend_;
21+
}));
22+
23+
var streamApi;
24+
beforeEach(function() {
25+
streamApi = streamApiService.getInstance({url: 'https://foo'});
26+
});
27+
28+
29+
// it('should make call to correct url', function() {
30+
// var requestedUrl = 'https://foo/attask/api/task/count?name=some+task+name&name_Mod=cicontains';
31+
// $httpBackend.expectGET(requestedUrl)
32+
// .respond(200);
33+
// var query = {};
34+
// query['name'] = 'some task name';
35+
// query['name' + streamApi.Constants.MOD] = streamApi.Constants.Operators.CICONTAINS;
36+
// streamApi.count('task', query);
37+
38+
// $httpBackend.flush();
39+
// });
40+
41+
it('should extract correct count from returend data', function(done) {
42+
var data = {count: 5};
43+
$httpBackend.whenGET()
44+
.respond(200, data);
45+
streamApi.count('task', {})
46+
.then(function(count) {
47+
expect(count).toBe(data.count);
48+
done();
49+
});
50+
$httpBackend.flush();
51+
});
52+
});

src/spec/get.spec.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* eslint-env jasmine */
2+
3+
'use strict';
4+
5+
var angular = require('angular');
6+
7+
describe('get', function() {
8+
beforeEach(function() {
9+
var ngModule = angular.module('mockModule', []);
10+
require('./../ApiServiceProvider')(ngModule);
11+
});
12+
13+
beforeEach(angular.mock.module('mockModule'));
14+
15+
var streamApiService,
16+
$httpBackend;
17+
beforeEach(angular.mock.inject(function(_streamApiService_, _$httpBackend_) {
18+
streamApiService = _streamApiService_;
19+
$httpBackend = _$httpBackend_;
20+
}));
21+
22+
var streamApi;
23+
beforeEach(function() {
24+
streamApi = streamApiService.getInstance({url: 'https://foo'});
25+
});
26+
27+
it('should make call by correct url with array of object IDs', function() {
28+
var requestedUrl = 'https://foo/attask/api/task?ID=12345678&ID=23456789&fields=*,projectID';
29+
$httpBackend.expect('GET', requestedUrl)
30+
.respond(200);
31+
32+
streamApi.get('task', ['12345678', '23456789'], ['*', 'projectID']);
33+
34+
$httpBackend.flush();
35+
});
36+
37+
it('should make call by correct url with single element in IDs array', function() {
38+
var requestedUrl = 'https://foo/attask/api/task?ID=12345678&fields=*,projectID';
39+
$httpBackend.expect('GET', requestedUrl)
40+
.respond(200);
41+
42+
streamApi.get('task', ['12345678'], ['*', 'projectID']);
43+
44+
$httpBackend.flush();
45+
});
46+
47+
it('should make call by correct url with string ID', function() {
48+
var requestedUrl = 'https://foo/attask/api/task?ID=12345678&fields=*,projectID';
49+
$httpBackend.expect('GET', requestedUrl)
50+
.respond(200);
51+
52+
streamApi.get('task', '12345678', ['*', 'projectID']);
53+
54+
$httpBackend.flush();
55+
});
56+
});

0 commit comments

Comments
 (0)