"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiRequest = void 0;
const request_1 = require("@ubio/request");
const inversify_1 = require("inversify");
const config_js_1 = require("../../config.js");
const AC_API_URL = (0, config_js_1.stringConfig)('AC_API_URL', 'https://api.automationcloud.net');
const AC_API_TOKEN_URL = (0, config_js_1.stringConfig)('AC_API_TOKEN_URL', '');
const AC_API_CLIENT_ID = (0, config_js_1.stringConfig)('AC_API_CLIENT_ID', '');
const AC_API_CLIENT_KEY = (0, config_js_1.stringConfig)('AC_API_CLIENT_KEY', '');
let ApiRequest = class ApiRequest {
    constructor(config) {
        this.config = config;
        this.request = null;
    }
    getRequest() {
        if (this.request == null) {
            this.request = this.createRequest();
        }
        return this.request;
    }
    createAuthAgent() {
        return new request_1.OAuth2Agent({
            tokenUrl: this.config.get(AC_API_TOKEN_URL),
            clientId: this.config.get(AC_API_CLIENT_ID),
            clientSecret: this.config.get(AC_API_CLIENT_KEY),
        });
    }
    createRequest() {
        return new request_1.Request({
            baseUrl: this.config.get(AC_API_URL),
            auth: this.createAuthAgent(),
        });
    }
    get(url, options = {}) {
        return this.getRequest().get(url, options);
    }
    post(url, options = {}) {
        return this.getRequest().post(url, options);
    }
    put(url, options = {}) {
        return this.getRequest().put(url, options);
    }
    delete(url, options = {}) {
        return this.getRequest().delete(url, options);
    }
    send(method, url, options = {}) {
        return this.getRequest().send(method, url, options);
    }
    getBlob(url, contentType, options = {}) {
        return this.getRequest().getBlob(url, contentType, options);
    }
};
ApiRequest = __decorate([
    (0, inversify_1.injectable)(),
    __param(0, (0, inversify_1.inject)(config_js_1.Configuration)),
    __metadata("design:paramtypes", [config_js_1.Configuration])
], ApiRequest);
exports.ApiRequest = ApiRequest;
//# sourceMappingURL=api-request.js.map