diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/http.ts | 115 | ||||
-rw-r--r-- | src/util/taleruri.ts | 11 |
2 files changed, 116 insertions, 10 deletions
diff --git a/src/util/http.ts b/src/util/http.ts index 0ac989a17..bc054096a 100644 --- a/src/util/http.ts +++ b/src/util/http.ts @@ -14,6 +14,9 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +import { Codec } from "./codec"; +import { OperationFailedError } from "../operations/errors"; + /** * Helpers for doing XMLHttpRequest-s that are based on ES6 promises. * Allows for easy mocking for test cases. @@ -172,3 +175,115 @@ export class BrowserHttpLib implements HttpRequestLibrary { // Nothing to do } } + +export interface PostJsonRequest<RespType> { + http: HttpRequestLibrary; + url: string; + body: any; + codec: Codec<RespType>; +} + +/** + * Helper for making Taler-style HTTP POST requests with a JSON payload and response. + */ +export async function httpPostTalerJson<RespType>( + req: PostJsonRequest<RespType>, +): Promise<RespType> { + const resp = await req.http.postJson(req.url, req.body); + + if (resp.status !== 200) { + let exc: OperationFailedError | undefined = undefined; + try { + const errorJson = await resp.json(); + const m = `received error response (status ${resp.status})`; + exc = new OperationFailedError({ + type: "protocol", + message: m, + details: { + httpStatusCode: resp.status, + errorResponse: errorJson, + }, + }); + } catch (e) { + const m = "could not parse response JSON"; + exc = new OperationFailedError({ + type: "network", + message: m, + details: { + status: resp.status, + }, + }); + } + throw exc; + } + let json: any; + try { + json = await resp.json(); + } catch (e) { + const m = "could not parse response JSON"; + throw new OperationFailedError({ + type: "network", + message: m, + details: { + status: resp.status, + }, + }); + } + return req.codec.decode(json); +} + + +export interface GetJsonRequest<RespType> { + http: HttpRequestLibrary; + url: string; + codec: Codec<RespType>; +} + +/** + * Helper for making Taler-style HTTP GET requests with a JSON payload. + */ +export async function httpGetTalerJson<RespType>( + req: GetJsonRequest<RespType>, +): Promise<RespType> { + const resp = await req.http.get(req.url); + + if (resp.status !== 200) { + let exc: OperationFailedError | undefined = undefined; + try { + const errorJson = await resp.json(); + const m = `received error response (status ${resp.status})`; + exc = new OperationFailedError({ + type: "protocol", + message: m, + details: { + httpStatusCode: resp.status, + errorResponse: errorJson, + }, + }); + } catch (e) { + const m = "could not parse response JSON"; + exc = new OperationFailedError({ + type: "network", + message: m, + details: { + status: resp.status, + }, + }); + } + throw exc; + } + let json: any; + try { + json = await resp.json(); + } catch (e) { + const m = "could not parse response JSON"; + throw new OperationFailedError({ + type: "network", + message: m, + details: { + status: resp.status, + }, + }); + } + return req.codec.decode(json); +} diff --git a/src/util/taleruri.ts b/src/util/taleruri.ts index 2eaea2846..30209d48a 100644 --- a/src/util/taleruri.ts +++ b/src/util/taleruri.ts @@ -97,15 +97,6 @@ export function classifyTalerUri(s: string): TalerUriType { return TalerUriType.Unknown; } -export function getOrderDownloadUrl( - merchantBaseUrl: string, - orderId: string, -): string { - const u = new URL("proposal", merchantBaseUrl); - u.searchParams.set("order_id", orderId); - return u.href; -} - export function parsePayUri(s: string): PayUriResult | undefined { const pfx = "taler://pay/"; if (!s.toLowerCase().startsWith(pfx)) { @@ -133,7 +124,7 @@ export function parsePayUri(s: string): PayUriResult | undefined { } if (maybePath === "-") { - maybePath = "public/"; + maybePath = ""; } else { maybePath = decodeURIComponent(maybePath) + "/"; } |