Post impl for Node based API client
parent
5543dbbf39
commit
d90dd3eafa
|
|
@ -1,4 +1,6 @@
|
|||
import * as http from "http";
|
||||
import { rejects } from "assert";
|
||||
import { timeStamp } from "console";
|
||||
|
||||
/**
|
||||
* The interface of a client for the LibreCcm RESTful API.
|
||||
|
|
@ -410,9 +412,9 @@ class NodeResponse implements ApiResponse {
|
|||
readonly status: number;
|
||||
readonly statusText: string;
|
||||
|
||||
#data: Buffer;
|
||||
#data: Buffer | null;
|
||||
|
||||
constructor(status: number, statusText: string, data: Buffer) {
|
||||
constructor(status: number, statusText: string, data: Buffer | null) {
|
||||
this.status = status;
|
||||
this.statusText = statusText;
|
||||
this.#data = data;
|
||||
|
|
@ -421,7 +423,11 @@ class NodeResponse implements ApiResponse {
|
|||
json(): Promise<Record<string, unknown>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
resolve(this.#data.toJSON());
|
||||
if (this.#data) {
|
||||
resolve(this.#data.toJSON());
|
||||
} else {
|
||||
resolve({});
|
||||
}
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
|
@ -431,11 +437,15 @@ class NodeResponse implements ApiResponse {
|
|||
arrayBuffer(): Promise<ArrayBuffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const arrayBuf: ArrayBuffer = this.#data.buffer.slice(
|
||||
this.#data.byteOffset,
|
||||
this.#data.byteLength + this.#data.byteLength
|
||||
);
|
||||
resolve(arrayBuf);
|
||||
if (this.#data) {
|
||||
const arrayBuf: ArrayBuffer = this.#data.buffer.slice(
|
||||
this.#data.byteOffset,
|
||||
this.#data.byteLength + this.#data.byteLength
|
||||
);
|
||||
resolve(arrayBuf);
|
||||
} else {
|
||||
resolve(new ArrayBuffer(0));
|
||||
}
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
|
@ -445,7 +455,11 @@ class NodeResponse implements ApiResponse {
|
|||
text(): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
return this.#data.toString();
|
||||
if (this.#data) {
|
||||
resolve(this.#data.toString());
|
||||
} else {
|
||||
resolve("");
|
||||
}
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
|
@ -511,7 +525,37 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
|
|||
body: unknown,
|
||||
searchParams?: Record<string, string>
|
||||
): Promise<ApiResponse> {
|
||||
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: "POST",
|
||||
});
|
||||
|
||||
request.on("response", (response) => {
|
||||
const status: number = response.statusCode
|
||||
? response.statusCode
|
||||
: -1;
|
||||
const statusText: string = response.statusMessage
|
||||
? response.statusMessage
|
||||
: "";
|
||||
|
||||
response.on("end", () => {
|
||||
const apiResponse: ApiResponse = new NodeResponse(
|
||||
status,
|
||||
statusText,
|
||||
null
|
||||
);
|
||||
resolve(apiResponse);
|
||||
});
|
||||
});
|
||||
|
||||
request.on("error", (error) => reject(error));
|
||||
|
||||
request.end();
|
||||
});
|
||||
}
|
||||
|
||||
put(
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue