Explorer

definy.sync-remote-rpc

description
Call an EventService RPC of another definy server with http_fetch
type
Fn(
  remote: String,
  method: String,
  request: @definy.json,
  then: Fn(result: Union{#ok(@definy.json), #error(String)}) -> @definy.service-io
) -> @definy.service-io
expression
fn(remote$0, method$1, request$2, then$3) =>
  #handle<@definy.service-io>({
    request: @http.request-of-url(
      method: "POST",
      url: @std.string-join(items: [$0, "/definy.v1.EventService/", $1], separator: ""),
      headers: [{name: "Content-Type", value: "application/json"}],
      body: @core.string-to-bytes(value: @definy.json-to-string(value: $2))
    ),
    secret_headers: [],
    then: fn(result$4) =>
      match $4 {
        #error(error$5) =>
          $3(
            result: #error(@std.string-join(items: ["Could not reach ", $0, ": ", @http.error-text(error: $5)], separator: ""))
          ),
        #ok(response$6) =>
          let text$7 = @http.body-text(response: $6);
          match @core.equal(left: $6.status, right: 200) {
            true =>
              match @definy.parse-json(text: $7) {
                #ok(json$9) =>
                  $3(result: #ok($9)),
                #error(message$10) =>
                  $3(
                    result: #error(@std.string-join(items: [$1, " returned a body that is not JSON: ", $10], separator: ""))
                  )
              },
            false =>
              $3(
                result: #error(@std.string-join(
                  items: [$1, " failed (status ", @std.number-to-string(value: $6.status), "): ", $7],
                  separator: ""
                ))
              )
          }
      }
  })
visibility
public
definy.sync-remote-rpc (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: o45u5G4TfLVdLeAP8CnjHgXM2J_bbgCAIeNYhza11E0
content hash: nFYpz34j4qidaZxvykuIruNNfoiiJGhQyx-QhopkmqU