Skip to content

Commit

Permalink
Use https keep-alive agent (#74)
Browse files Browse the repository at this point in the history
* Use https keep-alive agent

* Update tests
  • Loading branch information
lukekim authored Jun 24, 2023
1 parent 20eea75 commit 3e0927e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
76 changes: 42 additions & 34 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import * as https from 'https';
import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import { EventEmitter } from 'stream';
Expand All @@ -23,6 +24,7 @@ import {
} from './interfaces';

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';
Expand Down Expand Up @@ -98,7 +100,7 @@ class SpiceClient {
throw new Error('Pair is required');
}

const resp = await this.fetch(`/v0.1/prices/${pair}`);
const resp = await this.fetchInternal(`/v0.1/prices/${pair}`);
if (!resp.ok) {
throw new Error(
`Failed to get latest price: ${resp.statusText} (${await resp.text()})`
Expand Down Expand Up @@ -132,7 +134,7 @@ class SpiceClient {
params.granularity = granularity;
}

const resp = await this.fetch(`/v0.1/prices/${pair}`, params);
const resp = await this.fetchInternal(`/v0.1/prices/${pair}`, params);
if (!resp.ok) {
throw new Error(
`Failed to get prices: ${resp.statusText} (${await resp.text()})`
Expand All @@ -145,42 +147,43 @@ class SpiceClient {
public async getMultiplePrices(
convert: string,
symbols: string[]
): Promise<LatestPrice[]> {
if (symbols?.length < 1) {
throw new Error('At least 1 symbol is required');
}
): Promise<LatestPrice[]> {
if (symbols?.length < 1) {
throw new Error('At least 1 symbol is required');
}

// Defaults to USD if no conversion symbol provided
if (!convert) {
convert = 'USD';
}
// Defaults to USD if no conversion symbol provided
if (!convert) {
convert = 'USD';
}

const asyncMultiplePricesRequest : AsyncMultiplePricesRequest = {
symbols: symbols,
convert: convert
};

const prices = await fetch(`${HTTP_DATA_PATH}/v0.1/prices`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'br, gzip, deflate',
'X-API-Key': this._apiKey,
},
body: JSON.stringify(asyncMultiplePricesRequest),
});
const asyncMultiplePricesRequest: AsyncMultiplePricesRequest = {
symbols: symbols,
convert: convert,
};

if (!prices.ok) {
throw new Error(
`Failed to get prices: ${prices.status} ${
prices.statusText
} ${await prices.text()}`
);
}
const prices = await fetch(`${HTTP_DATA_PATH}/v0.1/prices`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'br, gzip, deflate',
'X-API-Key': this._apiKey,
},
body: JSON.stringify(asyncMultiplePricesRequest),
agent: httpsAgent,
});

return prices.json() as Promise<LatestPrice[]>;
if (!prices.ok) {
throw new Error(
`Failed to get prices: ${prices.status} ${
prices.statusText
} ${await prices.text()}`
);
}

return prices.json() as Promise<LatestPrice[]>;
}

public async query(
queryText: string,
onData: ((data: Table) => void) | undefined = undefined
Expand Down Expand Up @@ -240,6 +243,7 @@ class SpiceClient {
'X-API-Key': this._apiKey,
},
body: JSON.stringify(asyncQueryRequest),
agent: httpsAgent,
});

if (!resp.ok) {
Expand Down Expand Up @@ -281,7 +285,7 @@ class SpiceClient {
}
}

const resp = await this.fetch(`/v0.1/sql/${queryId}`, params);
const resp = await this.fetchInternal(`/v0.1/sql/${queryId}`, params);
if (!resp.ok) {
throw new Error(
`Failed to get query results: ${resp.status} ${
Expand Down Expand Up @@ -343,7 +347,10 @@ class SpiceClient {
return await this.getQueryResultsAll(notification.queryId);
}

private fetch = async (path: string, params?: { [key: string]: string }) => {
private fetchInternal = async (
path: string,
params?: { [key: string]: string }
) => {
let url;
if (params && Object.keys(params).length) {
url = `${HTTP_DATA_PATH}${path}?${new URLSearchParams(params)}`;
Expand All @@ -357,6 +364,7 @@ class SpiceClient {
'Accept-Encoding': 'br, gzip, deflate',
'X-API-Key': this._apiKey,
},
agent: httpsAgent,
});
};
}
Expand Down
31 changes: 12 additions & 19 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,47 +179,40 @@ test('test historical prices works', async () => {
expect(prices.prices[23].price).toEqual(16612.22);
});


test('test get multiple prices works, when convert is provided and symbols array is not empty', async () => {
var symbolsText = ["cbETH", "stETH", "rETH"];
const multiplePrices1 = await client.getMultiplePrices(
'AUD',
symbolsText
);
var symbolsText = ['ETH', 'LTC'];
const multiplePrices1 = await client.getMultiplePrices('ETH', symbolsText);

expect(multiplePrices1).toBeTruthy();
expect(multiplePrices1[0].pair).toEqual("CBETH-AUD");
expect(multiplePrices1[0].pair).toEqual('ETH-ETH');
expect(multiplePrices1[0].minPrice).toBeTruthy;
expect(multiplePrices1[0].maxPrice).toBeTruthy;
expect(multiplePrices1[0].avePrice).toBeTruthy;
expect(multiplePrices1[1].pair).toEqual("STETH-AUD");
expect(multiplePrices1[1].pair).toEqual('LTC-ETH');
expect(multiplePrices1[1].minPrice).toBeTruthy;
expect(multiplePrices1[1].maxPrice).toBeTruthy;
expect(multiplePrices1[1].avePrice).toBeTruthy;
expect(multiplePrices1.length).toEqual(symbolsText.length);
});

test('test get multiple prices works, when convert is not provided and symbols array is not empty', async () => {
var symbolsText = ["cbETH", "stETH", "rETH"];
const multiplePrices2 = await client.getMultiplePrices(
'',
symbolsText
);
const symbolsText = ['ETH', 'LTC'];
const multiplePrices2 = await client.getMultiplePrices('', symbolsText);

expect(multiplePrices2).toBeTruthy();
expect(multiplePrices2[0].pair).toEqual("CBETH-USD");
expect(multiplePrices2[0].pair).toEqual('ETH-USD');
expect(multiplePrices2[0].minPrice).toBeTruthy;
expect(multiplePrices2[0].maxPrice).toBeTruthy;
expect(multiplePrices2[0].avePrice).toBeTruthy;
expect(multiplePrices2[1].pair).toEqual("STETH-USD");
expect(multiplePrices2[1].pair).toEqual('LTC-USD');
expect(multiplePrices2[1].minPrice).toBeTruthy;
expect(multiplePrices2[1].maxPrice).toBeTruthy;
expect(multiplePrices2[1].avePrice).toBeTruthy;
expect(multiplePrices2.length).toEqual(symbolsText.length);
});

test('test get multiple prices works, when symbols is an empty array', async () => {
expect(async() => {
await client.getMultiplePrices('', [])
}).rejects.toThrow('At least 1 symbol is required')
});
expect(async () => {
await client.getMultiplePrices('', []);
}).rejects.toThrow('At least 1 symbol is required');
});

0 comments on commit 3e0927e

Please sign in to comment.