Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
Decouple http client
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Aug 16, 2019
1 parent 8015411 commit d4943f6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ gravo
/grafana-storage
seihon
manifest.json
dist
Empty file added go.sum
Empty file.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
17 changes: 10 additions & 7 deletions volkszaehler/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
}
}

Expand Down

0 comments on commit d4943f6

Please sign in to comment.