add /x/client.Clone method

This commit is contained in:
Gerasimos (Makis) Maropoulos 2023-11-13 21:27:35 +02:00
parent c484fa3dfa
commit 5f749453c7
No known key found for this signature in database
GPG Key ID: B9839E9CD30B7B6B
3 changed files with 23 additions and 4 deletions

2
go.mod
View File

@ -23,7 +23,7 @@ require (
github.com/json-iterator/go v1.1.12
github.com/kataras/blocks v0.0.8
github.com/kataras/golog v0.1.11
github.com/kataras/jwt v0.1.10
github.com/kataras/jwt v0.1.11
github.com/kataras/neffos v0.0.22
github.com/kataras/pio v0.0.13
github.com/kataras/sitemap v0.0.6

4
go.sum generated
View File

@ -115,8 +115,8 @@ github.com/kataras/blocks v0.0.8 h1:MrpVhoFTCR2v1iOOfGng5VJSILKeZZI+7NGfxEh3SUM=
github.com/kataras/blocks v0.0.8/go.mod h1:9Jm5zx6BB+06NwA+OhTbHW1xkMOYxahnqTN5DveZ2Yg=
github.com/kataras/golog v0.1.11 h1:dGkcCVsIpqiAMWTlebn/ZULHxFvfG4K43LF1cNWSh20=
github.com/kataras/golog v0.1.11/go.mod h1:mAkt1vbPowFUuUGvexyQ5NFW6djEgGyxQBIARJ0AH4A=
github.com/kataras/jwt v0.1.10 h1:GBXOF9RVInDPhCFBiDumRG9Tt27l7ugLeLo8HL5SeKQ=
github.com/kataras/jwt v0.1.10/go.mod h1:xkimAtDhU/aGlQqjwvgtg+VyuPwMiyZHaY8LJRh0mYo=
github.com/kataras/jwt v0.1.11 h1:nWMwIRCGeKz06dLrML1o6Z3s4myZt/+DA41/PemJndI=
github.com/kataras/jwt v0.1.11/go.mod h1:xkimAtDhU/aGlQqjwvgtg+VyuPwMiyZHaY8LJRh0mYo=
github.com/kataras/neffos v0.0.22 h1:3M4lHrUl//2OKmS9t9z3AKIZqwha6ABeA6WoF03HEv8=
github.com/kataras/neffos v0.0.22/go.mod h1:IIJZcUDvwBxJGlDj942dqQgyznVKYDti91f8Ez+RRxE=
github.com/kataras/pio v0.0.13 h1:x0rXVX0fviDTXOOLOmr4MUxOabu1InVSTu5itF8CXCM=

View File

@ -19,6 +19,8 @@ import (
// A Client is an HTTP client. Initialize with the New package-level function.
type Client struct {
opts []Option // keep for clones.
HTTPClient *http.Client
// BaseURL prepends to all requests.
@ -50,12 +52,14 @@ type Client struct {
// The default content type to send and receive data is JSON.
func New(opts ...Option) *Client {
c := &Client{
opts: opts,
HTTPClient: &http.Client{},
PersistentRequestOptions: defaultRequestOptions,
requestHandlers: defaultRequestHandlers,
}
for _, opt := range opts {
for _, opt := range c.opts { // c.opts in order to make with `NoOption` work.
opt(c)
}
@ -66,6 +70,17 @@ func New(opts ...Option) *Client {
return c
}
// NoOption is a helper function that clears the previous options in the chain.
// See `Client.Clone` method.
var NoOption = func(c *Client) { c.opts = make([]Option, 0) /* clear previous options */ }
// Clone returns a new Client with the same options as the original.
// If you want to override the options from the base "c" Client,
// use the `NoOption` variable as the 1st argument.
func (c *Client) Clone(opts ...Option) *Client {
return New(append(c.opts, opts...)...)
}
// RegisterRequestHandler registers one or more request handlers
// to be ran before and after of each new request.
//
@ -401,6 +416,10 @@ func (c *Client) ReadJSON(ctx context.Context, dest interface{}, method, urlpath
// println(string(b))
// return json.Unmarshal(b, &dest)
if dest != nil {
return json.NewDecoder(resp.Body).Decode(&dest)
}
return json.NewDecoder(resp.Body).Decode(&dest)
}