|
| 1 | +import * as vscode from "vscode" |
| 2 | +import { getExtensionVersion } from "../extension" |
| 3 | +import { AUTH_PROVIDER_ID } from "./auth" |
| 4 | +import type { |
| 5 | + App, |
| 6 | + Deployment, |
| 7 | + ListResponse, |
| 8 | + Team, |
| 9 | + UploadInfo, |
| 10 | + User, |
| 11 | +} from "./types" |
| 12 | + |
| 13 | +export const BASE_URL = "https://api.fastapicloud.com/api/v1" |
| 14 | +export const DASHBOARD_URL = "https://dashboard.fastapicloud.com" |
| 15 | + |
| 16 | +function getUserAgentHeaders(): Record<string, string> { |
| 17 | + if (vscode.env.uiKind === vscode.UIKind.Web) return {} |
| 18 | + return { "User-Agent": `fastapi-vscode/${getExtensionVersion()}` } |
| 19 | +} |
| 20 | + |
| 21 | +export class ApiService { |
| 22 | + static getDashboardUrl(teamSlug: string, appSlug: string): string { |
| 23 | + return `${DASHBOARD_URL}/${teamSlug}/apps/${appSlug}/general` |
| 24 | + } |
| 25 | + |
| 26 | + private async request<T>( |
| 27 | + endpoint: string, |
| 28 | + options: RequestInit = {}, |
| 29 | + ): Promise<T> { |
| 30 | + const session = await vscode.authentication.getSession( |
| 31 | + AUTH_PROVIDER_ID, |
| 32 | + [], |
| 33 | + { silent: true }, |
| 34 | + ) |
| 35 | + if (!session) { |
| 36 | + throw new Error("Not authenticated") |
| 37 | + } |
| 38 | + const token = session.accessToken |
| 39 | + |
| 40 | + const response = await fetch(`${BASE_URL}${endpoint}`, { |
| 41 | + ...options, |
| 42 | + headers: { |
| 43 | + Authorization: `Bearer ${token}`, |
| 44 | + "Content-Type": "application/json", |
| 45 | + ...getUserAgentHeaders(), |
| 46 | + ...options.headers, |
| 47 | + }, |
| 48 | + }) |
| 49 | + |
| 50 | + if (!response.ok) { |
| 51 | + throw new Error( |
| 52 | + `API request failed: ${options.method || "GET"} ${endpoint} returned ${response.status}`, |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + return response.json() as Promise<T> |
| 57 | + } |
| 58 | + |
| 59 | + static async getUser(token: string): Promise<User | null> { |
| 60 | + try { |
| 61 | + const response = await fetch(`${BASE_URL}/users/me`, { |
| 62 | + headers: { |
| 63 | + Authorization: `Bearer ${token}`, |
| 64 | + "Content-Type": "application/json", |
| 65 | + ...getUserAgentHeaders(), |
| 66 | + }, |
| 67 | + }) |
| 68 | + if (!response.ok) return null |
| 69 | + return (await response.json()) as User |
| 70 | + } catch { |
| 71 | + return null |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + async getTeams(): Promise<Team[]> { |
| 76 | + const data = await this.request<ListResponse<Team>>("/teams") |
| 77 | + return data.data |
| 78 | + } |
| 79 | + |
| 80 | + async getTeam(teamId: string): Promise<Team> { |
| 81 | + return this.request<Team>(`/teams/${teamId}/`) |
| 82 | + } |
| 83 | + |
| 84 | + async getApps(teamId: string): Promise<App[]> { |
| 85 | + const data = await this.request<ListResponse<App>>( |
| 86 | + `/apps/?team_id=${teamId}`, |
| 87 | + ) |
| 88 | + return data.data |
| 89 | + } |
| 90 | + |
| 91 | + async getApp(appId: string): Promise<App> { |
| 92 | + return this.request<App>(`/apps/${appId}`) |
| 93 | + } |
| 94 | + |
| 95 | + async createApp(teamId: string, name: string): Promise<App> { |
| 96 | + return this.request<App>("/apps/", { |
| 97 | + method: "POST", |
| 98 | + body: JSON.stringify({ team_id: teamId, name }), |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + async createDeployment(appId: string): Promise<Deployment> { |
| 103 | + return this.request<Deployment>(`/apps/${appId}/deployments/`, { |
| 104 | + method: "POST", |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + async getUploadUrl(deploymentId: string): Promise<UploadInfo> { |
| 109 | + return this.request<UploadInfo>(`/deployments/${deploymentId}/upload`, { |
| 110 | + method: "POST", |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + async completeUpload(deploymentId: string): Promise<void> { |
| 115 | + await this.request<void>(`/deployments/${deploymentId}/upload-complete`, { |
| 116 | + method: "POST", |
| 117 | + }) |
| 118 | + } |
| 119 | + |
| 120 | + async getDeployment( |
| 121 | + appId: string, |
| 122 | + deploymentId: string, |
| 123 | + ): Promise<Deployment> { |
| 124 | + return this.request<Deployment>( |
| 125 | + `/apps/${appId}/deployments/${deploymentId}/`, |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + static async requestDeviceCode(clientId: string): Promise<{ |
| 130 | + device_code: string |
| 131 | + user_code: string |
| 132 | + verification_uri: string |
| 133 | + verification_uri_complete?: string |
| 134 | + expires_in?: number |
| 135 | + interval?: number |
| 136 | + }> { |
| 137 | + const response = await fetch(`${BASE_URL}/login/device/authorization`, { |
| 138 | + method: "POST", |
| 139 | + headers: { |
| 140 | + "Content-Type": "application/x-www-form-urlencoded", |
| 141 | + ...getUserAgentHeaders(), |
| 142 | + }, |
| 143 | + body: new URLSearchParams({ client_id: clientId }).toString(), |
| 144 | + }) |
| 145 | + |
| 146 | + if (!response.ok) { |
| 147 | + throw new Error( |
| 148 | + `Device code request failed: ${response.status} ${response.statusText}`, |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + const data = (await response.json()) as { |
| 153 | + device_code?: string |
| 154 | + user_code?: string |
| 155 | + verification_uri?: string |
| 156 | + verification_uri_complete?: string |
| 157 | + expires_in?: number |
| 158 | + interval?: number |
| 159 | + } |
| 160 | + |
| 161 | + if (!data.device_code || !data.user_code || !data.verification_uri) { |
| 162 | + throw new Error("Invalid response from device code endpoint") |
| 163 | + } |
| 164 | + return { |
| 165 | + device_code: data.device_code, |
| 166 | + user_code: data.user_code, |
| 167 | + verification_uri: data.verification_uri, |
| 168 | + verification_uri_complete: data.verification_uri_complete ?? "", |
| 169 | + expires_in: data.expires_in, |
| 170 | + interval: data.interval, |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + static async pollDeviceToken( |
| 175 | + clientId: string, |
| 176 | + deviceCode: string, |
| 177 | + intervalMs = 5000, |
| 178 | + signal?: AbortSignal, |
| 179 | + ): Promise<string> { |
| 180 | + while (true) { |
| 181 | + if (signal?.aborted) { |
| 182 | + throw new Error("Sign-in cancelled") |
| 183 | + } |
| 184 | + |
| 185 | + const response = await fetch(`${BASE_URL}/login/device/token`, { |
| 186 | + method: "POST", |
| 187 | + headers: { |
| 188 | + "Content-Type": "application/x-www-form-urlencoded", |
| 189 | + ...getUserAgentHeaders(), |
| 190 | + }, |
| 191 | + body: new URLSearchParams({ |
| 192 | + client_id: clientId, |
| 193 | + device_code: deviceCode, |
| 194 | + grant_type: "urn:ietf:params:oauth:grant-type:device_code", |
| 195 | + }).toString(), |
| 196 | + signal, |
| 197 | + }) |
| 198 | + |
| 199 | + const data = (await response.json()) as { |
| 200 | + access_token?: string |
| 201 | + error?: string |
| 202 | + } |
| 203 | + |
| 204 | + if (response.ok && data.access_token) { |
| 205 | + return data.access_token |
| 206 | + } |
| 207 | + if (data.error === "authorization_pending") { |
| 208 | + await new Promise((resolve) => setTimeout(resolve, intervalMs)) |
| 209 | + } else if (data.error === "expired_token") { |
| 210 | + throw new Error("Device code has expired") |
| 211 | + } else { |
| 212 | + throw new Error( |
| 213 | + `Device token request failed: ${data.error || response.statusText}`, |
| 214 | + ) |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments