|
| 1 | +# Fx Http Client Module |
| 2 | + |
| 3 | +[](https://github.com/ankorstore/yokai/actions/workflows/fxhttpclient-ci.yml) |
| 4 | +[](https://goreportcard.com/report/github.com/ankorstore/yokai/fxhttpclient) |
| 5 | +[](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxhttpclient) |
| 6 | +[](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxhttpclient) |
| 7 | +[](https://pkg.go.dev/github.com/ankorstore/yokai/fxhttpclient) |
| 8 | + |
| 9 | +> [Fx](https://uber-go.github.io/fx/) module for [httpclient](https://github.com/ankorstore/yokai/tree/main/httpclient). |
| 10 | +
|
| 11 | +<!-- TOC --> |
| 12 | +* [Installation](#installation) |
| 13 | +* [Features](#features) |
| 14 | +* [Documentation](#documentation) |
| 15 | + * [Dependencies](#dependencies) |
| 16 | + * [Loading](#loading) |
| 17 | + * [Configuration](#configuration) |
| 18 | + * [Override](#override) |
| 19 | +<!-- TOC --> |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```shell |
| 24 | +go get github.com/ankorstore/yokai/fxhttpclient |
| 25 | +``` |
| 26 | + |
| 27 | +## Features |
| 28 | + |
| 29 | +This module provides the possibility to provide to your Fx application a `http.Client` with: |
| 30 | + |
| 31 | +- configurable transport |
| 32 | +- automatic and configurable request / response logging |
| 33 | +- configurable request / response tracing |
| 34 | + |
| 35 | +## Documentation |
| 36 | + |
| 37 | +### Dependencies |
| 38 | + |
| 39 | +This module is intended to be used alongside: |
| 40 | + |
| 41 | +- the [fxconfig](https://github.com/ankorstore/yokai/tree/main/fxconfig) module |
| 42 | +- the [fxlog](https://github.com/ankorstore/yokai/tree/main/fxlog) module |
| 43 | +- the [fxtrace](https://github.com/ankorstore/yokai/tree/main/fxtrace) module |
| 44 | + |
| 45 | +### Loading |
| 46 | + |
| 47 | +To load the module in your Fx application: |
| 48 | + |
| 49 | +```go |
| 50 | +package main |
| 51 | + |
| 52 | +import ( |
| 53 | + "net/http" |
| 54 | + |
| 55 | + "github.com/ankorstore/yokai/fxconfig" |
| 56 | + "github.com/ankorstore/yokai/fxhttpclient" |
| 57 | + "github.com/ankorstore/yokai/fxlog" |
| 58 | + "github.com/ankorstore/yokai/fxtrace" |
| 59 | + "go.uber.org/fx" |
| 60 | +) |
| 61 | + |
| 62 | +func main() { |
| 63 | + fx.New( |
| 64 | + fxconfig.FxConfigModule, // load the module dependencies |
| 65 | + fxlog.FxLogModule, |
| 66 | + fxtrace.FxTraceModule, |
| 67 | + fxhttpclient.FxHttpClientModule, // load the module |
| 68 | + fx.Invoke(func(httpClient *http.Client) { // invoke the client |
| 69 | + resp, err := httpClient.Get("https://example.com") |
| 70 | + }), |
| 71 | + ).Run() |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Configuration |
| 76 | + |
| 77 | +Configuration reference: |
| 78 | + |
| 79 | +```yaml |
| 80 | +# ./configs/config.yaml |
| 81 | +app: |
| 82 | + name: app |
| 83 | + env: dev |
| 84 | + version: 0.1.0 |
| 85 | + debug: true |
| 86 | +modules: |
| 87 | + log: |
| 88 | + level: info |
| 89 | + output: stdout |
| 90 | + trace: |
| 91 | + processor: |
| 92 | + type: stdout |
| 93 | + http: |
| 94 | + client: |
| 95 | + timeout: 30 # in seconds, 30 by default |
| 96 | + transport: |
| 97 | + max_idle_connections: 100 # 100 by default |
| 98 | + max_connections_per_host: 100 # 100 by default |
| 99 | + max_idle_connections_per_host: 100 # 100 by default |
| 100 | + log: |
| 101 | + request: |
| 102 | + enabled: true # to log request details, disabled by default |
| 103 | + body: true # to add request body to request details, disabled by default |
| 104 | + level: info # log level for request logging |
| 105 | + response: |
| 106 | + enabled: true # to log response details, disabled by default |
| 107 | + body: true # to add response body to request details, disabled by default |
| 108 | + level: info # log level for response logging |
| 109 | + level_from_response: true # to use response code for response logging |
| 110 | + trace: |
| 111 | + enabled: true # to trace http calls, disabled by default |
| 112 | +``` |
| 113 | +
|
| 114 | +If `http.client.log.response.level_from_response=true`, the response code will be used to determinate the log level: |
| 115 | + |
| 116 | +- `code < 400`: log level configured in `http.client.log.response.level` |
| 117 | +- `400 <= code < 500`: log level `warn` |
| 118 | +- `code >= 500`: log level `error` |
| 119 | + |
| 120 | +Notes: |
| 121 | + |
| 122 | +- the http client logging will be based on the [fxlog](https://github.com/ankorstore/yokai/tree/main/fxlog) module |
| 123 | + configuration |
| 124 | +- the http client tracing will be based on the [fxtrace](https://github.com/ankorstore/yokai/tree/main/fxtrace) module |
| 125 | + configuration |
| 126 | + |
| 127 | +### Override |
| 128 | + |
| 129 | +By default, the `http.Client` is created by |
| 130 | +the [DefaultHttpClientFactory](https://github.com/ankorstore/yokai/blob/main/httpclient/factory.go). |
| 131 | + |
| 132 | +If needed, you can provide your own factory and override the module: |
| 133 | + |
| 134 | +```go |
| 135 | +package main |
| 136 | +
|
| 137 | +import ( |
| 138 | + "net/http" |
| 139 | +
|
| 140 | + "github.com/ankorstore/yokai/fxconfig" |
| 141 | + "github.com/ankorstore/yokai/fxhttpclient" |
| 142 | + "github.com/ankorstore/yokai/fxlog" |
| 143 | + "github.com/ankorstore/yokai/fxtrace" |
| 144 | + "github.com/ankorstore/yokai/httpclient" |
| 145 | + "go.uber.org/fx" |
| 146 | +) |
| 147 | +
|
| 148 | +type CustomHttpClientFactory struct{} |
| 149 | +
|
| 150 | +func NewCustomHttpClientFactory() httpclient.HttpClientFactory { |
| 151 | + return &CustomHttpClientFactory{} |
| 152 | +} |
| 153 | +
|
| 154 | +func (f *CustomHttpClientFactory) Create(options ...httpclient.HttpClientOption) (*http.Client, error) { |
| 155 | + return http.DefaultClient, nil |
| 156 | +} |
| 157 | +
|
| 158 | +func main() { |
| 159 | + fx.New( |
| 160 | + fxconfig.FxConfigModule, // load the module dependencies |
| 161 | + fxlog.FxLogModule, |
| 162 | + fxtrace.FxTraceModule, |
| 163 | + fxhttpclient.FxHttpClientModule, // load the module |
| 164 | + fx.Decorate(NewCustomHttpClientFactory), // override the module with a custom factory |
| 165 | + fx.Invoke(func(httpClient *http.Client) { // invoke the custom client |
| 166 | + // ... |
| 167 | + }), |
| 168 | + ).Run() |
| 169 | +} |
| 170 | +``` |
0 commit comments