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
|
|
|
|
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
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-01-10 20:07:42 +01:00
|
|
|
export class BrowserHttpLib {
|
2016-01-06 15:58:18 +01:00
|
|
|
req(method: string,
|
2016-01-11 02:56:32 +01:00
|
|
|
url: string|uri.URI,
|
|
|
|
options?: any): Promise<HttpResponse> {
|
2016-01-06 15:58:18 +01:00
|
|
|
let urlString: string;
|
|
|
|
if (url instanceof URI) {
|
|
|
|
urlString = url.href();
|
|
|
|
} else if (typeof url === "string") {
|
|
|
|
urlString = url;
|
2016-01-05 01:13:48 +01:00
|
|
|
}
|
2016-01-06 15:58:18 +01:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let myRequest = new XMLHttpRequest();
|
|
|
|
myRequest.open(method, urlString);
|
|
|
|
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
|
|
|
|
|
|
|
|
2016-01-06 15:58:18 +01:00
|
|
|
get(url: string|uri.URI) {
|
|
|
|
return this.req("get", url);
|
|
|
|
}
|
2016-01-05 01:13:48 +01:00
|
|
|
|
|
|
|
|
2016-01-06 15:58:18 +01:00
|
|
|
postJson(url: string|uri.URI, body) {
|
|
|
|
return this.req("post", url, {req: JSON.stringify(body)});
|
|
|
|
}
|
2016-01-05 14:20:13 +01:00
|
|
|
|
|
|
|
|
2016-01-06 15:58:18 +01:00
|
|
|
postForm(url: string|uri.URI, form) {
|
|
|
|
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-01-05 01:13:48 +01:00
|
|
|
constructor(detail) {
|
|
|
|
|
|
|
|
}
|
2016-03-01 19:46:20 +01:00
|
|
|
}
|