The behavior of a simple HTTP Client in Elixir
Background
I maintain a hex Package that uses HTTP requests.
Currently, this package is supposed to use HTTPoison as the HTTP Client, but I wanted to know how to specify the HTTP Client more flexibly.
My competent colleague taught me the ExAws approach.
This is to define a Behavior that matches the interface you want to use in your application, and inject an implementation of it.
I would like to avoid defining a similar Behavior every time I develop a package that utilizes the HTTP Client, as it would be tedious.
Looking at the code above, ExAws Behavior is very versatile.
However, I would like to avoid the need for ExAws just to use the Behavior from the viewpoint of package dependence.
scratcher
So I released only Behavior, a simple HTTP client, as a standalone package.
Here, the “request” function is defined as callback.
I’m adding an HTTP header to the ExAws.Request.HttpClient response, It’s almost the same.This package is independent because it does not implement a specific HTTP Request.
The developer can call HTTP Request within our application/module by following the steps below.
- Use a module that already implements `Scratcher.HttpClient` as Behavior, or implement a module that uses the HTTP Client that you want to use.
- Specify the module in Config.
- The module that wants to send HTTP request can use the HTTP Client module obtained from Config.
Of course, you can use Scratcher.HTTPClient as a Behavior even if you want to mock an HTTP Request.
scratcher_hackney
Then, referring to ExAWS, I created a package that implements this in Hackney.
I plan to prepare a package with a minimum implementation for other famous HTTP Clients (HTTPoison, HTTPotion…).
I hope this package will be used by developers who are as troubled with handling HTTP Clients as I am. For feedback, please use the Issue or Pull Request on Github.