import axios, { AxiosResponse } from 'axios';
import { Singleton } from "typescript-ioc";
import { Value } from "mslib"
import { MeClientRequest } from './MeClientRequest';
import { MeClientRequestType } from './MeClientRequestType';

/**
 * 中台1.0通用客户端
 * @date 2021-05-08 17:14:32
 * @author 周勇强
 */
@Singleton
class MeClient {

    @Value('register.url')
    registerUrl: string

    /**
     * 执行调用中台1.0接口
     * @param parameters
     */
    async exec(meClientRequest: MeClientRequest): Promise<AxiosResponse> {
        let sid = `${meClientRequest.msId}.${meClientRequest.uri}`
        let config = {
            headers: {
                'sid': sid,
                'version': meClientRequest.version
            },
            timeout: 30000,
            params: meClientRequest.requestParam,
            data: meClientRequest.requestBody
        };

        switch (meClientRequest.requestType) {
            case MeClientRequestType.GET:
                return await axios.get(this.registerUrl, config);
            case MeClientRequestType.POST:
                return await axios.post(this.registerUrl, {}, config);
            case MeClientRequestType.PUT:
                return await axios.post(this.registerUrl, {}, config);
            case MeClientRequestType.DELETE:
                return await axios.get(this.registerUrl, config);
        }
    }
}

export { MeClient }