Skip to content

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 5 commits into from
Jun 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use temporary client for login
  • Loading branch information
code-asher committed May 31, 2024
commit 96c2642c97a00008e0101cef1e659ed35d44f81b
14 changes: 11 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Api } from "coder/site/src/api/api"
import { getErrorMessage } from "coder/site/src/api/errors"
import { User, Workspace, WorkspaceAgent } from "coder/site/src/api/typesGenerated"
import * as vscode from "vscode"
import { makeCoderSdk } from "./api"
import { extractAgents } from "./api-helper"
import { CertificateError } from "./error"
import { Remote } from "./remote"
Expand Down Expand Up @@ -94,7 +95,10 @@ export class Commands {
if (!url) {
return
}
this.restClient.setHost(url)

// Use a temporary client to avoid messing with the global one while trying
// to log in.
const restClient = await makeCoderSdk(url, undefined, this.storage)

let user: User | undefined
let token: string | undefined = args.length >= 2 ? args[1] : undefined
Expand All @@ -112,9 +116,9 @@ export class Commands {
value: await this.storage.getSessionToken(),
ignoreFocusOut: true,
validateInput: async (value) => {
this.restClient.setSessionToken(value)
restClient.setSessionToken(value)
try {
user = await this.restClient.getAuthenticatedUser()
user = await restClient.getAuthenticatedUser()
if (!user) {
throw new Error("Failed to get authenticated user")
}
Expand Down Expand Up @@ -145,6 +149,10 @@ export class Commands {
return
}

// The URL and token are good; authenticate the global client.
this.restClient.setHost(url)
this.restClient.setSessionToken(token)

// Store these to be used in later sessions and in the cli.
await this.storage.setURL(url)
await this.storage.setSessionToken(token)
Expand Down