From 54c7799a3ce3509351b2c696d230cecab433e122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Doubinine?= Date: Thu, 24 Mar 2022 15:27:04 +0100 Subject: [PATCH] Allow to override the HttpClient This possibility will allow user of the lib to define their custom http client implementation. One of the use case I have in mind right now is caching. There is existing package that do caching on http client directly. It could also be used for unit tests --- pinboard.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pinboard.go b/pinboard.go index 8d0e515..0f18647 100644 --- a/pinboard.go +++ b/pinboard.go @@ -59,6 +59,8 @@ var ( } pinboardToken = "" + + httpClient = &http.Client{} ) // get checks if endpoint is a valid Pinboard API endpoint and then @@ -103,7 +105,7 @@ func get(endpoint string, options interface{}) (body []byte, err error) { u.RawQuery = q.Encode() // Call APImethod with fully constructed URL. - res, err := http.Get(u.String()) + res, err := httpClient.Get(u.String()) if err != nil { return nil, err } @@ -209,3 +211,9 @@ func values(i interface{}) (url.Values, error) { func SetToken(token string) { pinboardToken = token } + +// SetHttpClient sets teh actual http client instance to use to make calls. +// This can be overridden for caching purposes for example +func SetHttpClient(client *http.Client) { + httpClient = client +}