Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RestAdapter = void 0; const axios_1 = __importDefault(require("axios")); const BadRequestError_1 = require("./BadRequestError"); class RestAdapter { constructor(config = {}, options = {}, cliVersion = 'unknown', agentId = 'unknown') { this.config = config; this.options = options; this.cliVersion = cliVersion; this.agentId = agentId; } setConfig(config) { this.config = config || {}; } async request(httpRequest, client, retries = 0) { let axiosResponse; try { const cliInfoHeader = `cognigy-cli/${this.cliVersion} project/${this.agentId}`; httpRequest.headers = { ...httpRequest.headers, 'X-Cognigy-Client-Info': cliInfoHeader, }; if (httpRequest.withAuthentication) { const authenticationHeaders = await client.authenticationHandler?.getAuthenticationHeaders(); httpRequest.headers = { ...httpRequest.headers, ...authenticationHeaders, }; } const axiosRequest = await this.convertRequest(httpRequest, client); axiosResponse = (await (0, axios_1.default)(axiosRequest)); } catch (err) { if (this.options.retries && retries < this.options.retries) { return this.request(httpRequest, client, retries + 1); } else { throw err; } } if (axiosResponse.status >= 400) { if (this.options.retries && retries < this.options.retries && axiosResponse.status >= 500) { return this.request(httpRequest, client, retries + 1); } else { this.handleError(axiosResponse); } } const response = await this.convertResponse(axiosResponse); return response; } async get(request, client) { return this.request({ ...request, method: 'GET' }, client); } async post(request, client) { return this.request({ ...request, method: 'POST' }, client); } async patch(request, client) { return this.request({ ...request, method: 'PATCH' }, client); } async put(request, client) { return this.request({ ...request, method: 'PUT' }, client); } async head(request, client) { return this.request({ ...request, method: 'HEAD' }, client); } async convertRequest(request, client) { const baseUrl = request.baseUrl || this.config.baseUrl; const method = request.method || 'GET'; const axiosRequest = { headers: request.headers, method, url: `${baseUrl}${request.url}`, validateStatus: null, }; if (method !== 'GET' && method !== 'HEAD') { axiosRequest.data = request.data; } else if (request.data) { const stack = new Error().stack; console.warn(`Warning: RestAdapter: ${method} request to ${request.url} includes body data which will be ignored. ` + `This may indicate an issue with the API client implementation. ` + `Stack trace: ${stack}`); } return axiosRequest; } handleError(axiosResponse) { const data = axiosResponse.data; throw new BadRequestError_1.BadRequestError(data.title || data.error, data.detail || data.error_description, data.status, data.details); } async convertResponse(axiosResponse) { const response = { data: axiosResponse.data, headers: axiosResponse.headers, status: axiosResponse.status, statusText: axiosResponse.statusText, }; return response; } } exports.RestAdapter = RestAdapter; |