Skip to content

Commit ff9d634

Browse files
committed
Switch HTTP methods to uppercase
Just for consistency... lowercase wasn't helping anything
1 parent a2aea74 commit ff9d634

File tree

6 files changed

+44
-34
lines changed

6 files changed

+44
-34
lines changed

common.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// The API contract that all generated code expects
22

33
export type HttpMethods =
4-
| "get"
5-
| "post"
6-
| "delete"
7-
| "put"
8-
| "patch"
9-
| "options"
10-
| "head";
4+
| "GET"
5+
| "POST"
6+
| "DELETE"
7+
| "PUT"
8+
| "PATCH"
9+
| "OPTIONS"
10+
| "HEAD";
1111

1212
export interface RequestOptions {
1313
method: HttpMethods;

demo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ querystring.set('limit', '1');
88

99
// Grab a normal JSON resource
1010
console.log(await client.performRequest({
11-
method: 'get',
11+
method: 'GET',
1212
path: `/api/v1/namespaces/default/endpoints`,
1313
expectJson: true,
1414
querystring,
1515
}));
1616

1717
// Stream multiple JSON objects for a Watch operation
1818
for await (const line of await client.performRequest({
19-
method: 'get',
19+
method: 'GET',
2020
path: `/api/v1/namespaces/default/endpoints`,
2121
expectStream: true,
2222
expectJson: true,
@@ -30,7 +30,7 @@ for await (const line of await client.performRequest({
3030

3131
// Stream plaintext log lines from a pod
3232
for await (const line of await client.performRequest({
33-
method: 'get',
33+
method: 'GET',
3434
path: `/api/v1/namespaces/default/pods/lambdabot-0/log`,
3535
expectStream: true,
3636
querystring: new URLSearchParams({

transports/unstable/via-incluster-unstable.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { RestClient, HttpMethods, RequestOptions } from '../../common.ts';
2+
import { JsonParsingTransformer, ReadLineTransformer } from "../../stream-transformers.ts";
23

34
function join(...args: string[]) {
45
return args.join('/');
@@ -22,7 +23,7 @@ function join(...args: string[]) {
2223
export class InClusterUnstableRestClient implements RestClient {
2324
readonly baseUrl: string;
2425
readonly secretsPath: string;
25-
readonly namespace: string;
26+
readonly defaultNamespace: string;
2627
readonly #httpClient: Deno.HttpClient;
2728
readonly #token: string;
2829

@@ -33,34 +34,43 @@ export class InClusterUnstableRestClient implements RestClient {
3334
this.baseUrl = baseUrl;
3435
this.secretsPath = secretsPath;
3536

36-
this.namespace = Deno.readTextFileSync(join(secretsPath, 'namespace'));
37+
this.defaultNamespace = Deno.readTextFileSync(join(secretsPath, 'namespace'));
3738
this.#httpClient = Deno.createHttpClient({ caFile: join(secretsPath, `ca.crt`) });
3839
this.#token = Deno.readTextFileSync(join(secretsPath, 'token'));
3940
}
4041

41-
async performRequest(method: HttpMethods, opts: RequestOptions={}): Promise<any> {
42+
async performRequest(opts: RequestOptions): Promise<any> {
4243
let path = opts.path || '/';
4344
if (opts.querystring) {
4445
path += `?${opts.querystring}`;
4546
}
46-
console.error(method.toUpperCase(), path);
47+
console.error(opts.method, path);
4748

4849
const resp = await fetch(this.baseUrl + path, {
49-
method: method,
50-
body: opts.bodyStream ?? JSON.stringify(opts.body),
50+
method: opts.method,
51+
body: opts.bodyStream ?? opts.bodyRaw ?? JSON.stringify(opts.bodyJson),
5152
redirect: 'error',
5253
signal: opts.abortSignal,
5354
headers: {
5455
'Authorization': `Bearer ${this.#token}`,
55-
'Accept': opts.accept ?? 'application/octet-stream',
56+
'Accept': opts.accept ?? (opts.expectJson ? 'application/json' : 'application/octet-stream'),
5657
},
5758
client: this.#httpClient,
5859
});
5960

60-
if (opts.streaming) {
61-
return resp.body;
62-
} else if (opts.accept === 'application/json') {
61+
if (opts.expectStream) {
62+
if (!resp.body) return new ReadableStream();
63+
if (opts.expectJson) {
64+
return resp.body
65+
.pipeThrough(new ReadLineTransformer('utf-8'))
66+
.pipeThrough(new JsonParsingTransformer());
67+
} else {
68+
return resp.body;
69+
}
70+
71+
} else if (opts.expectJson) {
6372
return resp.json();
73+
6474
} else {
6575
return new Uint8Array(await resp.arrayBuffer());
6676
}

transports/unstable/via-kubeconfig.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import { RestClient, HttpMethods, RequestOptions } from '../../common.ts';
2929

3030
export class KubeConfigRestClient implements RestClient {
3131
constructor(kubeConfig: KubeConfig, httpClient: Deno.HttpClient, namespace?: string) {
32-
this.namespace = namespace;
32+
this.defaultNamespace = namespace;
3333
throw new Error(`TODO: implement :)`);
3434
}
35-
namespace?: string;
35+
defaultNamespace?: string;
3636

3737
static async fromKubeConfig(path?: string): Promise<KubeConfigRestClient> {
3838

@@ -53,12 +53,12 @@ export class KubeConfigRestClient implements RestClient {
5353
}
5454

5555

56-
async performRequest(method: HttpMethods, opts: RequestOptions={}): Promise<any> {
56+
async performRequest(opts: RequestOptions): Promise<any> {
5757
let path = opts.path || '/';
5858
if (opts.querystring) {
5959
path += `?${opts.querystring}`;
6060
}
61-
console.error(method.toUpperCase(), path);
61+
console.error(opts.method, path);
6262

6363
throw new Error(`TODO: implement`);
6464
}

transports/via-incluster.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class InClusterRestClient implements RestClient {
3939
if (opts.querystring) {
4040
path += `?${opts.querystring}`;
4141
}
42-
console.error(opts.method.toUpperCase(), path);
42+
console.error(opts.method, path);
4343

4444
const resp = await fetch(this.baseUrl + path, {
4545
method: opts.method,

transports/via-kubectl-raw.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ export class KubectlRawRestClient implements RestClient {
2323

2424
async performRequest(opts: RequestOptions): Promise<any> {
2525
const command = {
26-
get: 'get',
27-
post: 'create',
28-
delete: 'delete',
29-
put: 'replace',
30-
patch: '',
31-
options: '',
32-
head: '',
26+
GET: 'get',
27+
POST: 'create',
28+
DELETE: 'delete',
29+
PUT: 'replace',
30+
PATCH: '',
31+
OPTIONS: '',
32+
HEAD: '',
3333
}[opts.method];
34-
if (!command) throw new Error(`KubectlRawRestClient cannot perform HTTP ${opts.method.toUpperCase()}`);
34+
if (!command) throw new Error(`KubectlRawRestClient cannot perform HTTP ${opts.method}`);
3535

3636
if (opts.abortSignal?.aborted) throw new Error(`Given AbortSignal is already aborted`);
3737

@@ -42,7 +42,7 @@ export class KubectlRawRestClient implements RestClient {
4242
}
4343

4444
const hasReqBody = opts.bodyJson !== undefined || !!opts.bodyRaw || !!opts.bodyStream;
45-
console.error(opts.method.toUpperCase(), path, hasReqBody ? '(w/ body)' : '');
45+
console.error(opts.method, path, hasReqBody ? '(w/ body)' : '');
4646

4747
const p = Deno.run({
4848
cmd: ["kubectl", command, ...(hasReqBody ? ['-f', '-'] : []), "--raw", path],

0 commit comments

Comments
 (0)