From 9270d286b0f3fd32732eab4e87f6c0d52bc2f3fb Mon Sep 17 00:00:00 2001 From: gfoo Date: Tue, 27 Mar 2018 23:56:30 +0200 Subject: [PATCH] fix #4 --- src/app/app.component.html | 4 +- src/app/app.component.ts | 165 +++++++++++++++++++++++++------------ 2 files changed, 114 insertions(+), 55 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index c294f65..0127572 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -11,7 +11,7 @@
Query
-
+
Query
- +
{ + this.jsonResult = res; + this.rawResult = JSON.stringify(res, null, 2); + this.isLaunched = false; + }, + error => { + this.jsonResult = error; + this.rawResult = JSON.stringify(error, null, 2); + this.isLaunched = false; + console.log(error); + } + ); + } + + postJsonMethod(headers: HttpHeaders) { + headers = headers.set('Content-Type', 'application/json'); + this.postMethod(headers, { query: this.knarql }); + } + + postSparqlMethod(headers: HttpHeaders) { + headers = headers.set('Content-Type', 'application/knarql-query'); + console.log(headers); + this.postMethod(headers, this.knarql); + } + + onLaunch() { + let headers = new HttpHeaders({}); + if (this.token) { + headers = headers.set('Authorization', 'Bearer ' + this.token); + } + + switch (this.httpMethod) { + case 'GET': { + this.getMethod(headers); + break; + } + case 'POST json': { + this.postJsonMethod(headers); + break; + } + case 'POST sparql-query': { + this.postSparqlMethod(headers); + break; + } + } + } + + signout(authURL) { + this.isLogLaunched = true; + this.http + .delete(authURL, { + headers: new HttpHeaders({ + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + this.token + }) + }) + .subscribe( + res => { + this.token = null; + this.isLogLaunched = false; + }, + error => { + this.token = null; + this.isLogLaunched = false; + window.alert(error.message); + console.log(error); + } + ); + } + + signin(authURL) { + this.isLogLaunched = true; + this.http + .post(authURL, { + email: this.login, + password: this.password + }) + .subscribe( + (res: any) => { + this.token = res.token; + this.isLogLaunched = false; + }, + error => { + this.token = null; + this.isLogLaunched = false; + window.alert(error.message); + console.log(error); + } + ); + } + + connection() { const url = parseURL(this.endpoint); const authURL = url.protocol + '//' + url.host + '/' + 'v2/authentication'; - this.isLogLaunched = true; if (this.token) { - this.http - .delete(authURL, { - headers: new HttpHeaders({ - 'Content-Type': 'application/json', - Authorization: 'Bearer ' + this.token - }) - }) - .subscribe( - res => { - this.token = null; - this.isLogLaunched = false; - }, - error => { - this.token = null; - this.isLogLaunched = false; - window.alert(error.message); - console.log(error); - } - ); + this.signout(authURL); } else { - this.http - .post(authURL, { - email: this.login, - password: this.password - }) - .subscribe( - (res: any) => { - this.token = res.token; - this.isLogLaunched = false; - }, - error => { - this.token = null; - this.isLogLaunched = false; - window.alert(error.message); - console.log(error); - } - ); + this.signin(authURL); } } }