-
Notifications
You must be signed in to change notification settings - Fork 21
Refactor REST client #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Refactor REST client #286
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
916d769
Convert setSessionToken to async
code-asher 33af7b9
Upgrade to latest Coder
code-asher f6bafef
Use new client in commands for the current workspace
code-asher a5efbab
Reverse authority parts check
code-asher 96c2642
Use temporary client for login
code-asher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Api } from "coder/site/src/api/api" | ||
import fs from "fs/promises" | ||
import * as https from "https" | ||
import * as os from "os" | ||
import * as vscode from "vscode" | ||
import { CertificateError } from "./error" | ||
import { Storage } from "./storage" | ||
|
||
// expandPath will expand ${userHome} in the input string. | ||
const expandPath = (input: string): string => { | ||
const userHome = os.homedir() | ||
return input.replace(/\${userHome}/g, userHome) | ||
} | ||
|
||
/** | ||
* Create an sdk instance using the provided URL and token and hook it up to | ||
* configuration. The token may be undefined if some other form of | ||
* authentication is being used. | ||
*/ | ||
export async function makeCoderSdk(baseUrl: string, token: string | undefined, storage: Storage): Promise<Api> { | ||
const restClient = new Api() | ||
restClient.setHost(baseUrl) | ||
if (token) { | ||
restClient.setSessionToken(token) | ||
} | ||
|
||
restClient.getAxiosInstance().interceptors.request.use(async (config) => { | ||
// Add headers from the header command. | ||
Object.entries(await storage.getHeaders(baseUrl)).forEach(([key, value]) => { | ||
config.headers[key] = value | ||
}) | ||
|
||
// Configure TLS. | ||
const cfg = vscode.workspace.getConfiguration() | ||
const insecure = Boolean(cfg.get("coder.insecure")) | ||
const certFile = expandPath(String(cfg.get("coder.tlsCertFile") ?? "").trim()) | ||
const keyFile = expandPath(String(cfg.get("coder.tlsKeyFile") ?? "").trim()) | ||
const caFile = expandPath(String(cfg.get("coder.tlsCaFile") ?? "").trim()) | ||
|
||
config.httpsAgent = new https.Agent({ | ||
cert: certFile === "" ? undefined : await fs.readFile(certFile), | ||
key: keyFile === "" ? undefined : await fs.readFile(keyFile), | ||
ca: caFile === "" ? undefined : await fs.readFile(caFile), | ||
// rejectUnauthorized defaults to true, so we need to explicitly set it to false | ||
// if we want to allow self-signed certificates. | ||
rejectUnauthorized: !insecure, | ||
}) | ||
|
||
return config | ||
}) | ||
|
||
// Wrap certificate errors. | ||
restClient.getAxiosInstance().interceptors.response.use( | ||
(r) => r, | ||
async (err) => { | ||
throw await CertificateError.maybeWrap(err, baseUrl, storage) | ||
}, | ||
) | ||
|
||
return restClient | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a huge deal, but is there a reason why the Axios instance isn't a separate variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though I guess another option would be to add both interceptors in one call of
interceptors.request.use
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh like why not split out into
const axios = restClient.getAxiosInstance()
type of thing?Could do that, I tend to like leaving things on their objects and accessing them that way, mostly just a preference thing I think. I think maybe I like having them "namespaced" or something like that.
Edit: to add that this only applies to simple getters, if the function does work or the return value can change I would encapsulate into a var for sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh and one is a
request
interceptor and the other aresponse
interceptor so I think they have to be separate callsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, wait – you're right. Totally missed that they were different names