nwaku/waku/common/utils/matterbridge_client.nim
Ivan FB fd6a71cdd7
chore: Bump dependencies for v0.31.0 (#2885)
* bump_dependencies.md: add nim-results dependency
* change imports stew/results to results
* switching to Nim 2.0.8
* waku.nimble: reflect the requirement nim 1.6.0 to 2.0.8
  Adding --mm:refc as nim 2.0 enables a new garbage collector that we're
  not yet ready to support
* adapt waku code to Nim 2.0
* gcsafe adaptations because Nim 2.0 is more strict
2024-07-09 13:14:28 +02:00

75 lines
2.0 KiB
Nim

{.push raises: [].}
import std/[httpclient, json, uri, options], results
const
# Resource locators
stream* = "/api/stream"
messages* = "/api/messages"
message* = "/api/message"
health* = "/api/health"
type
MatterbridgeResult[T] = Result[T, string]
MatterbridgeClient* = ref object of RootObj
hostClient*: HttpClient
host*: Uri
gateway*: string
proc new*(
T: type MatterbridgeClient, hostUri: string, gateway = "gateway1"
): MatterbridgeClient {.raises: [Defect, KeyError].} =
let mbClient = MatterbridgeClient()
mbClient.hostClient = newHttpClient()
mbClient.hostClient.headers = newHttpHeaders({"Content-Type": "application/json"})
mbClient.host = parseUri(hostUri)
mbClient.gateway = gateway
return mbClient
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)
assert response.status == "200 OK"
ok(msgs)
proc postMessage*(mb: MatterbridgeClient, msg: JsonNode): MatterbridgeResult[bool] =
var response: Response
try:
response =
mb.hostClient.request($(mb.host / message), httpMethod = HttpPost, body = $msg)
except Exception as e:
return err("post request failed: " & e.msg)
ok(response.status == "200 OK")
proc postMessage*(
mb: MatterbridgeClient, text: string, username: string
): MatterbridgeResult[bool] =
let jsonNode = %*{"text": text, "username": username, "gateway": mb.gateway}
return mb.postMessage(jsonNode)
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)
ok(response.status == "200 OK" and healthOk)