"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// This file is auto-generated, don't edit it
const gateway_spi_1 = __importDefault(require("@alicloud/gateway-spi"));
const tea_util_1 = __importDefault(require("@alicloud/tea-util"));
const $tea = __importStar(require("@alicloud/tea-typescript"));
class Client extends gateway_spi_1.default {
    constructor() {
        super();
    }
    async modifyConfiguration(context, attributeMap) {
    }
    async modifyRequest(context, attributeMap) {
        let request = context.request;
        let config = context.configuration;
        request.headers = Object.assign({ host: config.endpoint, 'user-agent': request.userAgent, accept: "application/json" }, request.headers);
        if (!tea_util_1.default.isUnset(request.body)) {
            let jsonObj = tea_util_1.default.toJSONString(request.body);
            request.stream = new $tea.BytesReadable(jsonObj);
            request.headers["content-type"] = "application/json; charset=utf-8";
        }
    }
    async modifyResponse(context, attributeMap) {
        let request = context.request;
        let response = context.response;
        if (tea_util_1.default.is4xx(response.statusCode) || tea_util_1.default.is5xx(response.statusCode)) {
            let _res = await tea_util_1.default.readAsJSON(response.body);
            let err = tea_util_1.default.assertAsMap(_res);
            err["statusCode"] = response.statusCode;
            throw $tea.newError({
                code: `${this.defaultAny(err["Code"], err["code"])}`,
                message: `code: ${response.statusCode}, ${this.defaultAny(err["Message"], err["message"])} request id: ${this.defaultAny(err["RequestId"], err["requestid"])}`,
                data: err,
                description: `${this.defaultAny(err["Description"], err["description"])}`,
                accessDeniedDetail: this.defaultAny(err["AccessDeniedDetail"], err["accessdenieddetail"]),
            });
        }
        if (tea_util_1.default.equalNumber(response.statusCode, 204)) {
            await tea_util_1.default.readAsString(response.body);
        }
        else if (tea_util_1.default.equalString(request.bodyType, "binary")) {
            response.deserializedBody = response.body;
        }
        else if (tea_util_1.default.equalString(request.bodyType, "byte")) {
            let byt = await tea_util_1.default.readAsBytes(response.body);
            response.deserializedBody = byt;
        }
        else if (tea_util_1.default.equalString(request.bodyType, "string")) {
            let str = await tea_util_1.default.readAsString(response.body);
            response.deserializedBody = str;
        }
        else if (tea_util_1.default.equalString(request.bodyType, "json")) {
            let obj = await tea_util_1.default.readAsJSON(response.body);
            let res = tea_util_1.default.assertAsMap(obj);
            response.deserializedBody = res;
        }
        else if (tea_util_1.default.equalString(request.bodyType, "array")) {
            let arr = await tea_util_1.default.readAsJSON(response.body);
            response.deserializedBody = arr;
        }
        else {
            response.deserializedBody = await tea_util_1.default.readAsString(response.body);
        }
    }
    defaultAny(inputValue, defaultValue) {
        if (tea_util_1.default.isUnset(inputValue)) {
            return defaultValue;
        }
        return inputValue;
    }
}
exports.default = Client;
//# sourceMappingURL=client.js.map