From 5f749453c7ff3dd125d7f0dcba568b5a007ffc6f Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Mon, 13 Nov 2023 21:27:35 +0200 Subject: [PATCH] add /x/client.Clone method --- go.mod | 2 +- go.sum | 4 ++-- x/client/client.go | 21 ++++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index fc26f2f9..fbf9c07b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index d68c31fe..b3b7a44d 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/x/client/client.go b/x/client/client.go index 59b2d529..61107a05 100644 --- a/x/client/client.go +++ b/x/client/client.go @@ -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) }