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