(evaluation, requestId, request)
| 11 | export class HttpDatabase extends Database { |
| 12 | |
| 13 | sendRequest(evaluation, requestId, request) { |
| 14 | let options: any = {url: request.url[0], headers: {}}; |
| 15 | if(request.headers) { |
| 16 | let headers = this.index.asObject(request.headers[0]); |
| 17 | for(let header in headers) { |
| 18 | options.headers[header] = headers[header]; |
| 19 | } |
| 20 | } |
| 21 | if(request.method) { |
| 22 | options.method = request.method[0]; |
| 23 | } |
| 24 | if(request.json) { |
| 25 | let object = this.index.asObject(request.json[0], true, true); |
| 26 | options.headers["Content-Type"] = "application/json"; |
| 27 | options.body = JSON.stringify(object); |
| 28 | } |
| 29 | if(request.body) { |
| 30 | options.body = request.body[0]; |
| 31 | } |
| 32 | httpRequest(options, (error, response, body) => { |
| 33 | if(error || !response) { |
| 34 | // @TODO: expose errors and other weirdness into Eve instead of just bailing here. |
| 35 | console.error(error || "ERROR: No response received from HTTP request"); |
| 36 | return; |
| 37 | } |
| 38 | let scope = "http"; |
| 39 | let responseId = `${requestId}|response`; |
| 40 | let changes = evaluation.createChanges(); |
| 41 | changes.store(scope, requestId, "response", responseId, this.id); |
| 42 | changes.store(scope, responseId, "tag", "response", this.id); |
| 43 | let contentType = response.headers["content-type"]; |
| 44 | if(contentType && contentType.indexOf("application/json") > -1) { |
| 45 | let id = eavs.fromJS(changes, JSON.parse(body), this.id, scope, `${responseId}|json`); |
| 46 | changes.store(scope, responseId, "json", id, this.id); |
| 47 | } |
| 48 | changes.store(scope, responseId, "body", body, this.id); |
| 49 | evaluation.executeActions([], changes); |
| 50 | }) |
| 51 | } |
| 52 | |
| 53 | onFixpoint(evaluation: Evaluation, changes: Changes) { |
| 54 | let name = evaluation.databaseToName(this); |
no test coverage detected