import * as fs from 'fs'
import  * as MsClient from "cn.keytop.midend.microservice.Client";


let routeHost = "https://mms-test.keytop.cn/";
let appId = "cn.keytop.stc.cm.app1";
let appKey = "98e56a0213ac603549a07eca7dfb9ed2";
let msId = 'cn.keytop.stc.test.ms-mng'
let version = '2.0'
let authType = MsClient.AuthType.BASE

const serviceClientConfig = new MsClient.ServiceClientConfig(routeHost, appId, appKey)
const serviceClient = new MsClient.ServiceClientImpl(serviceClientConfig)


/**
 * 测试 500 响应码
 */
const getTest1 = async () => {
    let uri = '/msServiceTest/getTest1'
    let tags = ''
    let header = {'debug-mode': 'true'};
    let paramMap = {}
    paramMap['param1'] = 'param1'
    paramMap['param2'] = 'param2'
    let response = await serviceClient.get(`${msId}.${uri}`, version, tags, header, paramMap, authType).catch(err => {
        console.log("getTest1=============>" + err)
    })
    if (response) {
        console.log("postTest1=============>" + JSON.stringify(response.data))
    }

}

/**
 * 200 GET 请求
 */
const getTest3 = async () => {
    let uri = '/msServiceTest/getTest3'
    let tags = ''
    let header = {'debug-mode': 'true'};
    let paramMap = {}
    paramMap['param1'] = 'param1'
    paramMap['param2'] = 'param2'
    let response = await serviceClient.get(`${msId}.${uri}`, version, tags, header, paramMap, authType).catch(err => {
        console.log("getTest3=============>" + err)
    })
    if (response) {
        console.log("getTest3=============>" + JSON.stringify(response.data))
    }
}

/**
 * 500 POST 请求
 */
const postTest1 = async () => {
    let uri = '/msServiceTest/postTest1'
    let tags = ''
    let header = {'debug-mode': 'true'};
    let paramMap = {}
    paramMap['param1'] = 'param1'
    paramMap['param2'] = 'param2'
    let response = await serviceClient.post(`${msId}.${uri}`, version, tags, header, paramMap, {}, authType, MsClient.RequestBodyType.WWW_FORM).catch(err => {
        console.log("postTest1=============>" + err)
    })
    if (response) {
        console.log("postTest1=============>" + JSON.stringify(response.data))
    }
}

/**
 *  POST JSON 请求
 */
const postTest2 = async () => {
    let uri = '/msServiceTest/postTest2'
    let tags = ''
    let header = {'debug-mode': 'true'};
    let paramMap = {}
    let bodyObj = {'param1': 'param1', 'param2': 'param2'}
    let response = await serviceClient.post(`${msId}.${uri}`, version, tags, header, paramMap, bodyObj, authType, MsClient.RequestBodyType.RAW_JSON).catch(err => {
        console.log("postTest2=============>" + err);
    });
    if (response) {
        console.log("postTest2=============>" + JSON.stringify(response.data))
    }
}

/**
 *  POST JSON 请求
 */
const postTest3 = async () => {
    let uri = '/msServiceTest/postTest3'
    let tags = ''
    let header = {'debug-mode': 'true'};
    let paramMap = {};
    paramMap['param1'] = 'param1'
    paramMap['param2'] = 'param2'
    paramMap['param3.p1'] = 'param3.p1'
    paramMap['param3.p2'] = 'param3.p2'

    let response = await serviceClient.post(`${msId}.${uri}`, version, tags, header, paramMap, {}, authType, MsClient.RequestBodyType.WWW_FORM).catch(err => {
        console.log("postTest3=============>" + err);
    });
    if (response) {
        console.log("postTest3=============>" + JSON.stringify(response.data))
    }
}

/**
 *  文件上传
 */
const upload = async () => {
    let uri = '/msServiceTest/upload'
    let tags = ''
    let header = {'debug-mode': 'true'};
    let paramMap = {};
    paramMap['param1'] = 'param1'

    let files = new Array<MsClient.MsFile>();
    let file1 = new MsClient.MsFile('file1', fs.createReadStream('d:\\BocompayMerchant.xml'))
    let file2 = new MsClient.MsFile('file2', fs.createReadStream('d:\\BocompayMerchant.xml'))
    files.push(file1)
    files.push(file2)


    let response = await serviceClient.postFiles(`${msId}.${uri}`, version, tags, header, paramMap, files, authType).catch(err => {
        console.log("upload=============>" + err);
    });
    if (response) {
        console.log("upload=============>" + JSON.stringify(response.data))
    }
}


const testUploadFile = async () => {

    let url = 'https://ts.keytop.cn/kt_midend/oss/upload';
    let files = new Array<MsClient.MsFile>();
    let header = {
        'Authorization': 'KT-MIDEND:OTg4OGRkYWNkNThkNGI2ZDlkNDA5Y2IwZThkMjA4M2Q6OTI2Y2M1OTI1YTQxNDc5YmJiNWExZDdmODI4ZWNjNzY=',
        'charset': 'utf-8'
    };

    let readStream = fs.createReadStream('d:\\BocompayMerchant.xml')
    let file1 = new MsClient.MsFile('upload', readStream)
    files.push(file1)

    let httpClient = new MsClient.SimpleHttpUtil(1000)

    let response = await httpClient.uploadFiles(url, {}, files, header).catch(err => {
        console.log("upload=============>" + err);
    });
    if (response) {
        console.log("upload=============>" + JSON.stringify(response.data))
    }

}

getTest1()
getTest3()
postTest1()
postTest2()
postTest3()
upload()