2016-01-05 01:13:48 +01:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
|
|
|
(C) 2016 GNUnet e.V.
|
|
|
|
|
|
|
|
TALER is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2016-07-07 17:59:29 +02:00
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
2016-01-05 01:13:48 +01:00
|
|
|
*/
|
|
|
|
|
2016-01-05 15:42:46 +01:00
|
|
|
/**
|
|
|
|
* Helpers for doing XMLHttpRequest-s that are based on ES6 promises.
|
|
|
|
* @module Http
|
|
|
|
* @author Florian Dold
|
|
|
|
*/
|
|
|
|
|
2016-01-05 14:20:13 +01:00
|
|
|
"use strict";
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export interface HttpResponse {
|
2016-01-05 01:13:48 +01:00
|
|
|
status: number;
|
|
|
|
responseText: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-13 08:16:12 +01:00
|
|
|
export interface HttpRequestLibrary {
|
|
|
|
req(method: string,
|
2017-04-20 03:09:25 +02:00
|
|
|
url: string,
|
2016-11-13 08:16:12 +01:00
|
|
|
options?: any): Promise<HttpResponse>;
|
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
get(url: string): Promise<HttpResponse>;
|
2016-11-13 08:16:12 +01:00
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
postJson(url: string, body: any): Promise<HttpResponse>;
|
2016-11-13 08:16:12 +01:00
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
postForm(url: string, form: any): Promise<HttpResponse>;
|
2016-11-13 08:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class BrowserHttpLib {
|
2016-01-06 15:58:18 +01:00
|
|
|
req(method: string,
|
2017-04-20 03:09:25 +02:00
|
|
|
url: string,
|
2016-01-11 02:56:32 +01:00
|
|
|
options?: any): Promise<HttpResponse> {
|
2017-04-20 03:09:25 +02:00
|
|
|
return new Promise<HttpResponse>((resolve, reject) => {
|
2016-01-06 15:58:18 +01:00
|
|
|
let myRequest = new XMLHttpRequest();
|
2017-04-20 03:09:25 +02:00
|
|
|
myRequest.open(method, url);
|
2016-01-06 15:58:18 +01:00
|
|
|
if (options && options.req) {
|
|
|
|
myRequest.send(options.req);
|
|
|
|
} else {
|
|
|
|
myRequest.send();
|
2016-01-05 01:13:48 +01:00
|
|
|
}
|
2016-01-06 15:58:18 +01:00
|
|
|
myRequest.addEventListener("readystatechange", (e) => {
|
|
|
|
if (myRequest.readyState == XMLHttpRequest.DONE) {
|
|
|
|
let resp = {
|
|
|
|
status: myRequest.status,
|
|
|
|
responseText: myRequest.responseText
|
|
|
|
};
|
|
|
|
resolve(resp);
|
|
|
|
}
|
|
|
|
});
|
2016-01-05 01:13:48 +01:00
|
|
|
});
|
2016-01-06 15:58:18 +01:00
|
|
|
}
|
2016-01-05 01:13:48 +01:00
|
|
|
|
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
get(url: string) {
|
2016-01-06 15:58:18 +01:00
|
|
|
return this.req("get", url);
|
|
|
|
}
|
2016-01-05 01:13:48 +01:00
|
|
|
|
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
postJson(url: string, body: any) {
|
2016-01-06 15:58:18 +01:00
|
|
|
return this.req("post", url, {req: JSON.stringify(body)});
|
|
|
|
}
|
2016-01-05 14:20:13 +01:00
|
|
|
|
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
postForm(url: string, form: any) {
|
2016-01-06 15:58:18 +01:00
|
|
|
return this.req("post", url, {req: form});
|
|
|
|
}
|
2016-01-05 01:13:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export class RequestException {
|
2016-09-12 20:25:56 +02:00
|
|
|
constructor(detail: any) {
|
2016-01-05 01:13:48 +01:00
|
|
|
|
|
|
|
}
|
2016-03-01 19:46:20 +01:00
|
|
|
}
|