https://github.com/groguelon/any_http
Elixir library to wrap the main HTTP libraries.
https://github.com/groguelon/any_http
elixir hex http
Last synced: 5 months ago
JSON representation
Elixir library to wrap the main HTTP libraries.
- Host: GitHub
- URL: https://github.com/groguelon/any_http
- Owner: GRoguelon
- License: mit
- Created: 2023-07-22T00:57:45.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-02T02:11:05.000Z (about 2 years ago)
- Last Synced: 2025-02-06T10:03:44.322Z (about 1 year ago)
- Topics: elixir, hex, http
- Language: Elixir
- Homepage: https://hex.pm/packages/any_http
- Size: 95.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Any HTTP
Elixir library which wraps the main HTTP libraries. It allows the final project to decide which
HTTP library they want to use and provide an unified interface.
[Documentation](https://hexdocs.pm/any_http)
## Installation
```elixir
def deps do
[
{:any_http, "~> 0.6"}
]
end
```
## Configuration
### [:httpc](https://www.erlang.org/doc/man/httpc.html)
`:httpc` is already part of your application because it's part of Erlang.
Change your configuration to declare Req as your adapter:
```elixir
config :any_http, client_adapter: AnyHttp.Adapters.Httpc
# You can provide default options to the adapter.
config :any_http, httpc_default_opts: []
```
### [Req](https://hexdocs.pm/req)
Add Req to your `mix.exs` file:
```elixir
def deps do
[
{:req, "~> 0.4"}
]
end
```
Change your configuration to declare Req as your adapter:
```elixir
config :any_http, client_adapter: AnyHttp.Adapters.Req
# You can provide default options to the adapter.
config :any_http, req_default_opts: []
```
### [:hackney](https://hexdocs.pm/hackney/)
Add Req to your `mix.exs` file:
```elixir
def deps do
[
{:hackney, "~> 1.20"}
]
end
```
Change your configuration to declare Req as your adapter:
```elixir
config :any_http, client_adapter: AnyHttp.Adapters.Hackney
# You can provide default options to the adapter.
config :any_http, hackney_default_opts: []
```
## Usage
Just call the `AnyHttp` module to interact with HTTP:
```elixir
AnyHttp.post(
# Provide the URL as a string or an URI
"https://my_server/api",
# Provide the HTTP headers as map or list of tuple, put nil if none
%{"content-type" => "application/json"},
# Provide the body, put nil if none
~s<{"hello": "world"}>,
# Provide the adapter options, can be ommited
receive_timeout: :timer.seconds(5)
)
```
The result will look like:
```elixir
{:ok,
%AnyHttp.Response{
status: 201,
headers: %{"content-type" => ["application/json"]},
body: "{\"bye\":\"world\"}"
}
}
```