diff --git a/src/providers/up9Api.ts b/src/providers/up9Api.ts index 4c55293..000225a 100644 --- a/src/providers/up9Api.ts +++ b/src/providers/up9Api.ts @@ -1,9 +1,15 @@ import axios from 'axios'; import { Workspace, TestResponse, Endpoint } from '../models/up9'; import {raiseForBadResponse} from'../utils'; +import * as vscode from 'vscode'; +import { internalExtensionName } from '../consts'; const LATEST_REVISION_CACHE_TTL_MS = 120 * 1000; //2 minutes +const client = axios.create({ + headers: {'x-up9-client-app': 'vscode-extension', 'x-up9-client-version': vscode?.extensions?.getExtension(internalExtensionName)?.packageJSON?.version ?? "unknown"} + }); + export class UP9ApiProvider { private readonly _trccUrl: string; private _latestRevisionCache: {[key: string]: {id: string, cachedAt: number}} @@ -14,7 +20,7 @@ export class UP9ApiProvider { } public getWorkspaces = async (token: string): Promise => { - const response = await axios.get(`${this._trccUrl}/models/`, {headers: {'Authorization': `Bearer ${token}`}}) + const response = await client.get(`${this._trccUrl}/models/`, {headers: {'Authorization': `Bearer ${token}`}}) raiseForBadResponse(response); return response.data.map(workspace => workspace.modelId); } @@ -22,7 +28,7 @@ export class UP9ApiProvider { public getWorkspaceEndpoints = async (workspaceId: string, token: string): Promise => { const latestRevision = await this.getLatestRevisionForWorkspace(workspaceId, token); - const response = await axios.get(`${this._trccUrl}/models/${workspaceId}/${latestRevision}/all/endpoints`, {headers: {'Authorization': `Bearer ${token}`}}) + const response = await client.get(`${this._trccUrl}/models/${workspaceId}/${latestRevision}/all/endpoints`, {headers: {'Authorization': `Bearer ${token}`}}) raiseForBadResponse(response); return response.data; } @@ -32,34 +38,34 @@ export class UP9ApiProvider { const url = `${this._trccUrl}/models/${workspaceId}/${latestRevision}/all/tests?base64Code=false&addTestData=true`; const body = {spanGuids: [spanGuid], microTestsOnly: true}; - const response = await axios.post(url, body, {headers: {'Authorization': `Bearer ${token}`, 'Content-Type': "application/json"}}); + const response = await client.post(url, body, {headers: {'Authorization': `Bearer ${token}`, 'Content-Type': "application/json"}}); raiseForBadResponse(response); return response.data; } public runTestCodeOnAgent = async(workspaceId: string, testCode: string, token: string) => { const serializedCode = `data:text/plain;base64,${Buffer.from(testCode).toString('base64')}`; - const response = await axios.post(`${this._trccUrl}/agents/runSingleTest`, {model: workspaceId, code: serializedCode, isRawCode: true}, {headers: {'Authorization': `Bearer ${token}`}, timeout: 30000}); + const response = await client.post(`${this._trccUrl}/agents/runSingleTest`, {model: workspaceId, code: serializedCode, isRawCode: true}, {headers: {'Authorization': `Bearer ${token}`}, timeout: 30000}); raiseForBadResponse(response); return response.data; } public getSwagger = async(workspaceId: string, token: string): Promise => { const latestRevision = await this.getLatestRevisionForWorkspace(workspaceId, token); - const response = await axios.get(`${this._trccUrl}/models/${workspaceId}/${latestRevision}/all/swagger`, {headers: {'Authorization': `Bearer ${token}`}}); + const response = await client.get(`${this._trccUrl}/models/${workspaceId}/${latestRevision}/all/swagger`, {headers: {'Authorization': `Bearer ${token}`}}); raiseForBadResponse(response); return response.data; } public getSpans = async(workspaceId: string, spanId: string, token: string): Promise => { const latestRevision = await this.getLatestRevisionForWorkspace(workspaceId, token); - const response = await axios.get(`${this._trccUrl}/models/${workspaceId}/${latestRevision}/all/dataDependency`, {headers: {'Authorization': `Bearer ${token}`}}); + const response = await client.get(`${this._trccUrl}/models/${workspaceId}/${latestRevision}/all/dataDependency`, {headers: {'Authorization': `Bearer ${token}`}}); raiseForBadResponse(response); return response.data; } public checkEnv = async(protocol: string, env: string): Promise => { - const response = await axios.get(`${protocol}://trcc.${env}/apidocs`); + const response = await client.get(`${protocol}://trcc.${env}/apidocs`); raiseForBadResponse(response); return true; } @@ -76,7 +82,7 @@ export class UP9ApiProvider { } } - const response = await axios.get(`${this._trccUrl}/models/${workspaceId}/revisions`, {headers: {'Authorization': `Bearer ${token}`}}); + const response = await client.get(`${this._trccUrl}/models/${workspaceId}/revisions`, {headers: {'Authorization': `Bearer ${token}`}}); raiseForBadResponse(response); const latestRevision = response.data[0];