2323 nextPart = func (mpr * multipart.Reader ) (* multipart.Part , error ) { return mpr .NextPart () }
2424)
2525
26+ var (
27+ // seq is used to generate unique client ids; it is incremented each time a client is created
28+ seq int
29+ )
30+
2631// RequestOption is a function that applies an option to a request
2732type RequestOption = func (* http.Request ) error
2833
@@ -55,8 +60,8 @@ type ClientOption func(*client) error
5560// This type is not exported; functionality is accessed through the implmented
5661// HttpClient interface.
5762type client struct {
58- // name is used to identify the client in error messages
59- name string
63+ // id is used to identify the client in error messages
64+ id string
6065
6166 // url is prepended to the url of any request made with the client
6267 url string
@@ -80,9 +85,10 @@ type client struct {
8085// The url typically includes the protocol, hostname and port for the client
8186// but may include any additional url components consistently required for
8287// requests performed using the client.
83- func NewClient (name string , opts ... ClientOption ) (HttpClient , error ) {
88+ func NewClient (opts ... ClientOption ) (HttpClient , error ) {
89+ seq ++
8490 w := client {
85- name : name ,
91+ id : "http-" + strconv . Itoa ( seq ) ,
8692 wrapped : http .DefaultClient ,
8793 }
8894 errs := make ([]error , 0 , len (opts ))
@@ -273,7 +279,7 @@ func (c client) execute(
273279) (* http.Response , error ) {
274280 rq , err := c .NewRequest (ctx , method , url , opts ... )
275281 if err != nil {
276- return nil , errorcontext .Errorf (ctx , "%s: %s: %w" , c .name , method , err )
282+ return nil , errorcontext .Errorf (ctx , "%s: %s: %w" , c .id , method , err )
277283 }
278284 return c .Do (rq )
279285}
@@ -283,7 +289,7 @@ func (c client) execute(
283289func (c client ) Do (rq * http.Request ) (* http.Response , error ) {
284290 ctx := rq .Context ()
285291 handle := func (r * http.Response , err error ) (* http.Response , error ) {
286- return r , errorcontext .Errorf (ctx , "%s: %s %s: %w" , c .name , rq .Method , rq .URL , err )
292+ return r , errorcontext .Errorf (ctx , "%s: %s %s: %w" , c .id , rq .Method , rq .URL , err )
287293 }
288294
289295 retries , statusCodes , bodyRequired , stream , err := c .parseRequestHeaders (rq )
@@ -417,11 +423,9 @@ func MapFromMultipartFormData[K comparable, V any](
417423//
418424// The function returns an error if the body cannot be read or if the body does not
419425// contain valid JSON and the result will be the zero value of the generic type.
420- func UnmarshalJSON [T any ](ctx context.Context , r * http.Response ) (T , error ) {
421- result := * new (T )
422-
423- handle := func (sen , err error ) (T , error ) {
424- return result , errorcontext .Errorf (ctx , "http.UnmarshalJSON: %w: %w" , sen , err )
426+ func UnmarshalJSON [T any ](r * http.Response ) (* T , error ) {
427+ handle := func (sen , err error ) (* T , error ) {
428+ return nil , fmt .Errorf ("http.UnmarshalJSON: %w: %w" , sen , err )
425429 }
426430
427431 body , err := ioReadAll (r .Body )
@@ -430,9 +434,9 @@ func UnmarshalJSON[T any](ctx context.Context, r *http.Response) (T, error) {
430434 return handle (ErrReadingResponseBody , err )
431435 }
432436
433- if err := json .Unmarshal (body , & result ); err != nil {
437+ result := new (T )
438+ if err := json .Unmarshal (body , result ); err != nil {
434439 return handle (ErrInvalidJSON , err )
435440 }
436-
437441 return result , nil
438442}
0 commit comments