"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const WizError_1 = require("../WizError");
class ServiceRequest {
    constructor(request, name = '', url = '') {
        this.request = request;
        this.name = name;
        this.url = url;
    }
    get(url, data) {
        return this.getData(this.request.get(url, data));
    }
    post(url, data) {
        return this.getData(this.request.post(url, data));
    }
    put(url, data) {
        return this.getData(this.request.put(url, data));
    }
    patch(url, data) {
        return this.getData(this.request.patch(url, data));
    }
    delete(url, data) {
        return this.getData(this.request.delete(url, data));
    }
    download(url, fileName, data) {
        return this.request.download(url, fileName, data).catch(this.handleError.bind(this));
    }
    getData(request) {
        return request
            .then(result => result.data)
            .catch(this.handleError.bind(this));
    }
    handleError(err) {
        if (err.code === 'SERVER_ERROR' || err.code === 'SERVER_UNAVAILABLE') {
            throw new WizError_1.WizError('SERVICE_UNAVAILABLE', this.name, this.url);
        }
        if (err.code === 'NOT_FOUND' || err.code === 'METHOD_NOT_ALLOWED') {
            throw new WizError_1.WizError('SERVICE_NOT_FOUND', this.name, this.url);
        }
        throw err;
    }
}
exports.default = ServiceRequest;
//# sourceMappingURL=index.js.map