From 921afb43ea051fe2050718335bc60a298fe0374d Mon Sep 17 00:00:00 2001 From: oliha Date: Thu, 2 May 2019 13:49:14 +0100 Subject: [PATCH 1/2] set default HTTP client --- clients/horizonclient/client.go | 10 +++++++++- clients/horizonclient/main.go | 2 ++ clients/horizonclient/main_test.go | 12 ++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/clients/horizonclient/client.go b/clients/horizonclient/client.go index b83200e572..5a43113e4e 100644 --- a/clients/horizonclient/client.go +++ b/clients/horizonclient/client.go @@ -53,7 +53,7 @@ func (c *Client) sendRequestURL(requestURL string, method string, a interface{}) return errors.Wrap(err, "error creating HTTP request") } c.setClientAppHeaders(req) - + c.fixHTTPOnce.Do(c.fixHTTP) if c.horizonTimeOut == 0 { c.horizonTimeOut = HorizonTimeOut } @@ -93,6 +93,7 @@ func (c *Client) stream( return errors.Wrap(err, "error creating HTTP request") } req.Header.Set("Accept", "text/event-stream") + c.fixHTTPOnce.Do(c.fixHTTP) // to do: confirm name and version c.setClientAppHeaders(req) @@ -203,6 +204,13 @@ func (c *Client) setClientAppHeaders(req *http.Request) { req.Header.Set("X-App-Version", c.AppVersion) } +// fixHTTP sets the default HTTP client when non is provided. +func (c *Client) fixHTTP() { + if c.HTTP == nil { + c.HTTP = http.DefaultClient + } +} + // fixHorizonURL strips all slashes(/) at the end of HorizonURL if any, then adds a single slash func (c *Client) fixHorizonURL() string { return strings.TrimRight(c.HorizonURL, "/") + "/" diff --git a/clients/horizonclient/main.go b/clients/horizonclient/main.go index e5c3b68b49..b9fc52c5b1 100644 --- a/clients/horizonclient/main.go +++ b/clients/horizonclient/main.go @@ -15,6 +15,7 @@ import ( "errors" "net/http" "net/url" + "sync" "time" hProtocol "github.com/stellar/go/protocols/horizon" @@ -116,6 +117,7 @@ type Client struct { AppName string AppVersion string isTestNet bool + fixHTTPOnce sync.Once } // ClientInterface contains methods implemented by the horizon client diff --git a/clients/horizonclient/main_test.go b/clients/horizonclient/main_test.go index a8b62d9760..4d6cd87125 100644 --- a/clients/horizonclient/main_test.go +++ b/clients/horizonclient/main_test.go @@ -2,6 +2,7 @@ package horizonclient import ( "fmt" + "net/http" "testing" "time" @@ -293,6 +294,17 @@ func ExampleClient_Fund() { fmt.Print(resp) } +func TestFixHTTP(t *testing.T) { + client := &Client{ + HorizonURL: "https://localhost/", + } + // No HTTP client is provided + assert.Nil(t, client.HTTP, "client HTTP is nil") + client.Root() + // When a request is made, default HTTP client is set + assert.IsType(t, client.HTTP, &http.Client{}) +} + func TestClientFund(t *testing.T) { hmock := httptest.NewClient() client := &Client{ From f067e7d185eb0073359d91cbf7b90db8ac869b77 Mon Sep 17 00:00:00 2001 From: oliha Date: Thu, 2 May 2019 17:38:38 +0100 Subject: [PATCH 2/2] changes from review --- clients/horizonclient/client.go | 8 ++++---- clients/horizonclient/main.go | 2 -- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/clients/horizonclient/client.go b/clients/horizonclient/client.go index 5a43113e4e..15c6915e5b 100644 --- a/clients/horizonclient/client.go +++ b/clients/horizonclient/client.go @@ -53,7 +53,7 @@ func (c *Client) sendRequestURL(requestURL string, method string, a interface{}) return errors.Wrap(err, "error creating HTTP request") } c.setClientAppHeaders(req) - c.fixHTTPOnce.Do(c.fixHTTP) + c.setDefaultClient() if c.horizonTimeOut == 0 { c.horizonTimeOut = HorizonTimeOut } @@ -93,7 +93,7 @@ func (c *Client) stream( return errors.Wrap(err, "error creating HTTP request") } req.Header.Set("Accept", "text/event-stream") - c.fixHTTPOnce.Do(c.fixHTTP) + c.setDefaultClient() // to do: confirm name and version c.setClientAppHeaders(req) @@ -204,8 +204,8 @@ func (c *Client) setClientAppHeaders(req *http.Request) { req.Header.Set("X-App-Version", c.AppVersion) } -// fixHTTP sets the default HTTP client when non is provided. -func (c *Client) fixHTTP() { +// setDefaultClient sets the default HTTP client when none is provided. +func (c *Client) setDefaultClient() { if c.HTTP == nil { c.HTTP = http.DefaultClient } diff --git a/clients/horizonclient/main.go b/clients/horizonclient/main.go index b9fc52c5b1..e5c3b68b49 100644 --- a/clients/horizonclient/main.go +++ b/clients/horizonclient/main.go @@ -15,7 +15,6 @@ import ( "errors" "net/http" "net/url" - "sync" "time" hProtocol "github.com/stellar/go/protocols/horizon" @@ -117,7 +116,6 @@ type Client struct { AppName string AppVersion string isTestNet bool - fixHTTPOnce sync.Once } // ClientInterface contains methods implemented by the horizon client