|
| 1 | +import { MetaSolverStrategyDto } from "./data-model/MetaSolverStrategyDto"; |
| 2 | +import { MetaSolverStrategyExecutionOutput } from "./data-model/MetaSolverStrategyExecutionOutput"; |
| 3 | + |
| 4 | +export class StrategyApi { |
| 5 | + baseUrl: string; |
| 6 | + |
| 7 | + constructor(baseUrl: string) { |
| 8 | + this.baseUrl = baseUrl; |
| 9 | + } |
| 10 | + |
| 11 | + async listStrategies( |
| 12 | + problemTypeId: string = "" |
| 13 | + ): Promise<MetaSolverStrategyDto[]> { |
| 14 | + const query = problemTypeId ? "?type=" + problemTypeId : ""; |
| 15 | + |
| 16 | + return await fetch(`${this.baseUrl}/strategies` + query) |
| 17 | + .then((res) => res.json()) |
| 18 | + .then((json) => json as MetaSolverStrategyDto[]) |
| 19 | + .catch((error) => { |
| 20 | + console.error("Failed to list strategies", error); |
| 21 | + return []; |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + listAllStrategies(): Promise<MetaSolverStrategyDto[]> { |
| 26 | + return fetch(`${this.baseUrl}/strategies`) |
| 27 | + .then((res) => res.json()) |
| 28 | + .then((json) => json as MetaSolverStrategyDto[]) |
| 29 | + .catch((error) => { |
| 30 | + console.error("Failed to list all strategies", error); |
| 31 | + throw error; |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + getStrategy(strategyId: string): Promise<MetaSolverStrategyDto> { |
| 36 | + return fetch(`${this.baseUrl}/strategies/${strategyId}`) |
| 37 | + .then((res) => res.json()) |
| 38 | + .then((json) => json as MetaSolverStrategyDto) |
| 39 | + .catch((error) => { |
| 40 | + console.error(`Failed to get strategy ${strategyId}`, error); |
| 41 | + throw error; |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + createStrategy(payload: { |
| 46 | + name: string; |
| 47 | + code: string; |
| 48 | + }): Promise<MetaSolverStrategyDto> { |
| 49 | + return fetch(`${this.baseUrl}/strategies`, { |
| 50 | + method: "POST", |
| 51 | + headers: { "Content-Type": "application/json" }, |
| 52 | + body: JSON.stringify(payload), |
| 53 | + }) |
| 54 | + .then((res) => res.json()) |
| 55 | + .then((json) => json as MetaSolverStrategyDto) |
| 56 | + .catch((error) => { |
| 57 | + console.error("Failed to create strategy", error); |
| 58 | + throw error; |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + updateStrategy( |
| 63 | + strategyId: string, |
| 64 | + payload: { name: string; code: string } |
| 65 | + ): Promise<MetaSolverStrategyDto> { |
| 66 | + return fetch(`${this.baseUrl}/strategies/${strategyId}`, { |
| 67 | + method: "PATCH", |
| 68 | + headers: { "Content-Type": "application/json" }, |
| 69 | + body: JSON.stringify(payload), |
| 70 | + }) |
| 71 | + .then((res) => res.json()) |
| 72 | + .then((json) => json as MetaSolverStrategyDto) |
| 73 | + .catch((error) => { |
| 74 | + console.error(`Failed to update strategy ${strategyId}`, error); |
| 75 | + throw error; |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + deleteStrategy(strategyId: string): Promise<void> { |
| 80 | + return fetch(`${this.baseUrl}/strategies/${strategyId}`, { |
| 81 | + method: "DELETE", |
| 82 | + }) |
| 83 | + .then((res) => res.json()) |
| 84 | + .then(() => undefined) |
| 85 | + .catch((error) => { |
| 86 | + console.error(`Failed to delete strategy ${strategyId}`, error); |
| 87 | + throw error; |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + executeStrategy( |
| 92 | + strategyId: string, |
| 93 | + problemId: string |
| 94 | + ): Promise<MetaSolverStrategyExecutionOutput> { |
| 95 | + return fetch(`${this.baseUrl}/strategies/${strategyId}/execute`, { |
| 96 | + method: "POST", |
| 97 | + headers: { "Content-Type": "application/json" }, |
| 98 | + body: JSON.stringify({ problemId: problemId }), |
| 99 | + }) |
| 100 | + .then((res) => res.json()) |
| 101 | + .then((json) => json as MetaSolverStrategyExecutionOutput) |
| 102 | + .catch((error) => { |
| 103 | + console.error( |
| 104 | + `Failed to execute strategy ${strategyId} on problem ${problemId}`, |
| 105 | + error |
| 106 | + ); |
| 107 | + throw error; |
| 108 | + }); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export const baseUrl = () => process.env.NEXT_PUBLIC_MSS_API_BASE_URL; |
| 113 | + |
| 114 | +export const strategyApi = new StrategyApi(baseUrl() || ""); |
| 115 | + |
| 116 | +console.log(baseUrl()); |
0 commit comments