import { HttpRequestLibrary, makeBasicAuthHeader, readSuccessResponseJsonOrThrow } from "../http-common.js"; import { createPlatformHttpLib } from "../http.js"; import { TalerWireGatewayApi, codecForAddIncomingResponse, codecForIncomingHistory, codecForOutgoingHistory, codecForTransferResponse } from "./types.js"; import { PaginationParams, UserAndPassword, addPaginationParams } from "./utils.js"; /** * The API is used by the exchange to trigger transactions and query * incoming transactions, as well as by the auditor to query incoming * and outgoing transactions. * * https://docs.taler.net/core/api-bank-wire.html */ export class TalerWireGatewayHttpClient { httpLib: HttpRequestLibrary; constructor( private baseUrl: string, private username: string, httpClient?: HttpRequestLibrary, ) { this.httpLib = httpClient ?? createPlatformHttpLib(); } /** * https://docs.taler.net/core/api-bank-wire.html#post-$BASE_URL-transfer * */ async transfer( auth: string, body: TalerWireGatewayApi.TransferRequest, ): Promise { const url = new URL(`transfer`, this.baseUrl); const resp = await this.httpLib.fetch(url.href, { method: "POST", headers: { Authorization: makeBasicAuthHeader(this.username, auth), }, body }); return readSuccessResponseJsonOrThrow(resp, codecForTransferResponse()); } /** * https://docs.taler.net/core/api-bank-wire.html#get-$BASE_URL-history-incoming * */ async getHistoryIncoming( auth: string, pagination?: PaginationParams ): Promise { const url = new URL(`history/incoming`, this.baseUrl); addPaginationParams(url, pagination) const resp = await this.httpLib.fetch(url.href, { method: "GET", headers: { Authorization: makeBasicAuthHeader(this.username, auth), } }); return readSuccessResponseJsonOrThrow(resp, codecForIncomingHistory()); } /** * https://docs.taler.net/core/api-bank-wire.html#get-$BASE_URL-history-outgoing * */ async getHistoryOutgoing( auth: string, pagination?: PaginationParams ): Promise { const url = new URL(`history/outgoing`, this.baseUrl); addPaginationParams(url, pagination) const resp = await this.httpLib.fetch(url.href, { method: "GET", headers: { Authorization: makeBasicAuthHeader(this.username, auth), } }); return readSuccessResponseJsonOrThrow(resp, codecForOutgoingHistory()); } /** * https://docs.taler.net/core/api-bank-wire.html#post-$BASE_URL-admin-add-incoming * */ async addIncoming( auth: string, body: TalerWireGatewayApi.AddIncomingRequest, ): Promise { const url = new URL(`admin/add-incoming`, this.baseUrl); const resp = await this.httpLib.fetch(url.href, { method: "POST", headers: { Authorization: makeBasicAuthHeader(this.username, auth), }, body }); return readSuccessResponseJsonOrThrow(resp, codecForAddIncomingResponse()); } }