starry / backend /libs /adminSolutionStore.ts
k-l-lambda's picture
feat: add Python ML services (CPU mode) with model download
2b7aae2
import { SolutionStore } from './store';
import { RegulationSolution } from '../../src/starry';
import getSign from '../../src/utils/request/createSign';
import * as asyncAtom from '../../src/utils/asyncAtom';
const getHeaders = (method: string = 'get', data: any = {}): Record<string, string> => {
const headers = {
stmp: Date.now(),
sess: '654a591de5d3a4b0',
ver: 512,
orn: '(X11;$Linux x86_64$android$1.0.0$torchTrain',
sign: '',
size: 0,
dst: '',
type: 1,
lang: 'zh_CN',
seq: 0,
code: 0,
ext: 'CN',
desc: '',
} as any;
headers.sign = getSign({ method, data, requestHeader: headers });
return headers;
};
export default class AdminSolutionStore implements SolutionStore {
env: string;
host: string;
fetch: typeof fetch;
constructor(options: { env: string; host: string; fetch: typeof fetch }) {
Object.assign(this, options);
}
post(path, { data }: any): Promise<any> {
const headers = getHeaders('post', data);
headers['content-type'] = 'application/json;charset=UTF-8';
return this.fetch(this.host + path, {
method: 'post',
headers,
body: JSON.stringify(data),
}).then((res) => res.json());
}
async batchGet(keys: string[]): Promise<RegulationSolution[]> {
const solutions = (
await this.post('/torch/musicSet/manage/cache/batchGet', {
data: {
env: this.env,
nameList: keys,
},
})
).data as RegulationSolution[];
//console.log('AdminSolutionStore.batchGet:', solutions);
return Promise.all(solutions);
}
async get(key: string): Promise<RegulationSolution> {
const solution = (await this.post('/torch/musicSet/manage/cache/batchGet', {
data: {
env: this.env,
nameList: [key],
},
}).then((res) => res.data[0])) as RegulationSolution;
//console.log('AdminSolutionStore.get:', key, solution);
return solution;
}
async set(key: string, value: RegulationSolution) {
const request = () =>
this.post('/torch/musicSet/manage/cache/set', {
data: {
env: this.env,
name: key,
value: value,
},
}).catch((err) => console.warn('failed to set solution:', key, err));
asyncAtom.queue(this, request);
//console.log('AdminSolutionStore.set:', key, res.ok);
}
/*remove(key: string) {
return request.post('/torch/musicSet/manage/cache/delete', {
data: {
env: this.env,
name: key,
},
});
}*/
}