c4710n

c4710n

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:

Showing Posts 1 to 3

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:

c4710n

c4710n OP

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.

mruoss

mruoss

Edit: sorry, misread your post

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews