From 4f99b27524518dab63fe834205b2b2645dc04a3c Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Fri, 15 Apr 2016 11:58:26 -0400 Subject: [PATCH 01/30] Add spot to inject service-level and global headers. --- front/current/services/rest.service.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/front/current/services/rest.service.ts b/front/current/services/rest.service.ts index 68db133..d8d3a06 100644 --- a/front/current/services/rest.service.ts +++ b/front/current/services/rest.service.ts @@ -6,6 +6,7 @@ import { config } from '../config'; import { extend } from '../lib/object'; import { param } from '../lib/url'; + export class Model { constructor( public service:any, @@ -32,7 +33,10 @@ export class Model { return this; }; - get url(): string { + get headers():any { + return config.HEADERS; + }; + get url():string { return ( this.service.config.url + this.service.url + @@ -52,7 +56,7 @@ export class Model { let self = this; self.loading = true; self.error = undefined; - return (self._http.get(self.url, {headers: config.HEADERS}) + return (self._http.get(self.url, {headers: self.headers}) .do(self.parse.bind(self), self.setError.bind(self), () => { self.loading = false; }) .subscribe(observer)); }; @@ -66,14 +70,14 @@ export class Model { self._http[method]( self.url, JSON.stringify(self.data), - {headers: config.HEADERS} + {headers: self.headers} ) .do(self.parse.bind(self), self.setError.bind(self), () => { self.loading = false; }) .subscribe(observer)); }; destroy():Observable { // Delete a model on the server. - return this._http.delete(this.url, {headers: config.HEADERS}); + return this._http.delete(this.url, {headers: this.headers}); }; }; @@ -111,7 +115,10 @@ export class Collection { return this; }; - public get url(): string { + get headers():any { + return config.HEADERS; + }; + get url():string { return this.service.config.url + this.service.url + param(this.filters); }; @@ -127,7 +134,7 @@ export class Collection { let self = this; self.loading = true; self.error = undefined; - return (self._http.get(self.url, {headers: config.HEADERS}) + return (self._http.get(self.url, {headers: self.headers}) .do(self.parse.bind(self), self.setError.bind(self), () => { self.loading = false; }) .subscribe(observer)); }; -- GitLab From a4fe0598c10fecdbdf240507ec67ea313c90f50c Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Fri, 15 Apr 2016 13:04:45 -0400 Subject: [PATCH 02/30] Refactor config to allow monkey-patching in new headers. --- config/dev.config.ts | 6 ++---- config/production.config.ts | 6 ++---- config/staging.config.ts | 6 ++---- front/current/services/rest.service.ts | 10 +++++++--- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/config/dev.config.ts b/config/dev.config.ts index 781445d..6aa061a 100644 --- a/config/dev.config.ts +++ b/config/dev.config.ts @@ -1,13 +1,11 @@ -import { Headers } from 'angular2/http'; - export class config { public static SERVICES:any = { "user": {"url": "http://127.0.0.1:5600"}, "project": {"url": "http://127.0.0.1:5310"}, "document": {"url": "http://127.0.0.1:5320"} }; - public static HEADERS = new Headers({ + public static HEADERS:any = { 'x-blocpower-app-key': '$APP_KEY' - }); + }; public static static_url:string = '/current'; }; diff --git a/config/production.config.ts b/config/production.config.ts index 9e99a36..87748e9 100644 --- a/config/production.config.ts +++ b/config/production.config.ts @@ -1,13 +1,11 @@ -import { Headers } from 'angular2/http'; - export class config { public static SERVICES:any = { "user": {"url": "http://user.s.blocpower.us"}, "project": {"url": "http://project.s.blocpower.us"}, "document": {"url": "http://document.s.blocpower.us"} }; - public static HEADERS = new Headers({ + public static HEADERS = { 'x-blocpower-app-key': '$APP_KEY' - }); + }; public static static_url:string = '$CURRENT'; }; diff --git a/config/staging.config.ts b/config/staging.config.ts index d8a54a4..0754322 100644 --- a/config/staging.config.ts +++ b/config/staging.config.ts @@ -1,13 +1,11 @@ -import { Headers } from 'angular2/http'; - export class config { public static SERVICES:any = { "user": {"url": "http://staging.user.s.blocpower.us"}, "project": {"url": "http://staging.project.s.blocpower.us"}, "document": {"url": "http://staging.document.s.blocpower.us"} }; - public static HEADERS = new Headers({ + public static HEADERS = { 'x-blocpower-app-key': '$APP_KEY' - }); + }; public static static_url:string = '$CURRENT'; }; diff --git a/front/current/services/rest.service.ts b/front/current/services/rest.service.ts index d8d3a06..5cd9dfe 100644 --- a/front/current/services/rest.service.ts +++ b/front/current/services/rest.service.ts @@ -1,5 +1,5 @@ import { Injectable } from 'angular2/core'; -import { Http, Response, URLSearchParams } from 'angular2/http'; +import { Http, Response, Headers } from 'angular2/http'; import { Observable } from 'rxjs/Observable'; import { config } from '../config'; @@ -34,7 +34,8 @@ export class Model { }; get headers():any { - return config.HEADERS; + let headers = extend({}, config.HEADERS); + return new Headers(headers); }; get url():string { return ( @@ -45,6 +46,7 @@ export class Model { }; parse(response:Response):Model { + // Update the model and metadata from the response. return this.set(response.json().data); }; setError(response:Response) { @@ -116,13 +118,15 @@ export class Collection { }; get headers():any { - return config.HEADERS; + let headers = extend({}, config.HEADERS); + return new Headers(headers); }; get url():string { return this.service.config.url + this.service.url + param(this.filters); }; parse(response:Response):Collection { + // Update the collection and metadata from the response. return this.set(response.json().data); }; setError(response:Response) { -- GitLab From becd1cb11839133cf4b39af7c90003d65057efaf Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Fri, 15 Apr 2016 14:15:52 -0400 Subject: [PATCH 03/30] Add a wrapper for local storage. --- front/current/lib/cache.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 front/current/lib/cache.ts diff --git a/front/current/lib/cache.ts b/front/current/lib/cache.ts new file mode 100644 index 0000000..9398b17 --- /dev/null +++ b/front/current/lib/cache.ts @@ -0,0 +1,27 @@ +import { extend } from './object'; + + +class Cache { + // A wrapper around local storage. + public get(key:string):any { + return localStorage.getItem(key); + }; + public set(key:string, value:any):any { + return localStorage.setItem(key, value); + }; + + public getObject(key:string):any { + // JSON.parse's an object from local storage. + return JSON.parse(this.get(key)) || {}; + }; + public setObject(key:string, value:any):any { + // JSON.stringify's an object into local storage. + return this.set(key, JSON.stringify(value)); + }; + public updateObject(key:string, value:any):any { + // Update an object in local storage. + let object = this.getObject(key) || {}; + return this.setObject(key, extend(object, value)); + }; +}; +export default new Cache(); -- GitLab From 3f1a7a1b3c6df23a0e529d993a8fae619a523a11 Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Fri, 15 Apr 2016 14:16:10 -0400 Subject: [PATCH 04/30] Send the app token on subsequent requests to the same service. --- .../services/document/document.service.ts | 1 + .../services/project/document-slot.service.ts | 1 + .../services/project/project.service.ts | 1 + front/current/services/rest.service.ts | 25 ++++++++++++++++++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/front/current/services/document/document.service.ts b/front/current/services/document/document.service.ts index 3850b18..524591c 100644 --- a/front/current/services/document/document.service.ts +++ b/front/current/services/document/document.service.ts @@ -3,6 +3,7 @@ import { config } from '../../config'; @Injectable() export class DocumentService { + public service_name:string = 'document'; public config:any = config.SERVICES.document; public url:string = '/document/'; }; diff --git a/front/current/services/project/document-slot.service.ts b/front/current/services/project/document-slot.service.ts index 8f0960f..960d1f5 100644 --- a/front/current/services/project/document-slot.service.ts +++ b/front/current/services/project/document-slot.service.ts @@ -3,6 +3,7 @@ import { config } from '../../config'; @Injectable() export class DocumentSlotService { + public service_name:string = 'project'; public config:any = config.SERVICES.project; public url:string = '/project/document/'; }; diff --git a/front/current/services/project/project.service.ts b/front/current/services/project/project.service.ts index d41e5b7..66586fb 100644 --- a/front/current/services/project/project.service.ts +++ b/front/current/services/project/project.service.ts @@ -3,6 +3,7 @@ import { config } from '../../config'; @Injectable() export class ProjectService { + public service_name:string = 'project'; public config:any = config.SERVICES.project; public url:string = '/project/'; }; diff --git a/front/current/services/rest.service.ts b/front/current/services/rest.service.ts index 5cd9dfe..1ceb042 100644 --- a/front/current/services/rest.service.ts +++ b/front/current/services/rest.service.ts @@ -4,6 +4,7 @@ import { Observable } from 'rxjs/Observable'; import { config } from '../config'; import { extend } from '../lib/object'; +import cache from '../lib/cache'; import { param } from '../lib/url'; @@ -35,6 +36,11 @@ export class Model { get headers():any { let headers = extend({}, config.HEADERS); + + let token = cache.getObject('app_tokens')[this.service.service_name]; + if (token) + headers['x-blocpower-app-token'] = token; + return new Headers(headers); }; get url():string { @@ -47,6 +53,12 @@ export class Model { parse(response:Response):Model { // Update the model and metadata from the response. + + // Set the app token in the cache. + let app_tokens = cache.getObject('app_tokens'); + app_tokens[this.service.service_name] = response.headers.get('x-blocpower-app-token'); + cache.setObject('app_tokens', app_tokens); + return this.set(response.json().data); }; setError(response:Response) { @@ -119,6 +131,11 @@ export class Collection { get headers():any { let headers = extend({}, config.HEADERS); + + let token = cache.getObject('app_tokens')[this.service.service_name]; + if (token) + headers['x-blocpower-app-token'] = token; + return new Headers(headers); }; get url():string { @@ -126,7 +143,13 @@ export class Collection { }; parse(response:Response):Collection { - // Update the collection and metadata from the response. + // Update the model and metadata from the response. + + // Set the app token in the cache. + let app_tokens = cache.getObject('app_tokens'); + app_tokens[this.service.service_name] = response.headers.get('x-blocpower-app-token'); + cache.setObject('app_tokens', app_tokens); + return this.set(response.json().data); }; setError(response:Response) { -- GitLab From a3758972563a6ba58a4d4080f15a9fd80169a868 Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Fri, 15 Apr 2016 16:42:55 -0400 Subject: [PATCH 05/30] Add googleapis dependency. --- front/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/front/package.json b/front/package.json index 21cb01b..9129149 100644 --- a/front/package.json +++ b/front/package.json @@ -16,6 +16,7 @@ "config-json": "^1.0.0", "es6-promise": "^3.0.2", "es6-shim": "^0.33.3", + "googleapis": "^5.0.0", "node-sass": "^3.4.2", "reflect-metadata": "0.1.2", "rxjs": "5.0.0-beta.2", -- GitLab From 465e6235e94a95d9ba303c2911cc8a669545a2de Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Mon, 25 Apr 2016 12:27:35 -0400 Subject: [PATCH 06/30] Add google platform API to the index. --- front/index.default.html | 2 ++ 1 file changed, 2 insertions(+) diff --git a/front/index.default.html b/front/index.default.html index a5c6978..9280e52 100644 --- a/front/index.default.html +++ b/front/index.default.html @@ -21,6 +21,8 @@ + +