aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util/src/http-client/bank-wire.ts
blob: 9f2b859ed43efc5c20be34a3fde4bbcb8fa8e726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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<TalerWireGatewayApi.TransferResponse> {
    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<TalerWireGatewayApi.IncomingHistory> {
    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<TalerWireGatewayApi.OutgoingHistory> {
    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<TalerWireGatewayApi.AddIncomingResponse> {
    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());
  }
}