Skip to content

Commit

Permalink
Adding custom User-Agent to spice.js (#213)
Browse files Browse the repository at this point in the history
* Adding custom user-agent to spice.js

* Fixing User-Agent header
  • Loading branch information
Scott Lyons authored Nov 21, 2024
1 parent 968f00c commit 232d046
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ class SpiceClient {
this._apiKey = params;
this._httpUrl = 'https://data.spiceai.io';
this._flightUrl = 'flight.spiceai.io:443';
this._userAgent = getUserAgent();
} else {
const { apiKey, httpUrl, flightUrl, flightTlsEnabled } = params;
const { apiKey, httpUrl, flightUrl, flightTlsEnabled, userAgent } = params;

this._apiKey = apiKey;
this._httpUrl = httpUrl || 'http://127.0.0.1:8090';
Expand All @@ -72,9 +73,8 @@ class SpiceClient {
: this._flightUrl.includes('127.0.0.1')
? false
: true;
this._userAgent = userAgent || getUserAgent();
}

this._userAgent = getUserAgent();
}

private createClient(meta: any): any {
Expand Down Expand Up @@ -105,7 +105,7 @@ class SpiceClient {
const meta = new grpc.Metadata();
const client: FlightClient = this.createClient(meta);
meta.set('authorization', 'Bearer ' + this._apiKey);
meta.set('x-spice-user-agent', this._userAgent);
meta.set('User-Agent', this._userAgent);

let queryBuff = Buffer.from(queryText, 'utf8');

Expand Down Expand Up @@ -231,7 +231,7 @@ class SpiceClient {
const headers = [
['Content-Type', 'application/json'],
['Accept-Encoding', 'br, gzip, deflate'],
['X-Spice-User-Agent', this._userAgent]
['User-Agent', this._userAgent]
];

if (this._apiKey) {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface SpiceClientConfig {
httpUrl?: string;
flightUrl?: string;
flightTlsEnabled?: boolean;
userAgent?: string;
}

export interface RefreshOverrides {
Expand Down
2 changes: 1 addition & 1 deletion src/user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export function getUserAgent(): string {
const osType = os.type();
const osRelease = os.release();
const osArch = os.machine();
return `spice.js ${VERSION} (${osType}/${osRelease} ${osArch})`;
return `spice.js/${VERSION} (${osType}/${osRelease} ${osArch})`;
}
2 changes: 1 addition & 1 deletion test/user-agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getUserAgent } from "../src/user-agent";

describe("user-agent", () => {
const matching_regex = /spice.js \d+\.\d+\.\d+ \((Linux|Windows|Darwin)\/[\d\w\.\-\_]+ (x86_64|aarch64|i386|arm64)\)/;
const matching_regex = /spice.js\/\d+\.\d+\.\d+ \((Linux|Windows|Darwin)\/[\d\w\.\-\_]+ (x86_64|aarch64|i386|arm64)\)/;

it("should match the user-agent regex", async () => {
const ua = getUserAgent();
Expand Down

0 comments on commit 232d046

Please sign in to comment.