Hello!
I have a LiveView that uploads a CSV, processes it with some information from external APIs, and creates a download.
To use the external APIs, I have to get a token. The token expires eventually, and I have code to catch the response and get a new token.
I’m really failing at adding the token to the state and making it available for future calls.
I know I could persist the token in a DB, but this seems a little heavy. I have been trying to add the token to socket assigns (so generally available in the app), but I can’t seem to make it work.
The user clicks a button, which starts handle_event(“process”). If one of the API calls fails a certain way, I have a def get_token() function that gets a new token.
defp get_token(base_url, api_key, socket) do
IO.puts("GETTING TOKEN")
get_token_task =
Task.async(fn ->
resp =
Req.post!("#{base_url}/api/v1/1/prod/get_token", json: %{api_key: api_key})
error = resp.body["err"]
token = resp.body["resp"]["SecurityToken"]
case error do
error when error == true ->
{:error, error}
_ ->
{:ok, token}
IO.puts("TOKEN RECEIVED")
######################################
# What am I supposed to do with it?
######################################
end
end)
Task.await(get_token_task)
end
Is there really no way to just add the token to assigns? I have tried every method I could find, including just assigning, {:noreply, assign(socket, token: token)}, send self() + handle_info()…
What’s the right way to do this?
It does not need to be inside the Task. I’m just returning the token now and passing that around. The problem is how to update the value from within a function.
Is there some other way to persist state in the LiveView that I don’t know about yet? I feel like I’m missing something pretty basic here!
Looking forward to other people’s input.
Thank you in advance!