Skip to content

Commit

Permalink
Make client accept URLs for flight and HTTP endpoints (#93)
Browse files Browse the repository at this point in the history
* basic v1 price functions

* remove v0.1 prices

* fix tests remove v0.1

* fix values

* fix

* revert yarn.lock

* revert whitespace

* more

* make client accept URLs for flight and HTTP endpoints

* timeouts
  • Loading branch information
Jeadie authored Sep 27, 2023
1 parent 0542d43 commit 13948a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
20 changes: 10 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import {
const fetch = require('node-fetch');
const httpsAgent = new https.Agent({ keepAlive: true });

const HTTP_DATA_PATH = 'https://data.spiceai.io';
const FLIGHT_PATH = 'flight.spiceai.io:443';

const PROTO_PATH = './proto/Flight.proto';
// If we're running in a Next.js environment, we need to adjust the path to the proto file
const PACKAGE_PATH = __dirname.includes('/.next/server/app')
Expand All @@ -48,10 +45,13 @@ const flight_proto = arrow.flight.protocol;

class SpiceClient {
private _apiKey: string;
private _url: string;
public constructor(apiKey: string, url: string = FLIGHT_PATH) {
private _flight_url: string;
private _http_url: string;

public constructor(apiKey: string, http_url: string = 'https://data.spiceai.io', flight_url: string = 'flight.spiceai.io:443') {
this._apiKey = apiKey;
this._url = url;
this._http_url = http_url;
this._flight_url = flight_url;
}

private createClient(meta: any): any {
Expand All @@ -65,7 +65,7 @@ class SpiceClient {
creds,
callCreds
);
return new flight_proto.FlightService(this._url, combCreds);
return new flight_proto.FlightService(this._flight_url, combCreds);
}

private async getResultStream(
Expand Down Expand Up @@ -191,7 +191,7 @@ public async getPrices(pair: string[], startTime?: number, endTime?: number, gra
notifications: [{ name: queryName, type: 'webhook', uri: webhookUri }],
};

const resp = await fetch(`${HTTP_DATA_PATH}/v0.1/sql`, {
const resp = await fetch(`${this._http_url}/v0.1/sql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -308,9 +308,9 @@ public async getPrices(pair: string[], startTime?: number, endTime?: number, gra
) => {
let url;
if (params && Object.keys(params).length) {
url = `${HTTP_DATA_PATH}${path}?${new URLSearchParams(params)}`;
url = `${this._http_url}${path}?${new URLSearchParams(params)}`;
} else {
url = `${HTTP_DATA_PATH}${path}`;
url = `${this._http_url}${path}`;
}

return await fetch(url, {
Expand Down
9 changes: 6 additions & 3 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import { LatestPrices } from '../src/interfaces';
const RELAY_BUCKETS = ['spice.js'];
const RELAY_URL = 'https://o4skc7qyx7mrl8x7wdtgmc.hooks.webhookrelay.com';

const HTTP_DATA_PATH = process.env.HTTP_URL ? process.env.HTTP_URL : 'https://data.spiceai.io'
const FLIGHT_PATH = process.env.FLIGHT_URL ? process.env.FLIGHT_URL : 'flight.spiceai.io:443'

dotenv.config();
const api_key = process.env.API_KEY;
if (!api_key) {
throw 'API_KEY environment variable not set';
}
const client = new SpiceClient(api_key);
const client = new SpiceClient(api_key, HTTP_DATA_PATH, FLIGHT_PATH);

const wait = (ms: number) => new Promise((res) => setTimeout(res, ms));

Expand Down Expand Up @@ -188,7 +191,7 @@ test('test historical prices works', async () => {
expect(prices[v][0].price).toEqual(16527.39);
expect(prices[v][23].price).toEqual(16612.22);
})
});
}, 10000);

test('test historical prices with multiple pairs', async () => {
let pairs=['BTC-USD', 'ETH-AUD'];
Expand All @@ -211,4 +214,4 @@ test('test historical prices with multiple pairs', async () => {
expect(prices[v][0].price).toBeGreaterThan(0.0);
expect(prices[v][23].price).toBeGreaterThan(0.0);
})
});
}, 10000);

0 comments on commit 13948a6

Please sign in to comment.