consumerportal
Version:
mydna Custimised for you
269 lines (239 loc) • 10 kB
text/typescript
/// <reference path="../../includes.ts" />
declare function unescape(es: any): any;
module apiSrvc {
export interface IApiService {
get_request_params(resource: any, params?: any): any;
post_request(resource: any, params: any): any;
registration_requests(resource: any, params: any, addOauth: boolean, requestMethod: string, ContentType: string): any;
delete_request(resource: any, params: any): any;
login(resource: any, params: any): any;
validateTokenOAuth(params: any): any;
post_request_urlEncode(resource: any, params: any): any;
get_request_params_otherResource(resource: any, params?: any):any;
post_request_other(resource: any, params: any): any;
registration_requests_Other(resource: any, params: any, requestMethod: string, ContentType: string): any;
}
export class ApiService implements IApiService {
static $inject = [
'$http',
'$q',
'mydnaValues',
'$httpParamSerializer',
'authenSrvc',
'appLocalStorage'
];
constructor(
private $http: angular.IHttpService,
private $q: angular.IQService,
private mydnaValues: ImyDNAValues,
private $httpParamSerializer: any,
private authenSrvc: authenSrvc.IAuthenService,
private storage: app.IAppStorage
) {
}
call(method: string, resource: string, params: any, authType: string = 'bearer', contentType: string = null, cache: boolean = false ) {
let headers: any = {};
if (authType === 'bearer') {
headers.Authorization = 'Bearer ' + this.storage.getItem('Token');
} else if (authType === 'oath') {
headers.Authorization = 'Oauth ' + this.authenSrvc.getOauth().access_token;
}
if (contentType) {
headers.contentType = contentType;
if (contentType === 'application/x-www-form-urlencoded') {
params = this.$httpParamSerializer(params);
}
}
let defer = this.$q.defer();
this.$http({
method: method,
url: resource,
headers: headers,
data: params,
cache: cache
}).then((data) => {
defer.resolve(data.data);
}, (error: any) => {
defer.reject(error);
});
return defer.promise;
}
get(url: string, params?: any, auth: boolean = true) { return this.call('GET', url, params, auth ? 'bearer': null); }
post(url: string, params?: any, auth: boolean = true) { return this.call('POST', url, params, auth ? 'bearer': null); }
del(url: string, params?: any, auth: boolean = true) { return this.call('DELETE', url, params, auth ? 'bearer': null); }
get_request_params(resource: any, params?: any) {
let defer = this.$q.defer();
let AuthenToken = this.storage.getItem('Token');
if (AuthenToken) {
this.$http({
method: 'GET',
url: resource,
headers: { 'Authorization': 'Bearer ' + AuthenToken },
data: params,
cache: false
}).then((data) => {
defer.resolve(data.data);
}, (error: any) => {
defer.reject(error);
});
} else {
defer.reject({ status: 401 })
}
return defer.promise;
}
post_request(resource: any, params: any) {
let token = this.storage.getItem('Token');
let deferred = this.$q.defer();
this.$http({
method: 'POST',
url: resource,
headers: { 'Authorization': 'Bearer ' + token },
data: params
}).then(function success(data) {
deferred.resolve(data);
}, function error(error) {
deferred.reject(error);
});
return deferred.promise;
}
registration_requests(resource: any, params: any, addOauth: boolean, requestMethod: string, ContentType: string) {
let Oauth = addOauth ? this.authenSrvc.getOauth().access_token : "";
let ContentT: string;
let url_encode: any;
if(ContentType == "url_encoded"){
ContentT = "application/x-www-form-urlencoded";
url_encode = this.$httpParamSerializer(params);
} else {
ContentT = "application/json";
url_encode = params;
}
let deferred = this.$q.defer();
this.$http({
method: requestMethod,
url: resource,
headers: {
'Authorization':'Oauth ' + Oauth,
'Content-Type': ContentT
},
data: url_encode
}).then(function success(data) {
deferred.resolve(data);
}, function error(error) {
deferred.reject(error);
});
return deferred.promise;
}
registration_requests_Other(resource: string, params: any, requestMethod: string, ContentType: string) {
let ContentT: string;
let url_encode: any;
if(ContentType == "url_encoded"){
ContentT = "application/x-www-form-urlencoded";
url_encode = this.$httpParamSerializer(params);
} else {
ContentT = "application/json";
url_encode = params;
}
let deferred = this.$q.defer();
this.$http({
method: requestMethod,
url: resource,
headers: {
'Content-Type': ContentT
},
data: url_encode
}).then(function success(data) {
deferred.resolve(data);
}, function error(error) {
deferred.reject(error);
});
return deferred.promise;
}
delete_request(resource: string, params: any) {
var token = this.storage.getItem('Token');
var deferred = this.$q.defer();
this.$http({
method: 'DELETE',
url: resource,
headers: { 'Authorization': 'Bearer ' + token },
data: params
}).then(function success(data) {
deferred.resolve(data);
}, function error(error) {
deferred.reject(error);
});
return deferred.promise;
}
login(resource: string, params: any) {
var deferred = this.$q.defer();
var url_encode = this.$httpParamSerializer(params);
this.$http({
method: 'POST',
url: resource,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: url_encode
}).then(function success(data) {
deferred.resolve(data);
}, function error(error) {
deferred.reject(error);
});
return deferred.promise;
}
post_request_urlEncode(resource: string, params: any) {
var token = this.storage.getItem('Token');
var deferred = this.$q.defer();
var url_encode = this.$httpParamSerializer(params);
this.$http({
method: 'POST',
url: resource,
headers: { 'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + token },
data: url_encode
}).then(function success(data) {
deferred.resolve(data);
}, function error(error) {
deferred.reject(error);
});
return deferred.promise;
}
validateTokenOAuth(params: any) {
var deferred = this.$q.defer();
this.$http({
method: 'GET',
headers: { 'Content-Type': 'application/json' },
url: OAuthTokenVerificationEndPoint + '?token=' + encodeURIComponent(unescape(params.access_token))
}).then(function success(data) {
deferred.resolve(data);
}, function error(error) {
deferred.reject(error);
});
return deferred.promise;
}
get_request_params_otherResource(resource: string, params?: any) {
var deferred = this.$q.defer();
this.$http({
method: 'GET',
url: resource,
cache: false
}).then(function success(data) {
deferred.resolve(data.data);
}, function error(error) {
deferred.reject(error);
});
return deferred.promise;
}
post_request_other(resource: string, params: any) {
var deferred = this.$q.defer();
this.$http({
method: 'POST',
url: resource,
data: params
}).then(function success(data) {
deferred.resolve(data);
}, function error(error) {
deferred.reject(error);
});
return deferred.promise;
}
}
angular.module('apiSrvc', []).service('apiSrvc', ApiService);
}