Reputation: 4719
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?
Upvotes: 2
Views: 249
Reputation: 2923
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
).
Upvotes: 4
Reputation: 21005
I'm not really sure what elm-playground is, but that's not the right starting point for a webapp such as you want to create, as it does not support Commands, such as Http requests.
You want to be using normal / standard Elm - see https://guide.elm-lang.org/install/elm.html and then you want to be building a Program
based on Browser.document
- https://guide.elm-lang.org/webapps/
Hope that gets you on your way
Upvotes: 0