jarvism
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!
Trending in Questions
Other Trending Topics
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #ai
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
derek-zhou
You need to keep in mind that:
So you need to pass the pid of the LV process into the task, then send the token back as part of a message, then have a handle_info() at the LV process to update the socket.
al2o3cr
The
Task.awaitbelow will get the value returned by this code, which right now is the return value ofIO.puts. You likely want:That way the
Task.awaitreceives the token.I’d recommend keeping the “get a token” and “put that token into
assigns” parts separate; having a side-effecting function namedget_*seems odd. So you’d store the token intosocketwherever is callingget_token.Mostly-unrelated question: what’s the purpose of the
Task.asyncfollowed immediately by aTask.await? Is it for the 5s timeout?