Skip to content

Commit

Permalink
fix #4
Browse files Browse the repository at this point in the history
  • Loading branch information
gfoo committed Mar 27, 2018
1 parent 6764601 commit 9270d28
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h5>Query</h5>
</div>
<div class="card-body">

<form #knarqlmeForm="ngForm" (ngSubmit)="onSubmit()">
<form #knarqlmeForm="ngForm" (ngSubmit)="onLaunch()">

<div class="form-group">
<input [disabled]="isLaunched" type="url" class="form-control"
Expand All @@ -22,7 +22,7 @@ <h5>Query</h5>
</div>
</div>

<form #loginForm="ngForm" (ngSubmit)="onLogin()" class="form-inline">
<form #loginForm="ngForm" (ngSubmit)="connection()" class="form-inline">
<div class="form-group">
<label for="login">Login:</label>
<input [disabled]="isLaunched || token || isLogLaunched"
Expand Down
165 changes: 112 additions & 53 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,7 @@ CONSTRUCT {

constructor(private http: HttpClient) {}

onSubmit() {
let httpOptions = {};
if (this.token) {
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization: 'Bearer ' + this.token
})
};
}

console.log(this.httpMethod);

getMethod(headers: HttpHeaders) {
this.jsonResult = null;
this.rawResult = null;
this.isLaunched = true;
Expand All @@ -64,7 +52,9 @@ CONSTRUCT {
this.endpoint +
(!this.endpoint.endsWith('/') ? '/' : '') +
encodeURIComponent(this.knarql),
httpOptions
{
headers: headers
}
)
//.delay(3000)
.timeout(30000)
Expand All @@ -83,49 +73,118 @@ CONSTRUCT {
);
}

onLogin() {
// const url = new URL(this.endpoint);
postMethod(headers: HttpHeaders, payload) {
this.jsonResult = null;
this.rawResult = null;
this.isLaunched = true;

console.log(headers);

this.http
.post(this.endpoint, payload, {
headers: headers
})
//.delay(3000)
.timeout(30000)
.subscribe(
res => {
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);
}
}
}

0 comments on commit 9270d28

Please sign in to comment.