From 5543dbbf39859be586e59a40861b30accdf78b9b Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Mon, 20 Jul 2020 21:15:30 +0200 Subject: [PATCH] Started implemention of ApiClient using the Node.js HTTP API --- .../src/main/typescript/ApiClient.ts | 101 +++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/ccm-apiclient-commons/src/main/typescript/ApiClient.ts b/ccm-apiclient-commons/src/main/typescript/ApiClient.ts index 3e041203a..7c89c1abc 100644 --- a/ccm-apiclient-commons/src/main/typescript/ApiClient.ts +++ b/ccm-apiclient-commons/src/main/typescript/ApiClient.ts @@ -1,3 +1,5 @@ +import * as http from "http"; + /** * The interface of a client for the LibreCcm RESTful API. */ @@ -373,7 +375,10 @@ export class ApiClientFetchImpl implements LibreCcmApiClient { async options(endpoint: string): Promise { const url = buildUrl(this.#baseUrl, endpoint); try { - const response = await fetch(url, this.buildFetchOptions("options")); + const response = await fetch( + url, + this.buildFetchOptions("options") + ); if (response.ok) { return new FetchResponse(response); } else { @@ -397,16 +402,108 @@ export class ApiClientFetchImpl implements LibreCcmApiClient { } } +/** + * An implementation of the {@link ApiResponse} for the Node.js HTTP API supported + * by browsers. + */ +class NodeResponse implements ApiResponse { + readonly status: number; + readonly statusText: string; + + #data: Buffer; + + constructor(status: number, statusText: string, data: Buffer) { + this.status = status; + this.statusText = statusText; + this.#data = data; + } + + json(): Promise> { + return new Promise((resolve, reject) => { + try { + resolve(this.#data.toJSON()); + } catch (err) { + reject(err); + } + }); + } + + arrayBuffer(): Promise { + return new Promise((resolve, reject) => { + try { + const arrayBuf: ArrayBuffer = this.#data.buffer.slice( + this.#data.byteOffset, + this.#data.byteLength + this.#data.byteLength + ); + resolve(arrayBuf); + } catch (err) { + reject(err); + } + }); + } + + text(): Promise { + return new Promise((resolve, reject) => { + try { + return this.#data.toString(); + } catch (err) { + reject(err); + } + }); + } +} + /** * An implementation of the {@link LibreCcmApiClient} interface using the HTTP API * provided by node.js. */ export class ApiClientNodeImpl implements LibreCcmApiClient { + readonly #baseUrl: string; + + constructor(baseUrl: string) { + this.#baseUrl = baseUrl; + } + get( endpoint: string, searchParams?: Record ): Promise { - throw "Not implemented yet."; + return new Promise((resolve, reject) => { + const url = buildUrl(this.#baseUrl, endpoint, searchParams); + const request: http.ClientRequest = http.request(url, { + headers: { + Authorization: "", + }, + method: "GET", + }); + + request.on("response", (response) => { + const status: number = response.statusCode + ? response.statusCode + : -1; + const statusText: string = response.statusMessage + ? response.statusMessage + : ""; + + const data: Array = []; + + response.on("data", (chunk: Buffer) => { + data.push(chunk); + }); + response.on("end", () => { + const buffer: Buffer = Buffer.concat(data); + const apiResponse: ApiResponse = new NodeResponse( + status, + statusText, + buffer + ); + resolve(apiResponse); + }); + }); + request.on("error", (error) => reject(error)); + + request.end(); + }); } post(