Skip to content

Commit

Permalink
Merge pull request #10 from joeblubaugh/master
Browse files Browse the repository at this point in the history
Support setting a custom http.Client in darksky.
  • Loading branch information
shawntoffel committed Nov 20, 2017
2 parents 34a2cb3 + 0817c84 commit 3b84db8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
11 changes: 9 additions & 2 deletions darksky.go
Expand Up @@ -2,6 +2,8 @@ package darksky

import (
"fmt"
"net/http"

"github.com/google/go-querystring/query"
)

Expand All @@ -17,11 +19,16 @@ type DarkSky interface {

type darkSky struct {
ApiKey string
Client *http.Client
}

// New creates a new DarkSky client
func New(apiKey string) DarkSky {
return &darkSky{apiKey}
return &darkSky{apiKey, &http.Client{}}
}

func NewWithClient(apiKey string, client *http.Client) DarkSky {
return &darkSky{apiKey, client}
}

// Forecast request a forecast
Expand All @@ -30,7 +37,7 @@ func (d *darkSky) Forecast(request ForecastRequest) (ForecastResponse, error) {

url := d.buildRequestUrl(request)

err := get(url, &response)
err := get(d.Client, url, &response)

return response, err
}
Expand Down
3 changes: 1 addition & 2 deletions rest.go
Expand Up @@ -9,7 +9,7 @@ import (
"net/http"
)

func get(url string, output interface{}) error {
func get(client *http.Client, url string, output interface{}) error {
req, err := http.NewRequest("GET", url, nil)

if err != nil {
Expand All @@ -19,7 +19,6 @@ func get(url string, output interface{}) error {
req.Header.Add("Content-Type", "application/json; charset=utf-8")
req.Header.Add("Accept-Encoding", "gzip")

client := http.Client{}
response, err := client.Do(req)

if err != nil {
Expand Down

0 comments on commit 3b84db8

Please sign in to comment.