c4710n

c4710n

A proposal for community: Tesla-compatible libraries

Background

When implementing libraries which have to sent HTTP/HTTPS requests, we have to implement an HTTP client.

In most of time, people implement their own HTTP client with their preferred underlying library, such as: httpc, hackney, mint, finch, etc.

But, maybe that’s not the good way.

Doing so not only wastes the time but also makes the libraries inflexible. If using multiple libraries, and these libraries use different HTTP clients, we will have many different implementations of HTTP clients running at the same time.

What’s the big deal?

Using multiple implementations of HTTP clients in one project, not only consumes more resources but may also creates inconsistencies in usage patterns, complicates maintenance, observation and debugging, etc.

It would be good if we can have a unified HTTP implementation, and then each library author can use it. In the way:

  • every library author can add multiple HTTP-adapters support in short time.
  • every library user can choose their preferred HTTP client in a flexible way.

What to use?

After investigating existing HTTP implementations, I found elixir-tesla/tesla, which considers what I said above.

Tesla provides a very flexible way to configure adapters. Let’s say we want use hackney as the adapter.

  1. Set an adapter at module level in the module itself:
defmodule GitHubClient do
  use Tesla

  adapter Tesla.Adapter.Hackney, recv_timeout: 30_000

  # ...
end
  1. Set an adapter at module level in the config file:
config :tesla, GitHubClient, adapter: {Tesla.Adapter.Hackney, [recv_timeout: 30_000]}
  1. Set an adapter at app level in the config file (this is not supported for now, but I have created an PR for it):
config :tesla, :my_app, adapter: {Tesla.Adapter.Hackney, [recv_timeout: 30_000]}
  1. Set an adapter at global level in the config file:
config :tesla, adapter: {Tesla.Adapter.Hackney, [recv_timeout: 30_000]}

Last

Everyone can benefit from using Tesla:

  • every library author can add multiple HTTP-adapters support in short time.
  • every library user can choose their preferred HTTP client in a flexible way.

Consider using it when writing your next package. If you do, remember to include a badge in your README.md, like Tesla compatible.

[![Tesla compatible](https://img.shields.io/badge/Tesla%20compatible-6e4a7e?Color=white)](https://github.com/elixir-tesla/tesla)

That’s it. :wink:

Most Liked

mruoss

mruoss

@whatyouhide also discussed this topic in his blog post “A Breakdown of HTTP Clients in Elixir”. Me personally, as a library author I’d rather use Req than Tesla. It also allows for custom HTTP adapters. The problem I have with Req (for libraries) is that its default adapter (Finch) is not an optional dependency which, in the case of libraries, would be desirable.

The “adapter behaviour” Andrea discusses in the blog post is a possible approach for libraries. But as a library author, I don’t want to give up on the conveniences I get from req (steps) or tesla (middlewares). I’m in a bit of a dilemma here… :slight_smile:

mruoss

mruoss

Edit: sorry, misread your post

Where Next?

Popular in Discussions Top

AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18634 126
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
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

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 54260 488
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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 31586 112
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement