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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import Validator from 'swagger-model-validator';
import axios from 'axios';
/**
* Handle authentication and send/receive with the backbone
*/
export class Adapter {
constructor(protocol, config) {
this.protocol = protocol;
this.config = config;
this.axios = axios;
this.validator = new Validator(protocol.schema);
this.auth();
}
/**
* Test parsing the message based on the provided protocol schema
*
* The message must be successfully json decoded into an object
* prior to validation
*
* At present this returns the validation result which is an
* object containing a boolean valid field as well as details
* of any errors.
* @param {object} message
* @returns {object}
*/
validate(message) {
return this.validator.validate(
message,
this.protocol.schema.definitions.Message
);
}
/**
* Ensure the token in this.credentials is still valid
* @returns {boolean}
*/
tokenValid() {
let valid = false;
if (this.credentials) {
const now = new Date().toISOString();
valid = this.credentials.expiry > now;
}
return valid;
}
/**
* Return an http headers object containing a bearer token authorisation header
* @returns {object}
*/
getAuthorizationHeader() {
if (!this.tokenValid())
return this.auth().then((response) => {
return {
Authorization: `Bearer ${this.credentials.token}`,
};
});
else {
return Promise.resolve({
Authorization: `Bearer ${this.credentials.token}`,
});
}
}
/**
* Do a client credentials grant and store in this.credentials
* @returns {object}
*/
auth() {
let adapterConfig = this.config;
console.error('config', adapterConfig);
return this.axios
.get(`${adapterConfig.api}/token`, {
params: {
client_id: adapterConfig.client_id,
secret: adapterConfig.secret,
},
})
.then((response) => {
this.credentials = response.data;
return response;
});
}
/**
* Call the GET /receive endpoint and process the messages with decode
*
* Returns the response
* @returns {object}
*/
poll() {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
return this.axios.get(`${adapterConfig.api}/receive`, {
headers,
});
})
.then((response) => {
response.data.forEach((message) => {
const parsed = JSON.parse(message.message);
const validation = this.validate(parsed);
if (validation.valid) {
const type = this.protocol.getType(parsed);
this.protocol.decode(type, parsed);
}
});
return response;
});
}
/**
* Publish a message to the backbone with the specified topic
*
* Messages should be passed through encode before sending
* @param {string} topic
* @param {string} body
* @returns
*/
publish(topic, body) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
return this.axios.post(
`${adapterConfig.api}/send`,
{
topic,
body,
},
{
headers,
}
);
})
.then((response) => {
return response;
});
}
/**
* Broadcast the message on the backbone
*
* Broadcast messages bypass the normal publisher queues
* this means they can be used to deliver messages more
* quickly in an emergency scenario.
*
* Messages should be passed through encode before sending
* @param {*} body
* @returns
*/
broadcast(body) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
return this.axios.post(
`${adapterConfig.api}/notify`,
{
body,
},
{
headers,
}
);
})
.then((response) => {
return response;
});
}
}