yordisprieto

yordisprieto

API Clients Design

Hey there, I am working with a client for an OpenAPI spec, and I have a dilemma.

Imagine the following

defmodule API.Account do
  @moduledoc """
  Implements /account endpoints.
  """

  alias StrawHat.Response
  alias API.{Client, Client.Request}

  @type t :: %__MODULE__{
          email: String.t(),
          id: String.t(),
          is_international: boolean()
        }

  defstruct [:email, :id, :is_international]

  @doc """
  Retrieve account information.
  """
  @spec get_account(%Client{}) :: Response.t(%__MODULE__{}, Client.error())
  def get_account(client) do
    client
    |> Request.new()
    |> Request.put_method(:get)
    |> Request.put_path("/account")
    |> Client.send()
    |> StrawHat.Response.and_then(&transform_response/1)
  end

  @doc false
  def new(data) do
    %__MODULE__{
      email: data["email"],
      id: data["id"],
      is_international: data["isInternational"]
    }
  end

  defp transform_response(data) do
    data
    |> new()
    |> Response.ok()
  end
end

Here is my dilemma: Should I transform the incoming data into structs, or I shouldn’t bother with it?

I am talking about the following part

    |> StrawHat.Response.and_then(&transform_response/1)

Should I keep that? Or should I just return the raw data?

I still don’t know what the right answer is here honestly.

From one side, it is a lot of work and I think doesn’t matter much, not sure.
From the otherwise, it is nice, I get to have structs and normalize a bit the keys and such.

Most Liked

princemaple

princemaple

My personal preference is to return raw, so that you don’t have to keep up with the api as closely, and don’t have to distinguish between versions as strictly.

stefanchrobot

stefanchrobot

If I’m writing an API client where I work with the data (as opposed to just passing it to somewhere else), I usually add the request and response structs. This makes the API more discoverable without the need to reach to the API documentation. But this is not a universal advice - there are scenarios where you don’t care about the response, so why waste the time on building a reach response.

Where Next?

Popular in Discussions Top

chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19568 166
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
jsonify
So, is Heroku the only free option for hosting Phoenix/Elixir at this point? I’m not ready to commit to paying monthly and was wondering ...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement