|
| 1 | +import fetch from "node-fetch"; |
| 2 | +import { IAssetRequest } from "./models/IAssetRequest"; |
| 3 | +import { IAssetResponse } from "./models/IAssetResponse"; |
| 4 | +import { IResponse } from "./models/IResponse"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Class to handle API communications. |
| 8 | + */ |
| 9 | +export class ApiRegistryClient { |
| 10 | + /** |
| 11 | + * The end point of the api. |
| 12 | + */ |
| 13 | + private readonly _endpoint: string; |
| 14 | + |
| 15 | + /** |
| 16 | + * The user of the api. |
| 17 | + */ |
| 18 | + private readonly _user?: string; |
| 19 | + |
| 20 | + /** |
| 21 | + * The password of the api. |
| 22 | + */ |
| 23 | + private readonly _password?: string; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a new instance of ApiClient. |
| 27 | + * @param endPoint The endpoint for the API. |
| 28 | + * @param user The user for the API. |
| 29 | + * @param password The password for the API. |
| 30 | + */ |
| 31 | + constructor(endPoint: string, user?: string, password?: string) { |
| 32 | + this._endpoint = endPoint; |
| 33 | + this._user = user; |
| 34 | + this._password = password; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Fetch Asset info. |
| 39 | + * @returns The response from the request. |
| 40 | + */ |
| 41 | + public async fetchAsset(assetID: string): Promise<IAssetResponse> { |
| 42 | + return this.sendRequest<null, IAssetResponse>( |
| 43 | + "get", "registries/test/assets/"+assetID); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Reigester Asset info. |
| 48 | + * @returns The response from the request. |
| 49 | + */ |
| 50 | + public async registerAsset(request: IAssetRequest): Promise<IAssetResponse> { |
| 51 | + return this.sendRequest<IAssetRequest, IAssetResponse>( |
| 52 | + "post", "registries/test/assets", request); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Send a request and handle errors. |
| 57 | + * @param verb The HTTP verb to make the request. |
| 58 | + * @param path The path to send the request to. |
| 59 | + * @param request The request to send. |
| 60 | + * @returns The response from the request. |
| 61 | + */ |
| 62 | + private async sendRequest<T, U extends IResponse>( |
| 63 | + verb: "put" | "post" | "get" | "delete", |
| 64 | + path: string, |
| 65 | + request?: T | undefined): Promise<U> { |
| 66 | + let response: U; |
| 67 | + |
| 68 | + try { |
| 69 | + const headers: { [id: string]: string } = {}; |
| 70 | + headers["Content-Type"] = "application/json"; |
| 71 | + |
| 72 | + if (this._user && this._password) { |
| 73 | + headers.Authorization = `Basic ${btoa(`${this._user}:${this._password}`)}`; |
| 74 | + } |
| 75 | + |
| 76 | + let fetchResponse; |
| 77 | + |
| 78 | + if (verb === "get") { |
| 79 | + fetchResponse = await fetch( |
| 80 | + `${this._endpoint}/${path}`, |
| 81 | + { |
| 82 | + method: "get", |
| 83 | + headers |
| 84 | + } |
| 85 | + ); |
| 86 | + } else if (verb === "post") { |
| 87 | + fetchResponse = await fetch( |
| 88 | + `${this._endpoint}/${path}`, |
| 89 | + { |
| 90 | + method: "post", |
| 91 | + headers, |
| 92 | + body: JSON.stringify(request) |
| 93 | + } |
| 94 | + ); |
| 95 | + } else if (verb === "put") { |
| 96 | + fetchResponse = await fetch( |
| 97 | + `${this._endpoint}/${path}`, |
| 98 | + { |
| 99 | + method: "put", |
| 100 | + headers, |
| 101 | + body: JSON.stringify(request) |
| 102 | + } |
| 103 | + ); |
| 104 | + } else if (verb === "delete") { |
| 105 | + fetchResponse = await fetch( |
| 106 | + `${this._endpoint}/${path}`, |
| 107 | + { |
| 108 | + method: "delete", |
| 109 | + headers |
| 110 | + } |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + if (!fetchResponse) { |
| 115 | + throw new Error("No data was returned from the API"); |
| 116 | + } else { |
| 117 | + try { |
| 118 | + response = await fetchResponse.json(); |
| 119 | + } catch (err) { |
| 120 | + const text = await fetchResponse.text(); |
| 121 | + throw new Error(err.message + " --- " + text); |
| 122 | + } |
| 123 | + if (!fetchResponse.ok) { |
| 124 | + if (response.error) { |
| 125 | + throw new Error(response.error); |
| 126 | + } else { |
| 127 | + const isComError = fetchResponse.status >= 500; |
| 128 | + let msg = fetchResponse.statusText; |
| 129 | + |
| 130 | + if (msg === "Network Error") { |
| 131 | + msg = "There is a problem communicating with the network"; |
| 132 | + } |
| 133 | + |
| 134 | + if (!msg.endsWith(".")) { |
| 135 | + msg += "."; |
| 136 | + } |
| 137 | + |
| 138 | + if (isComError) { |
| 139 | + msg += "\n\nPlease try again later."; |
| 140 | + } |
| 141 | + |
| 142 | + throw new Error(msg); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } catch (err) { |
| 147 | + throw new Error(`The application is not able to complete the request, due to the following error:\n\n${err.message}`); |
| 148 | + } |
| 149 | + |
| 150 | + return response; |
| 151 | + } |
| 152 | +} |
0 commit comments