Skip to content
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

Adding custom User-Agent to spice.js #213

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading