andreasewering/elm-grpc/1.0.0

NOTE: the latest version is 1.1.1

Renamed to anmolitor/elm-grpc. It is published under that name, and new versions come out there.

Versions published as andreasewering/elm-grpc still install under that name. 5 versions of 6 come from the new location (see their notes).

Rerouted (renamed, needs approval). The GitHub user or repository was renamed. The tag is still there, at the new name. The Lamdera package server serves a copy recovered from https://github.com/anmolitor/elm-grpc.

What we checked: The repo moved to github.com/anmolitor/elm-grpc and tag 1.0.0 is there; its commit afe3f65d2a8c is from 2023-12-19 11:34 UTC, after the version was published (2023-03-02 08:25 UTC); elm.json differs from package.elm-lang.org's; the exposed modules match docs.json; the zip's SHA-1 is not the published one and nothing else confirms its contents.

We could not verify this copy against what was published, so the Lamdera compiler asks before using it (y/n). Without a terminal, as in CI, it stops with an error unless approved:

  • lamdera make --auto-approve-package-rerouting approves rerouted versions for that run,
  • LAMDERA_AUTO_APPROVE_PACKAGE_REROUTING=1 does the same for every run in that environment,
  • LAMDERA_CACHE=off skips the Lamdera package server altogether: packages come from package.elm-lang.org and GitHub only, as with elm, and a rerouted version then fails to download.

Details · package.elm-lang.org's zip: https://github.com/andreasewering/elm-grpc/zipball/1.0.0/

Grpc

This is the public API for gRPC application usage. It is intended to be imported qualified.

Main Types

type alias Rpc req res =
Rpc req res

Opaque type for an rpc endpoint. These should be constructed by the protoc-gen-elm code generator. If for some reason, you need to define an endpoint by hand, you can do so by importing the Grpc.Internal module.

type alias RpcRequest req res =
InternalRpcRequest req res Never

A request to the server that has not been sent yet. This is designed to be used in a builder-pattern kind of way:

Grpc.new helloWorld "hello"
    |> Grpc.addHeader "authorization" token
    |> Grpc.setHost "http://example.com"


In this example `helloWorld` could be a value of type `Rpc String String` that was generated by `protoc-gen-elm`.
new : Rpc req res -> req -> RpcRequest req res

Build a request from an rpc endpoint by providing a request body.

Configuring the request

String
-> String
-> InternalRpcRequest req res usesTracker
-> InternalRpcRequest req res usesTracker

Add a header to a request. The first parameter is the header name, the second parameter is the value.

List ( String, String )
-> InternalRpcRequest req res usesTracker
-> InternalRpcRequest req res usesTracker

Add multiple headers at once to a request.

String
-> InternalRpcRequest req res usesTracker
-> InternalRpcRequest req res usesTracker

Set the host for the request. By default the host is empty (i.e. the current website), which is recommended to avoid CORS issues.

Float
-> InternalRpcRequest req res usesTracker
-> InternalRpcRequest req res usesTracker

Make the request cancel itself after a given timeout in milliseconds.

String
-> InternalRpcRequest req res usesTracker
-> InternalRpcRequest req res ()

The tracker lets you cancel and track requests via Http.cancel and Http.track. This only works if you use toCmd to send your Request, Tasks unfortunately do not support this. We use the type system to our advantage here to prevent you from calling toTask after using this function.

Sending the request

toTask : RpcRequest req res -> Task Error res

Convert the given RpcRequest into a Task. This can be useful if you want to chain multiple requests but are only interested in the end result.

(Result Error res -> msg)
-> InternalRpcRequest req res usesTracker
-> Cmd msg

Convert the given RpcRequest into a Command to make the Elm runtime execute the request.

Handling Errors

type Error
= BadUrl String
| Timeout
| NetworkError
| BadStatus
{ metadata : Metadata
, response : Bytes
, errMessage : String
, status : GrpcStatus
}
| BadBody Bytes
| UnknownGrpcStatus String

A gRPC request may fail with in a variety of ways. Since the request is just a HTTP request under the hood, it may fail in the roughly same ways a HTTP request could.

  • BadUrl means you did not provide a valid URL.

  • Timeout means it took too long to get a response.

  • NetworkError means the user turned off their wifi, went in a cave, etc.

  • BadStatus means you got a response back, but the http status code indicates failure. This can be either a bad HTTP status code or a bad grpc-status response header according to https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md

  • BadBody means you got a response back with a nice status code, but the body of the response was something unexpected. The Bytes in this case is the response that failed to be parsed.

  • UnknownGrpcStatus means you got a response back but the grpc-status header was set to either

    • something that is not an Int
    • an Int that is outside of the range of the ErrCode enum.

    Note that not setting the header is fine and will delegate failure checking to the HTTP status.

= Ok_
| Cancelled
| Unknown
| InvalidArgument
| DeadlineExceeded
| NotFound
| AlreadyExists
| PermissionDenied
| ResourceExhausted
| FailedPrecondition
| Aborted
| OutOfRange
| Unimplemented
| Internal
| Unavailable
| DataLoss
| Unauthenticated

A valid grpc-status header encodes one of these possibilities as an Int.

  • 0 -> Ok_
  • 1 -> Cancelled
  • 2 -> Unknown
  • ...