adding data-services
This commit is contained in:
parent
e3bfd05b90
commit
405b818c86
139 changed files with 55943 additions and 416 deletions
30
libs/api-client/src/BacktestClient.ts
Normal file
30
libs/api-client/src/BacktestClient.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { BaseApiClient } from './BaseApiClient';
|
||||
import { ApiResponse, BacktestConfig, BacktestResult } from '@stock-bot/shared-types';
|
||||
|
||||
/**
|
||||
* Client for interacting with the Backtest Engine service
|
||||
*/
|
||||
export class BacktestClient extends BaseApiClient {
|
||||
/**
|
||||
* Run a backtest
|
||||
*/
|
||||
async runBacktest(config: BacktestConfig): Promise<ApiResponse<BacktestResult>> {
|
||||
return this.client.post('/api/backtest/run', config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a backtest by ID
|
||||
*/
|
||||
async getBacktest(id: string): Promise<ApiResponse<BacktestResult>> {
|
||||
return this.client.get(`/api/backtest/${id}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* List all backtests for a strategy
|
||||
*/
|
||||
async listBacktests(strategyId: string): Promise<ApiResponse<BacktestResult[]>> {
|
||||
return this.client.get(`/api/backtest`, {
|
||||
params: { strategyId }
|
||||
});
|
||||
}
|
||||
}
|
||||
39
libs/api-client/src/BaseApiClient.ts
Normal file
39
libs/api-client/src/BaseApiClient.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
|
||||
import { ApiResponse } from '@stock-bot/shared-types';
|
||||
|
||||
/**
|
||||
* Base API client that all service clients extend
|
||||
*/
|
||||
export abstract class BaseApiClient {
|
||||
protected client: AxiosInstance;
|
||||
|
||||
constructor(baseURL: string, config?: AxiosRequestConfig) {
|
||||
this.client = axios.create({
|
||||
baseURL,
|
||||
timeout: 10000, // 10 seconds timeout
|
||||
...config
|
||||
});
|
||||
|
||||
// Add response interceptor for consistent error handling
|
||||
this.client.interceptors.response.use(
|
||||
(response) => response.data,
|
||||
(error) => {
|
||||
// Format error for consistent error handling
|
||||
const formattedError = {
|
||||
status: error.response?.status || 500,
|
||||
message: error.response?.data?.error || error.message || 'Unknown error',
|
||||
originalError: error
|
||||
};
|
||||
|
||||
return Promise.reject(formattedError);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the health status of a service
|
||||
*/
|
||||
async getHealth(): Promise<ApiResponse<{ status: string }>> {
|
||||
return this.client.get('/api/health');
|
||||
}
|
||||
}
|
||||
56
libs/api-client/src/StrategyClient.ts
Normal file
56
libs/api-client/src/StrategyClient.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import { BaseApiClient } from './BaseApiClient';
|
||||
import { ApiResponse, Strategy } from '@stock-bot/shared-types';
|
||||
|
||||
/**
|
||||
* Client for interacting with the Strategy Orchestrator service
|
||||
*/
|
||||
export class StrategyClient extends BaseApiClient {
|
||||
/**
|
||||
* Get a strategy by ID
|
||||
*/
|
||||
async getStrategy(id: string): Promise<ApiResponse<Strategy>> {
|
||||
return this.client.get(`/api/strategy/${id}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* List all strategies
|
||||
*/
|
||||
async listStrategies(): Promise<ApiResponse<Strategy[]>> {
|
||||
return this.client.get('/api/strategy');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new strategy
|
||||
*/
|
||||
async createStrategy(strategy: Omit<Strategy, 'id'>): Promise<ApiResponse<Strategy>> {
|
||||
return this.client.post('/api/strategy', strategy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a strategy
|
||||
*/
|
||||
async updateStrategy(id: string, strategy: Partial<Strategy>): Promise<ApiResponse<Strategy>> {
|
||||
return this.client.put(`/api/strategy/${id}`, strategy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a strategy
|
||||
*/
|
||||
async deleteStrategy(id: string): Promise<ApiResponse<void>> {
|
||||
return this.client.delete(`/api/strategy/${id}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable a strategy
|
||||
*/
|
||||
async enableStrategy(id: string): Promise<ApiResponse<Strategy>> {
|
||||
return this.client.post(`/api/strategy/${id}/enable`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable a strategy
|
||||
*/
|
||||
async disableStrategy(id: string): Promise<ApiResponse<Strategy>> {
|
||||
return this.client.post(`/api/strategy/${id}/disable`);
|
||||
}
|
||||
}
|
||||
14
libs/api-client/src/index.ts
Normal file
14
libs/api-client/src/index.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import { BaseApiClient } from './BaseApiClient';
|
||||
import { BacktestClient } from './BacktestClient';
|
||||
import { StrategyClient } from './StrategyClient';
|
||||
|
||||
export { BaseApiClient, BacktestClient, StrategyClient };
|
||||
|
||||
// Factory functions
|
||||
export function createBacktestClient(baseUrl: string = 'http://localhost:4002'): BacktestClient {
|
||||
return new BacktestClient(baseUrl);
|
||||
}
|
||||
|
||||
export function createStrategyClient(baseUrl: string = 'http://localhost:4001'): StrategyClient {
|
||||
return new StrategyClient(baseUrl);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue