Version: 5.2.x

Update Mediator

Update Mediator

To update a mediator, you will need to make a TLS request to the OpenHIM API for the below method and endpoint and supply the JSON object of the mediator.

Method: POST
Endpoint: {openhim_url}:8080/mediators
Payload: JSON object of the mediator

Note: For a mediator to be updated the version number in the update object has to be greater than current one for that mediator. If the mediator does not exist a new one is created.

Update Example

Before we can send our request to the OpenHIM API we need to ensure that we construct our valid HTTP headers to successfully authenticate with the OpenHIM API.

Copy the below code at the bottom of your nodejs script that handles the authentication of the OpenHIM headers as described in the authentication section.

Replace the openhimOptions values with the correct implementation details and supply the SampleData payload to submit.

// append below code to the "openhim-api.js" script containing the authentication methods.
// This is described within the authentication section
(async () => {
const openhimOptions = {
apiURL: 'https://localhost:8080',
apiEndpoint: '/mediators',
username: 'root@openhim.org',
password: 'openhim-password',
rejectUnauthorized: false
}
const SampleData = 'SampleData'
const headers = await genAuthHeaders(openhimOptions)
const options = {
method: 'POST',
url: `${openhimOptions.apiURL}${openhimOptions.apiEndpoint}`,
rejectUnauthorized: openhimOptions.rejectUnauthorized,
headers: headers,
body: SampleData,
json: true
}
request(options, (error, response, body) => {
if (error) throw new Error(error)
console.log({
statusCode: response.statusCode,
body
})
})
})()

Execute the below command in your terminal to run the nodejs script

node openhim-api.js

The response status code will be 201 whether the update succeeds or not. To check the status of the update, fetch the updated mediator or check the console.

Set Mediator configuration

To set a mediator's configuration, you will need to make a TLS request to the OpenHIM API for the below method and endpoint and supply the JSON object of the mediator's configuration.

Method: PUT
Endpoint: {openhim_url}:8080/mediators/:urn/config
Payload: JSON object of the mediator's config

The response will have an http status code of 201 if successful, 404 if the mediator referenced by urn cannot be found and 400 if the config supplied cannot be validated against the configuration definition supplied in the mediator registration message.

This endpoint can only be called by an admin user.

Setting Mediator's configuration Example

Before we can send our request to the OpenHIM API we need to ensure that we construct our valid HTTP headers to successfully authenticate with the OpenHIM API.

Copy the below code at the bottom of your nodejs script that handles the authentication of the OpenHIM headers as described in the authentication section.

Replace the openhimOptions values with the correct implementation details and supply the SampleData payload to submit.

// append below code to the "openhim-api.js" script containing the authentication methods.
// This is described within the authentication section
(async () => {
const openhimOptions = {
apiURL: 'https://localhost:8080',
apiEndpoint: '/mediators/:urn/config',
username: 'root@openhim.org',
password: 'openhim-password',
rejectUnauthorized: false
}
const SampleData = 'SampleData'
const headers = await genAuthHeaders(openhimOptions)
const options = {
method: 'PUT',
url: `${openhimOptions.apiURL}${openhimOptions.apiEndpoint}`,
rejectUnauthorized: openhimOptions.rejectUnauthorized,
headers: headers,
body: SampleData,
json: true
}
request(options, (error, response, body) => {
if (error) throw new Error(error)
console.log({
statusCode: response.statusCode,
body
})
})
})()

Execute the below command in your terminal to run the nodejs script

node openhim-api.js

The response status code will be 200 if successful.