D4no0

D4no0

Getting data from Rest API in Liveview project

At the moment I have an API that returns json responses, the api is used for a mobile application. The next step is to create a web frontend and the choice we ended up using is LiveView.

From what I understand I have a few approaches how I can get the required data:

  • Reimplement the logic from Rest API controllers.
  • Use the REST API controllers and decode the json response.

Is there a cleaner way to do this? I would like to use the controllers, without having to encode an decode jsons everytime

Most Liked

PJUllrich

PJUllrich

Author of Building Table Views with Phoenix LiveView

Are you serving the API with a Elixir + Phoenix backend? If yes you could extract the data-gathering functionality from your controllers into another module and use the same functions from both your controller and your LiveView.

dimitarvp

dimitarvp

Same advice applies: extract that auth logic outside of the plugs and use them there and in your LiveView code.:slight_smile:

D4no0

D4no0

Yes, I have an umbrella project that contains these projects.

The problem with extracting the logic is that I have plug guards for authentication and roles checking on specific endpoints, I would like to avoid duplicating them, since at some point somebody might make a mistake and mess up roles on specific endpoints.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement