-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclients.js
117 lines (94 loc) · 3.25 KB
/
clients.js
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
109
110
111
112
113
114
115
116
117
const axios = require("axios").default;
const fs = require("fs");
const url = require("url");
const pRetry = require("p-retry");
const host = process.env["TARGET_HOST"] || "localhost";
// ====================== FHIR via OpenHim =====================================
const openHimFhirBaseUrl = `http://${host}:5001/fhir`;
// HAPI FHIR via OpenHIM Client
const openHimFhir = axios.create({
baseURL: openHimFhirBaseUrl,
headers: { "Content-Type": "application/json", Authorization: "Custom test" },
responseType: "json",
});
// curl --location --request POST 'http://localhost:3447/fhir/Organization' \
// --header 'Content-Type: application/json' \
// --header 'If-None-Exist: identifier=Gastro' \
// --data-binary '@fixtures/organization.json'
function postOrganization(data, uniqueIdentifier) {
const headers = uniqueIdentifier ? { "If-None-Exist": uniqueIdentifier } : {};
return openHimFhir
.post("Organization", data, { headers })
.catch(warnVersionConflict)
.catch(verboseCatch);
}
function postPatient(data, uniqueIdentifier) {
const headers = uniqueIdentifier ? { "If-None-Exist": uniqueIdentifier } : {};
return openHimFhir
.post("Patient", data, { headers })
.catch(warnVersionConflict)
.catch(verboseCatch);
}
function getPatients(search) {
const params = new url.URLSearchParams(search);
return openHimFhir.get(`Patient?${params.toString()}`).catch(verboseCatch);
}
// ====================== OpenFn Microservice ==================================
const microserviceBaseUrl = `http://${host}:4001/`;
// HAPI FHIR via OpenHIM Client
const microservice = axios.create({
baseURL: microserviceBaseUrl,
headers: { "Content-Type": "application/json" },
responseType: "json",
});
async function postToInbox(body) {
return await microservice.post("inbox", body).catch(verboseCatch);
}
async function isInboxAvailable() {
function getIndex() {
return microservice.get("");
}
const result = await pRetry(getIndex, {
onFailedAttempt: (error) => {
console.log(
`Attempt ${error.attemptNumber}/10 failed.`
);
},
retries: 10,
});
}
// ============================ Utils ==========================================
function verboseCatch(error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error", error.message);
}
console.log(error.config);
throw error;
}
function warnVersionConflict(error) {
if (error.response && error.response.status == 412) {
console.warn("⚠ Got 412, already exists.");
return error.response;
}
throw error;
}
// ============================ FHIR Methods ===================================
module.exports = {
postOrganization,
postPatient,
getPatients,
postToInbox,
isInboxAvailable,
};