jarvism

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!

Showing Posts 1 to 2

derek-zhou

derek-zhou

You need to keep in mind that:

  • All values are immutable
  • 2 processes share nothing, such as the LV process and the get_token_task

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

al2o3cr

The Task.await below will get the value returned by this code, which right now is the return value of IO.puts. You likely want:

          _ ->
            IO.puts("TOKEN RECEIVED")
            {:ok, token}

That way the Task.await receives the token.

I’d recommend keeping the “get a token” and “put that token into assigns” parts separate; having a side-effecting function named get_* seems odd. So you’d store the token into socket wherever is calling get_token.


Mostly-unrelated question: what’s the purpose of the Task.async followed immediately by a Task.await? Is it for the 5s timeout?

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews