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:

4 Likes

@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:

2 Likes

Previously, I think that req and finch are tightly bound. If, as he says, finch will also support multiple adapters and offer flexible configuration options, that would be terrific.

Thank you for refreshing my understanding. :wink:

Maybe, someday, it would be Req-compatible libraries.

Edit: sorry, misread your post

1 Like