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

1 Like

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.

1 Like

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.

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

1 Like