|
| 1 | +package hc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/jjeffcaii/reactor-go/mono" |
| 9 | +) |
| 10 | + |
| 11 | +// DefaultClient is the default Client. |
| 12 | +var DefaultClient = NewClient(http.DefaultClient) |
| 13 | + |
| 14 | +// Option is the type of http client options. |
| 15 | +type Option func(*option) |
| 16 | + |
| 17 | +// Mono is alias of mono.Mono. |
| 18 | +type Mono = mono.Mono |
| 19 | + |
| 20 | +// Client is a reactor style http client which returns a Mono. |
| 21 | +type Client interface { |
| 22 | + // Do sends an HTTP request and returns a response Mono. |
| 23 | + Do(req *http.Request, opts ...Option) Mono |
| 24 | + // Get sends a GET request and returns a response Mono. |
| 25 | + Get(url string, opts ...Option) Mono |
| 26 | + // Delete sends a DELETE request and returns a response Mono. |
| 27 | + Delete(url string, opts ...Option) Mono |
| 28 | + // Post sends a POST request and returns a response Mono. |
| 29 | + Post(url string, contentType string, body io.Reader, opts ...Option) Mono |
| 30 | + // Put sends a PUT request and returns a response Mono. |
| 31 | + Put(url string, contentType string, body io.Reader, opts ...Option) Mono |
| 32 | + // Patch sends a PATCH request and returns a response Mono. |
| 33 | + Patch(url string, contentType string, body io.Reader, opts ...Option) Mono |
| 34 | +} |
| 35 | + |
| 36 | +// Do sends an HTTP request and returns a response Mono. |
| 37 | +func Do(req *http.Request, opts ...Option) Mono { |
| 38 | + return DefaultClient.Do(req, opts...) |
| 39 | +} |
| 40 | + |
| 41 | +// Get sends a GET request and returns a response Mono. |
| 42 | +func Get(url string, opts ...Option) Mono { |
| 43 | + return DefaultClient.Get(url, opts...) |
| 44 | +} |
| 45 | + |
| 46 | +// Delete sends a DELETE request and returns a response Mono. |
| 47 | +func Delete(url string, opts ...Option) Mono { |
| 48 | + return DefaultClient.Delete(url, opts...) |
| 49 | +} |
| 50 | + |
| 51 | +// Post sends a POST request and returns a response Mono. |
| 52 | +func Post(url string, contentType string, body io.Reader, opts ...Option) Mono { |
| 53 | + return DefaultClient.Post(url, contentType, body, opts...) |
| 54 | +} |
| 55 | + |
| 56 | +// Put sends a PUT request and returns a response Mono. |
| 57 | +func Put(url string, contentType string, body io.Reader, opts ...Option) Mono { |
| 58 | + return DefaultClient.Put(url, contentType, body, opts...) |
| 59 | +} |
| 60 | + |
| 61 | +// Patch sends a PATCH request and returns a response Mono. |
| 62 | +func Patch(url string, contentType string, body io.Reader, opts ...Option) Mono { |
| 63 | + return DefaultClient.Patch(url, contentType, body, opts...) |
| 64 | +} |
| 65 | + |
| 66 | +// NewClient creates a new Client. |
| 67 | +func NewClient(cli *http.Client, defaultOptions ...Option) Client { |
| 68 | + return &client{ |
| 69 | + c: cli, |
| 70 | + opts: defaultOptions, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +var _ Client = (*client)(nil) |
| 75 | + |
| 76 | +type client struct { |
| 77 | + c *http.Client |
| 78 | + opts []Option |
| 79 | +} |
| 80 | + |
| 81 | +func (h *client) Do(req *http.Request, opts ...Option) Mono { |
| 82 | + gen := func(ctx context.Context) (*http.Request, error) { |
| 83 | + return req.WithContext(ctx), nil |
| 84 | + } |
| 85 | + return h.execute(gen, opts...) |
| 86 | +} |
| 87 | + |
| 88 | +func (h *client) Get(url string, opts ...Option) Mono { |
| 89 | + return h.customExecute(http.MethodGet, url, "", nil, opts...) |
| 90 | +} |
| 91 | + |
| 92 | +func (h *client) Delete(url string, opts ...Option) Mono { |
| 93 | + return h.customExecute(http.MethodDelete, url, "", nil, opts...) |
| 94 | +} |
| 95 | + |
| 96 | +func (h *client) Post(url string, contentType string, body io.Reader, opts ...Option) Mono { |
| 97 | + return h.customExecute(http.MethodPost, url, contentType, body, opts...) |
| 98 | +} |
| 99 | + |
| 100 | +func (h *client) Put(url string, contentType string, body io.Reader, opts ...Option) Mono { |
| 101 | + return h.customExecute(http.MethodPut, url, contentType, body, opts...) |
| 102 | +} |
| 103 | + |
| 104 | +func (h *client) Patch(url string, contentType string, body io.Reader, opts ...Option) Mono { |
| 105 | + return h.customExecute(http.MethodPatch, url, contentType, body, opts...) |
| 106 | +} |
| 107 | + |
| 108 | +func (h *client) customExecute(method string, url string, contentType string, body io.Reader, opts ...Option) Mono { |
| 109 | + return h.execute(func(ctx context.Context) (*http.Request, error) { |
| 110 | + req, err := http.NewRequestWithContext(ctx, method, url, body) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + if len(contentType) > 0 { |
| 115 | + req.Header.Set("Content-Type", contentType) |
| 116 | + } |
| 117 | + return req, nil |
| 118 | + }, opts...) |
| 119 | +} |
| 120 | + |
| 121 | +func (h *client) execute(gen func(ctx context.Context) (*http.Request, error), opts ...Option) Mono { |
| 122 | + return mono.Create(func(ctx context.Context, s mono.Sink) { |
| 123 | + var ( |
| 124 | + opt *option |
| 125 | + req *http.Request |
| 126 | + err error |
| 127 | + ) |
| 128 | + |
| 129 | + // init option |
| 130 | + if len(opts)+len(h.opts) > 0 { |
| 131 | + opt = new(option) |
| 132 | + for _, it := range h.opts { |
| 133 | + it(opt) |
| 134 | + } |
| 135 | + for _, it := range opts { |
| 136 | + it(opt) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + req, err = gen(ctx) |
| 141 | + if err != nil { |
| 142 | + s.Error(err) |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + if opt != nil && len(opt.reqChain) > 0 { |
| 147 | + for _, it := range opt.reqChain { |
| 148 | + it(req) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + var res *http.Response |
| 153 | + res, err = h.c.Do(req) |
| 154 | + if err != nil { |
| 155 | + s.Error(err) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + if opt == nil || opt.bodyParser == nil { |
| 160 | + s.Success(res) |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + var body interface{} |
| 165 | + body, err = opt.bodyParser(res) |
| 166 | + if err != nil { |
| 167 | + s.Error(err) |
| 168 | + return |
| 169 | + } |
| 170 | + |
| 171 | + s.Success(body) |
| 172 | + }) |
| 173 | +} |
0 commit comments