Skip to content

Commit 4204cad

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent a5614e2 commit 4204cad

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

lib/httputil/httputil.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"time"
3030
)
3131

32+
////////////////////////////////// stream /////////////////////////////////////
33+
3234
// Message represents a single message unit read from the stream.
3335
type Message struct {
3436
Data string
@@ -122,6 +124,8 @@ func (s *Stream) Close() {
122124
}
123125
}
124126

127+
///////////////////////////////// interface ///////////////////////////////////
128+
125129
// RequestMeta holds contextual information for an HTTP request.
126130
type RequestMeta struct {
127131
Target string
@@ -139,7 +143,8 @@ type HTTPClient interface {
139143
Stream(req *http.Request, meta RequestMeta) (*http.Response, *Stream, error)
140144
}
141145

142-
var DefaultHTTPClient HTTPClient = &SimpleHTTPClient{http.DefaultClient}
146+
// DefaultClient is the default HTTPClient implementation.
147+
var DefaultClient HTTPClient = &SimpleHTTPClient{http.DefaultClient}
143148

144149
// SimpleHTTPClient is the default implementation of HTTPClient,
145150
// which delegates to the standard library http.Client.
@@ -148,6 +153,19 @@ type SimpleHTTPClient struct {
148153
Client *http.Client
149154
}
150155

156+
// do executes the given HTTP request using the embedded http.Client.
157+
func (c *SimpleHTTPClient) do(r *http.Request, meta RequestMeta) (*http.Response, error) {
158+
r.Host = meta.Target
159+
r.URL.Host = meta.Target
160+
r.URL.Scheme = meta.Schema
161+
for k, values := range meta.Header {
162+
for _, v := range values {
163+
r.Header.Add(k, v)
164+
}
165+
}
166+
return c.Client.Do(r)
167+
}
168+
151169
// JSON executes the HTTP request using the embedded http.Client.
152170
// It reads the entire response body into memory and returns both
153171
// the *http.Response and the body as a byte slice.
@@ -157,7 +175,7 @@ type SimpleHTTPClient struct {
157175
//
158176
// Note: For very large responses, this may be memory intensive.
159177
func (c *SimpleHTTPClient) JSON(r *http.Request, meta RequestMeta) (*http.Response, []byte, error) {
160-
resp, err := c.doRequest(r, meta)
178+
resp, err := c.do(r, meta)
161179
if err != nil {
162180
return nil, nil, err
163181
}
@@ -175,7 +193,7 @@ func (c *SimpleHTTPClient) JSON(r *http.Request, meta RequestMeta) (*http.Respon
175193
// Stream executes an HTTP request and continuously reads lines from the response body.
176194
// Each line is sent into the returned Stream channel asynchronously.
177195
func (c *SimpleHTTPClient) Stream(r *http.Request, meta RequestMeta) (*http.Response, *Stream, error) {
178-
resp, err := c.doRequest(r, meta)
196+
resp, err := c.do(r, meta)
179197
if err != nil {
180198
return nil, nil, err
181199
}
@@ -202,18 +220,7 @@ func (c *SimpleHTTPClient) Stream(r *http.Request, meta RequestMeta) (*http.Resp
202220
return resp, respStream, nil
203221
}
204222

205-
// doRequest executes the given HTTP request using the embedded http.Client.
206-
func (c *SimpleHTTPClient) doRequest(r *http.Request, meta RequestMeta) (*http.Response, error) {
207-
r.Host = meta.Target
208-
r.URL.Host = meta.Target
209-
r.URL.Scheme = meta.Schema
210-
for k, values := range meta.Header {
211-
for _, v := range values {
212-
r.Header.Add(k, v)
213-
}
214-
}
215-
return c.Client.Do(r)
216-
}
223+
////////////////////////////////// response ///////////////////////////////////
217224

218225
// RequestOption is a function type that modifies the RequestMeta.
219226
type RequestOption func(info *RequestMeta)
@@ -274,7 +281,7 @@ func buildMeta(opts []RequestOption) RequestMeta {
274281
// JSONResponse executes the given HTTP request using the provided HTTPClient,
275282
// reads the response body, and unmarshals it into a value of type RespType.
276283
func JSONResponse[RespType any](r *http.Request, opts ...RequestOption) (*http.Response, *RespType, error) {
277-
resp, b, err := DefaultHTTPClient.JSON(r, buildMeta(opts))
284+
resp, b, err := DefaultClient.JSON(r, buildMeta(opts))
278285
if err != nil {
279286
return nil, nil, err
280287
}
@@ -288,5 +295,5 @@ func JSONResponse[RespType any](r *http.Request, opts ...RequestOption) (*http.R
288295
// StreamResponse executes the given HTTP request using the provided HTTPClient,
289296
// and returns a Stream instance for streaming the response body.
290297
func StreamResponse(r *http.Request, opts ...RequestOption) (*http.Response, *Stream, error) {
291-
return DefaultHTTPClient.Stream(r, buildMeta(opts))
298+
return DefaultClient.Stream(r, buildMeta(opts))
292299
}

lib/httputil/httputil_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ func TestHello(t *testing.T) {
311311
h := http.Header{}
312312
h.Set("X-Request-ID", "12345678")
313313

314-
httputil.DefaultHTTPClient = &LogHTTPClient{
315-
HTTPClient: httputil.DefaultHTTPClient,
314+
httputil.DefaultClient = &LogHTTPClient{
315+
HTTPClient: httputil.DefaultClient,
316316
}
317317

318318
client := &HelloClient{
@@ -387,8 +387,8 @@ func TestStream(t *testing.T) {
387387
h := http.Header{}
388388
h.Set("X-Request-ID", "12345678")
389389

390-
httputil.DefaultHTTPClient = &LogHTTPClient{
391-
HTTPClient: httputil.DefaultHTTPClient,
390+
httputil.DefaultClient = &LogHTTPClient{
391+
HTTPClient: httputil.DefaultClient,
392392
}
393393

394394
client := &HelloClient{

0 commit comments

Comments
 (0)