From d4943f6ff0bda06c29204bd0620ca24816306828 Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 16 Aug 2019 15:42:20 +0200 Subject: [PATCH] Decouple http client --- .gitignore | 1 + go.sum | 0 main.go | 3 ++- volkszaehler/client.go | 17 ++++++++++------- 4 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index fd76449..b9aac37 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ gravo /grafana-storage seihon manifest.json +dist diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go index aaae9ba..4b55ea3 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,8 @@ func main() { os.Exit(0) } - client := volkszaehler.NewClient(*apiURL, apiTimeout, *verbose) + httpClient := http.Client{Timeout: *apiTimeout} + client := volkszaehler.NewClient(*apiURL, &httpClient, *verbose) server := newServer(client) http.HandleFunc("/", handler(server.rootHandler, *verbose)) diff --git a/volkszaehler/client.go b/volkszaehler/client.go index 715403e..6e8842c 100644 --- a/volkszaehler/client.go +++ b/volkszaehler/client.go @@ -13,6 +13,11 @@ import ( "time" ) +// HTTPDoer is the http client Do() interface +type HTTPDoer interface { + Do(req *http.Request) (*http.Response, error) +} + // Client is the volkszaehler API client type Client interface { Get(endpoint string) (io.ReadCloser, error) @@ -25,18 +30,16 @@ type Client interface { type client struct { url string - client http.Client + client HTTPDoer debug bool } // NewClient creates new volkszaehler api client -func NewClient(url string, timeout *time.Duration, debug bool) Client { +func NewClient(url string, httpClient HTTPDoer, debug bool) Client { return &client{ - url: url, - client: http.Client{ - Timeout: *timeout, - }, - debug: debug, + url: url, + client: httpClient, + debug: debug, } }