diff options
Diffstat (limited to 'packages/taler-util/src/http-client/bank-wire.ts')
-rw-r--r-- | packages/taler-util/src/http-client/bank-wire.ts | 57 |
1 files changed, 40 insertions, 17 deletions
diff --git a/packages/taler-util/src/http-client/bank-wire.ts b/packages/taler-util/src/http-client/bank-wire.ts index 0a032cc61..af0857ac5 100644 --- a/packages/taler-util/src/http-client/bank-wire.ts +++ b/packages/taler-util/src/http-client/bank-wire.ts @@ -1,5 +1,7 @@ import { HttpRequestLibrary, makeBasicAuthHeader, readSuccessResponseJsonOrThrow } from "../http-common.js"; +import { HttpStatusCode } from "../http-status-codes.js"; import { createPlatformHttpLib } from "../http.js"; +import { opEmptySuccess, opFixedSuccess, opKnownFailure, opSuccess, opUnknownFailure } from "../operation.js"; import { PaginationParams, TalerWireGatewayApi, codecForAddIncomingResponse, codecForIncomingHistory, codecForOutgoingHistory, codecForTransferResponse } from "./types.js"; import { addPaginationParams } from "./utils.js"; @@ -28,7 +30,7 @@ export class TalerWireGatewayHttpClient { async transfer( auth: string, body: TalerWireGatewayApi.TransferRequest, - ): Promise<TalerWireGatewayApi.TransferResponse> { + ) { const url = new URL(`transfer`, this.baseUrl); const resp = await this.httpLib.fetch(url.href, { method: "POST", @@ -37,17 +39,21 @@ export class TalerWireGatewayHttpClient { }, body }); - return readSuccessResponseJsonOrThrow(resp, codecForTransferResponse()); + switch (resp.status) { + case HttpStatusCode.Ok: return opSuccess(resp, codecForTransferResponse()) + case HttpStatusCode.BadRequest: return opKnownFailure("invalid-input", resp); + case HttpStatusCode.Unauthorized: return opKnownFailure("unauthorized", resp); + case HttpStatusCode.NotFound: return opKnownFailure("not-found", resp); + case HttpStatusCode.Conflict: return opKnownFailure("request-uid-already-used", resp); + default: return opUnknownFailure(resp, await resp.text()) + } } /** * https://docs.taler.net/core/api-bank-wire.html#get-$BASE_URL-history-incoming * */ - async getHistoryIncoming( - auth: string, - pagination?: PaginationParams - ): Promise<TalerWireGatewayApi.IncomingHistory> { + async getHistoryIncoming(auth: string, pagination?: PaginationParams) { const url = new URL(`history/incoming`, this.baseUrl); addPaginationParams(url, pagination) const resp = await this.httpLib.fetch(url.href, { @@ -56,16 +62,21 @@ export class TalerWireGatewayHttpClient { Authorization: makeBasicAuthHeader(this.username, auth), } }); - return readSuccessResponseJsonOrThrow(resp, codecForIncomingHistory()); + switch (resp.status) { + case HttpStatusCode.Ok: return opSuccess(resp, codecForIncomingHistory()) + case HttpStatusCode.NoContent: return opFixedSuccess({ incoming_transactions: [], credit_account: "" }) + case HttpStatusCode.BadRequest: return opKnownFailure("invalid-input", resp); + case HttpStatusCode.Unauthorized: return opKnownFailure("unauthorized", resp); + case HttpStatusCode.NotFound: return opKnownFailure("not-found", resp); + default: return opUnknownFailure(resp, await resp.text()) + } + // 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<TalerWireGatewayApi.OutgoingHistory> { + async getHistoryOutgoing(auth: string, pagination?: PaginationParams) { const url = new URL(`history/outgoing`, this.baseUrl); addPaginationParams(url, pagination) const resp = await this.httpLib.fetch(url.href, { @@ -74,16 +85,20 @@ export class TalerWireGatewayHttpClient { Authorization: makeBasicAuthHeader(this.username, auth), } }); - return readSuccessResponseJsonOrThrow(resp, codecForOutgoingHistory()); + switch (resp.status) { + case HttpStatusCode.Ok: return opSuccess(resp, codecForOutgoingHistory()) + case HttpStatusCode.NoContent: return opFixedSuccess({ outgoing_transactions: [], debit_account: "" }) + case HttpStatusCode.BadRequest: return opKnownFailure("invalid-input", resp); + case HttpStatusCode.Unauthorized: return opKnownFailure("unauthorized", resp); + case HttpStatusCode.NotFound: return opKnownFailure("not-found", resp); + default: return opUnknownFailure(resp, await resp.text()) + } } /** * https://docs.taler.net/core/api-bank-wire.html#post-$BASE_URL-admin-add-incoming * */ - async addIncoming( - auth: string, - body: TalerWireGatewayApi.AddIncomingRequest, - ): Promise<TalerWireGatewayApi.AddIncomingResponse> { + async addIncoming(auth: string, body: TalerWireGatewayApi.AddIncomingRequest,) { const url = new URL(`admin/add-incoming`, this.baseUrl); const resp = await this.httpLib.fetch(url.href, { method: "POST", @@ -92,7 +107,15 @@ export class TalerWireGatewayHttpClient { }, body }); - return readSuccessResponseJsonOrThrow(resp, codecForAddIncomingResponse()); + switch (resp.status) { + case HttpStatusCode.Ok: return opSuccess(resp, codecForAddIncomingResponse()) + case HttpStatusCode.NoContent: return opFixedSuccess({ outgoing_transactions: [], debit_account: "" }) + case HttpStatusCode.BadRequest: return opKnownFailure("invalid-input", resp); + case HttpStatusCode.Unauthorized: return opKnownFailure("unauthorized", resp); + case HttpStatusCode.NotFound: return opKnownFailure("not-found", resp); + case HttpStatusCode.Conflict: return opKnownFailure("reserve-id-already-used", resp); + default: return opUnknownFailure(resp, await resp.text()) + } } } |