I’ve not done it myself, but it looks like you would need to create a Http.Resolver to check the status code of the Http.Response from your first request, and another Http.Resolver for the refresh token request. The trick would be to write these resolvers in such a way that they “pass along” the details of the initial request, so that you can then reissue it if you need to.
You can then use Http.task to turn the requests in to tasks and Task.onError to conditionally run the refresh request (which itself should be chained with Task.andThen to rerun the initial request on success). I guess you’d also need some way to avoid infinite loops if for some reason the refresh succeeds but the initial request still fails, so you would maybe also want to pass some kind of retry count along through the custom resolvers.
I’m afraid I don’t have time right now to try this myself, so I may have missed some details. I should point out that if you search the package database there are also some existing OAuth packages for elm out there…
I think the short answer is that in case you want to persist new state like new token you need to do so via update function as there is no other way to update the model in tea.
The second question is how would you go about it. And answer to that is that there are different ways with different tradeoffs. I would personally suggest use something you’re familiar with. It can be anything on a scale from the most simple function String -> Model -> Model to state monad. Passing functions in Msgs or avoiding that.