github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xnet/xhttp/xrequest/README.md (about) 1 # XRequest 2 3 ## Request 4 `http.Request`的包装。 5 ```go 6 package xrequest 7 8 type Request struct { 9 R *http.Request 10 } 11 ``` 12 ### RequestOption 13 简化对http.Request的修改。 14 ```go 15 package xrequest 16 type RequestOption func(req *http.Request) 17 ``` 18 分别提供了以下的实现: 19 - AddHeader(key string, value string) RequestOption 20 - AddHeaderMap(h http.Header) RequestOption 21 - WithHeader(key string, value string) RequestOption 22 - WithHeaderMap(h http.Header) RequestOption 23 - WithForm(f url.Values) RequestOption 24 - WithContentType(contentType string) RequestOption 25 - AddCookie(cookie *http.Cookie) RequestOption 26 - WithRequestBody(body RequestBody) RequestOption 27 28 ### RequestBody 29 请求体的抽象 30 ```go 31 package xrequest 32 33 type RequestBody interface { 34 io.Reader 35 ContentType() string 36 ContentEncoding() *string 37 } 38 ``` 39 默认提供了如下实现: 40 - JsonBody 序列化Json数据 41 - XmlBody 序列化Xml数据 42 - UrlEncodedBody 序列化UrlEncoded数据 43 - BinaryBody 序列化二进制数据 44 - GzipBody 压缩RequestBody 45 46 ## Response 47 `http.Response`的包装 48 ```go 49 type Response struct { 50 Response *http.Response 51 status *xhttp.Status 52 } 53 ``` 54 ## Client 55 `http.Client`的包装. 56 ```go 57 package xrequest 58 59 import "net/http" 60 61 // RoundTripperFn implement http.RoundTripper for convenient usage. 62 type RoundTripperFn func(request *http.Request) (*http.Response, error) 63 64 func (fn RoundTripperFn) RoundTrip(request *http.Request) (*http.Response, error) { return fn(request) } 65 66 // Interceptor is interceptor that can do more work before/after an request 67 type Interceptor interface { 68 Next(fn http.RoundTripper) http.RoundTripper 69 } 70 71 // InterceptorFn implement Interceptor for convenient usage. 72 type InterceptorFn func(rt http.RoundTripper) http.RoundTripper 73 74 func (fn InterceptorFn) Next(rt http.RoundTripper) http.RoundTripper { return fn(rt) } 75 76 type Client struct { 77 client *http.Client 78 interceptors []Interceptor 79 baseHost string 80 } 81 ``` 82 83 ### Methods 84 - func Head(ctx context.Context, url string, options ...RequestOption) (*Response, error) 85 - func Connect(ctx context.Context, url string, options ...RequestOption) (*Response, error) 86 - func Options(ctx context.Context, url string, options ...RequestOption) (*Response, error) 87 - func Trace(ctx context.Context, url string, options ...RequestOption) (*Response, error) 88 - func Get(ctx context.Context, url string, options ...RequestOption) (*Response, error) 89 - func Post(ctx context.Context, url string, body RequestBody, options ...RequestOption) (*Response, error) 90 - func Put(ctx context.Context, url string, body RequestBody, options ...RequestOption) (*Response, error) 91 - func Patch(ctx context.Context, url string, body RequestBody, options ...RequestOption) (*Response, error) 92 - func Delete(ctx context.Context, url string, body RequestBody, options ...RequestOption) (*Response, error) 93 94 ### Interceptor 95 拦截器,在http请求前/后做一些额外的工作。比如日志,重试,负载均衡等操作。 96 简单的日志重试: 97 ```go 98 package xrequest 99 100 func RetryOnStatusCode(codes ...int) InterceptorFn { 101 return func(rt http.RoundTripper) http.RoundTripper { 102 return RoundTripperFn(func(request *http.Request) (response *http.Response, err error) { 103 for i := 0; i < 3; i++ { 104 response, err = rt.RoundTrip(request) 105 if err != nil || (response != nil && xslice.ContainsInt(codes, response.StatusCode)) { 106 continue 107 } 108 return 109 } 110 return 111 }) 112 } 113 } 114 ```