@@ -29,6 +29,8 @@ import (
2929 "time"
3030)
3131
32+ ////////////////////////////////// stream /////////////////////////////////////
33+
3234// Message represents a single message unit read from the stream.
3335type 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.
126130type 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.
159177func (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.
177195func (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.
219226type 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.
276283func 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.
290297func 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}
0 commit comments