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

Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
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
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
hazardfn
I suppose this question is effectively hackney vs. ibrowse but we are at a point in our project where we have to make a choice between th...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
kostonstyle
Hi all How can I compare haskell with elixir, included tools, webservices, ect. Thanks
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New

We're in Beta

About us Mission Statement