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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
mohsen
I’m using an Umbrella project for a Phoenix application, and I want to have one Ecto Repo and one PostgreSQL database shared by all apps....
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews