|
1 | | -# HTTP.fetch |
| 1 | +# HTTP Fetch |
2 | 2 |
|
3 | | -Use `:httpc` to implement a `http.fetch` in `Elixir` |
| 3 | +A modern HTTP client library for Elixir that provides a fetch API similar to web browsers, built on Erlang's built-in `:httpc` module. |
4 | 4 |
|
| 5 | +## Features |
5 | 6 |
|
| 7 | +- **Browser-like API**: Familiar fetch interface with promises and async/await patterns |
| 8 | +- **Full HTTP support**: GET, POST, PUT, DELETE, PATCH, HEAD methods |
| 9 | +- **Promise-based**: Async operations with chaining support |
| 10 | +- **Request cancellation**: AbortController support for cancelling requests |
| 11 | +- **Automatic JSON parsing**: Built-in JSON response handling |
| 12 | +- **Zero dependencies**: Uses only Erlang/OTP built-in modules |
6 | 13 |
|
| 14 | +## Quick Start |
| 15 | + |
| 16 | +```elixir |
| 17 | +# Simple GET request |
| 18 | +{:ok, response} = |
| 19 | + HTTP.fetch("https://jsonplaceholder.typicode.com/posts/1") |
| 20 | + |> HTTP.Promise.await() |
| 21 | + |
| 22 | +# Get response data |
| 23 | +IO.puts("Status: #{response.status}") |
| 24 | +text = HTTP.Response.text(response) |
| 25 | +{:ok, json} = HTTP.Response.json(response) |
| 26 | + |
| 27 | +# POST request with JSON |
| 28 | +{:ok, response} = |
| 29 | + HTTP.fetch("https://jsonplaceholder.typicode.com/posts", [ |
| 30 | + method: "POST", |
| 31 | + headers: %{"Content-Type" => "application/json"}, |
| 32 | + body: JSON.encode\!(%{title: "Hello", body: "World"}) |
| 33 | + ]) |
| 34 | + |> HTTP.Promise.await() |
| 35 | +``` |
| 36 | + |
| 37 | +## API Reference |
| 38 | + |
| 39 | +### HTTP.fetch/2 |
| 40 | +Performs an HTTP request and returns a Promise. |
| 41 | + |
| 42 | +```elixir |
| 43 | +promise = HTTP.fetch(url, [ |
| 44 | + method: "GET", |
| 45 | + headers: %{"Accept" => "application/json"}, |
| 46 | + body: "request body", |
| 47 | + content_type: "application/json", |
| 48 | + options: [timeout: 10_000], |
| 49 | + signal: abort_controller |
| 50 | +]) |
| 51 | +``` |
| 52 | + |
| 53 | +### HTTP.Promise |
| 54 | +Asynchronous promise wrapper for HTTP requests. |
| 55 | + |
| 56 | +```elixir |
| 57 | +{:ok, response} = HTTP.Promise.await(promise) |
| 58 | + |
| 59 | +# Promise chaining |
| 60 | +HTTP.fetch("https://api.example.com/data") |
| 61 | +|> HTTP.Promise.then(fn response -> HTTP.Response.json(response) end) |
| 62 | +|> HTTP.Promise.await() |
| 63 | +``` |
| 64 | + |
| 65 | +### HTTP.Response |
| 66 | +Represents an HTTP response. |
| 67 | + |
| 68 | +```elixir |
| 69 | +text = HTTP.Response.text(response) |
| 70 | +{:ok, json} = HTTP.Response.json(response) |
| 71 | +``` |
| 72 | + |
| 73 | +### HTTP.Request |
| 74 | +Request configuration struct. |
| 75 | + |
| 76 | +```elixir |
| 77 | +request = %HTTP.Request{ |
| 78 | + method: :post, |
| 79 | + url: "https://api.example.com/data", |
| 80 | + headers: [{"Authorization", "Bearer token"}], |
| 81 | + body: "data" |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### HTTP.AbortController |
| 86 | +Request cancellation. |
| 87 | + |
| 88 | +```elixir |
| 89 | +controller = HTTP.AbortController.new() |
| 90 | +HTTP.AbortController.abort(controller) |
| 91 | +``` |
| 92 | + |
| 93 | +## Error Handling |
| 94 | + |
| 95 | +The library handles: |
| 96 | +- Network errors and timeouts |
| 97 | +- HTTP error status codes |
| 98 | +- JSON parsing errors |
| 99 | +- Invalid URLs |
| 100 | +- Cancelled requests |
| 101 | + |
| 102 | +## License |
| 103 | + |
| 104 | +MIT License |
| 105 | +EOF < /dev/null |
0 commit comments