Search code examples
openapielmopenapi-generator

How to make a http request in a playground update function?


I want to make http requests from my elm program. I use the openapi-generator https://eriktim.github.io/openapi-elm for the http requests: https://github.com/eriktim/openapi-elm, The only example I could find is this:
https://github.com/eriktim/openapi-elm/tree/master/example
There, a request has e.g. type Api.Request Api.Data.PlanetList and is converted with the send function: (Result Http.Error a -> msg) -> Request a -> Cmd msg.
The send function takes a function to convert the Request result to msg but but returns it wrapped in Cmd. The update function has type update : Msg -> Model -> ( Model, Cmd Msg ) So as long as the request is made in the update function and the result is put in the return value the framework will get msg out of Cmd.

Now I want to make requests in my program, but I'm using playground game as my main function (example) where the update function is update : Computer -> Model -> Model so the "trick" from the example project is not applicable. How can I still get the values from my request call then?


Solution

  • A Http request is a piece of data for the runtime to execute. If the Cmd is not passed to the runtime through the main update, the actual http call will never happen.

    This is why you cannot have side-effects in simple programs (Playground and Browser.sandbox).