2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2021-07-16 15:13:36 +00:00
|
|
|
|
2024-07-09 11:14:28 +00:00
|
|
|
import std/[httpclient, json, uri, options], results
|
2021-05-06 13:43:43 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
# Resource locators
|
|
|
|
stream* = "/api/stream"
|
|
|
|
messages* = "/api/messages"
|
|
|
|
message* = "/api/message"
|
|
|
|
health* = "/api/health"
|
|
|
|
|
|
|
|
type
|
2021-07-16 15:13:36 +00:00
|
|
|
MatterbridgeResult[T] = Result[T, string]
|
|
|
|
|
2021-05-06 13:43:43 +00:00
|
|
|
MatterbridgeClient* = ref object of RootObj
|
|
|
|
hostClient*: HttpClient
|
|
|
|
host*: Uri
|
|
|
|
gateway*: string
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc new*(
|
|
|
|
T: type MatterbridgeClient, hostUri: string, gateway = "gateway1"
|
|
|
|
): MatterbridgeClient {.raises: [Defect, KeyError].} =
|
2021-05-06 13:43:43 +00:00
|
|
|
let mbClient = MatterbridgeClient()
|
|
|
|
|
|
|
|
mbClient.hostClient = newHttpClient()
|
2024-03-15 23:08:47 +00:00
|
|
|
mbClient.hostClient.headers = newHttpHeaders({"Content-Type": "application/json"})
|
|
|
|
|
2021-05-06 13:43:43 +00:00
|
|
|
mbClient.host = parseUri(hostUri)
|
|
|
|
mbClient.gateway = gateway
|
|
|
|
|
|
|
|
return mbClient
|
|
|
|
|
2021-07-16 15:13:36 +00:00
|
|
|
proc getMessages*(mb: MatterbridgeClient): MatterbridgeResult[seq[JsonNode]] =
|
|
|
|
var
|
|
|
|
response: Response
|
|
|
|
msgs: seq[JsonNode]
|
|
|
|
try:
|
|
|
|
response = mb.hostClient.get($(mb.host / messages))
|
|
|
|
msgs = parseJson(response.body()).getElems()
|
|
|
|
except Exception as e:
|
|
|
|
return err("failed to get messages: " & e.msg)
|
2021-05-06 13:43:43 +00:00
|
|
|
|
2021-07-16 15:13:36 +00:00
|
|
|
assert response.status == "200 OK"
|
2021-05-06 13:43:43 +00:00
|
|
|
|
2021-07-16 15:13:36 +00:00
|
|
|
ok(msgs)
|
2021-05-06 13:43:43 +00:00
|
|
|
|
2021-07-16 15:13:36 +00:00
|
|
|
proc postMessage*(mb: MatterbridgeClient, msg: JsonNode): MatterbridgeResult[bool] =
|
|
|
|
var response: Response
|
|
|
|
try:
|
2024-03-15 23:08:47 +00:00
|
|
|
response =
|
|
|
|
mb.hostClient.request($(mb.host / message), httpMethod = HttpPost, body = $msg)
|
2021-07-16 15:13:36 +00:00
|
|
|
except Exception as e:
|
|
|
|
return err("post request failed: " & e.msg)
|
2021-05-06 13:43:43 +00:00
|
|
|
|
2021-07-16 15:13:36 +00:00
|
|
|
ok(response.status == "200 OK")
|
2021-05-06 13:43:43 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc postMessage*(
|
|
|
|
mb: MatterbridgeClient, text: string, username: string
|
|
|
|
): MatterbridgeResult[bool] =
|
|
|
|
let jsonNode = %*{"text": text, "username": username, "gateway": mb.gateway}
|
2021-05-06 13:43:43 +00:00
|
|
|
|
2021-07-16 15:13:36 +00:00
|
|
|
return mb.postMessage(jsonNode)
|
2021-05-06 13:43:43 +00:00
|
|
|
|
2021-07-16 15:13:36 +00:00
|
|
|
proc isHealthy*(mb: MatterbridgeClient): MatterbridgeResult[bool] =
|
|
|
|
var
|
|
|
|
response: Response
|
|
|
|
healthOk: bool
|
|
|
|
try:
|
|
|
|
response = mb.hostClient.get($(mb.host / health))
|
|
|
|
healthOk = response.body == "OK"
|
|
|
|
except Exception as e:
|
|
|
|
return err("failed to get health: " & e.msg)
|
2021-05-06 13:43:43 +00:00
|
|
|
|
2021-07-16 15:13:36 +00:00
|
|
|
ok(response.status == "200 OK" and healthOk)
|