ensoft / entrance / EnTrance.Request

A Request is a client-side representation of a request, that can be manipulated client-side before actually serialising into a JSON message and sending to the server. For example, this:

import EnTrance.Request as Request

Request.new "order_ice_cream"
|> Request.addString "flavour" "chocolate"
|> Request.addBool "with_flake" True

would end up as a JSON request to the server containing

{
    "msg_type": "order_ice_cream",
    "flavour": "chocolate",
    "with_flake": true
}

(Actually, it also adds a "target": "defaultTarget", but you don't have to care about this unless you're writing a particular type of complex app, in which case you want to use Target.set on most requests yourself.)

In the typical case, you would finish with Channel.sendSimpleRpc or Channel.sendRpc, eg:

Request.new "order_ice_cream"
    |> Request.addString "flavour" "chocolate"
    |> Request.addBool "with_flake" True
    |> Channel.sendSimpleRpc model

Sending invokes some extra magic, whereby the actual JSON going to the server would be something like:

{
    "msg_type": "order_ice_cream",
    "flavour": "chocolate",
    "with_flake": true,
    "channel": "my_app",
    "id": <some auto-generated unique identifier>
}

so that the reply gets routed back to the sending channel, and any out-of-order or outdated replies get automatically dropped.

If you add multiple values for the same key, then the most recent value wins. For example:

Request.new "order_ice_cream"
    |> Request.addString "flavour" "chocolate"
    |> Request.addString "flavour" "strawberry"
    |> Request.encode

yields:

{
    "msg_type": "order_ice_cream",
    "flavour": "strawberry"
}

Request type


type Request
    = Request (Dict String Json.Encode.Value)

A request value, that can be built up client-side, before eventually being sent over a channel to the server (typically using Channel.sendSimpleRpc or Channel.sendRpc).

Constructing a Request

new : String -> Request

Create a request Param value containing just a req_type parameter.

(Actually it also creates a default target value, but you don't have to care. See Target if interested.)

addBool : String -> Basics.Bool -> Request -> Request

Add a 'Bool'-valued parameter to a request.

addInt : String -> Basics.Int -> Request -> Request

Add an 'Int'-valued parameter to a request.

addInts : String -> List Basics.Int -> Request -> Request

Add a 'List Int'-valued parameter to a request.

addString : String -> String -> Request -> Request

Add a 'String'-valued parameter to a request.

addStrings : String -> List String -> Request -> Request

Add a 'List String'-valued parameter to a request.

addValue : String -> Json.Encode.Value -> Request -> Request

Add an arbitrary 'Encode.Value' parameter to a request

Using a Request

The most common use of a Request is calling something like Channel.sendRpc with it. But you can also just encode it as JSON if you like.

encode : Request -> Json.Encode.Value

Encode a 'Request' into a JSON value.