-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from VeritasOS/feature/initial-client
Feature: initial client code & TravisCI pipeline
- Loading branch information
Showing
5 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
language: go | ||
go: | ||
- 1.9.2 | ||
- latest | ||
script: | ||
- go get -u golang.org/x/tools/cmd/goimports && goimports -l -d . | ||
- gofmt -l -d -s . | ||
- go get -u github.com/golang/lint/golint && golint ./... | ||
- go vet ./... | ||
- cd uptrends && go test -v -cover |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
# Gopkg.toml example | ||
# | ||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package uptrends | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type clientor interface { | ||
Do(*http.Request) (*http.Response, error) | ||
} | ||
|
||
type requestCreator interface { | ||
NewRequest(method, path string, body io.Reader) (*http.Request, error) | ||
} | ||
|
||
type httpReqCreator struct{} | ||
|
||
func (h *httpReqCreator) NewRequest(method, path string, body io.Reader) (*http.Request, error) { | ||
return http.NewRequest(method, path, body) | ||
} | ||
|
||
// Client is an Uptrends.com API Client | ||
type Client struct { | ||
baseURL url.URL | ||
clientor | ||
username string | ||
password string | ||
requestCreator | ||
} | ||
|
||
// New returns an Uptrends.com API Client | ||
// username is the Uptrends.com login user | ||
// password is the corresponding password for username | ||
func New(username string, password string) (*Client, error) { | ||
u, err := url.Parse("https://api.uptrends.com") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Client{ | ||
*u, | ||
&http.Client{ | ||
Timeout: time.Second * 10, | ||
}, | ||
username, | ||
password, | ||
&httpReqCreator{}, | ||
}, nil | ||
} | ||
|
||
func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request, error) { | ||
url := c.baseURL | ||
url.Path = strings.Join([]string{"", "v3", path}, "/") | ||
req, err := c.NewRequest(method, url.String(), body) | ||
if err != nil { | ||
return req, err | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
req.Header.Add("Accept", "application/json, text/json, */*; q=0.1") | ||
|
||
req.SetBasicAuth(c.username, c.password) | ||
|
||
return req, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package uptrends | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
var ( | ||
u = "[email protected]" | ||
p = "Cryp7!cP@$$w0rd" | ||
) | ||
|
||
type mockReqFailingCreator struct{} | ||
|
||
func (m *mockReqFailingCreator) NewRequest(method, path string, body io.Reader) (req *http.Request, err error) { | ||
return req, errors.New("failed to create a request") | ||
} | ||
|
||
type mockReqCreator struct{} | ||
|
||
func (m *mockReqCreator) NewRequest(method, path string, body io.Reader) (req *http.Request, err error) { | ||
req, err = http.NewRequest(method, path, body) | ||
return req, err | ||
} | ||
|
||
func TestClientNew(t *testing.T) { | ||
c, err := New(u, p) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if c.username != u { | ||
t.Errorf("username %s, expected %s", c.username, u) | ||
} | ||
if c.password != p { | ||
t.Errorf("password %s, expected %s", c.password, p) | ||
} | ||
n := url.URL{} | ||
if c.baseURL == n { | ||
t.Errorf("baseURL nil, expected net/url.URL") | ||
} | ||
} | ||
|
||
func TestClientNewRequest(t *testing.T) { | ||
|
||
c := &Client{ | ||
url.URL{}, | ||
&http.Client{}, | ||
u, | ||
p, | ||
&mockReqFailingCreator{}, | ||
} | ||
|
||
r, err := c.newRequest("GET", "foo", nil) | ||
if err == nil { | ||
t.Fatalf("test request construction didn't error as expected: %s", err) | ||
} | ||
|
||
c = &Client{ | ||
url.URL{}, | ||
&http.Client{}, | ||
u, | ||
p, | ||
&mockReqCreator{}, | ||
} | ||
|
||
r, err = c.newRequest("GET", "foo", nil) | ||
if err != nil { | ||
t.Fatalf("test request construction errored: %s", err) | ||
} | ||
if r == nil { | ||
t.Fatalf("test request construction failed: %s", err) | ||
} | ||
if r.URL.Path != "/v3/foo" { | ||
t.Fatalf("test request wrong path. expected 'v3/foo', constructed '%s'", r.URL.Path) | ||
} | ||
|
||
c, err = New(u, p) | ||
if err != nil { | ||
t.Fatalf("test client creation errored: %s", err) | ||
} | ||
|
||
r, err = c.newRequest("GET", "foo", nil) | ||
if err != nil { | ||
t.Fatalf("test request construction errored: %s", err) | ||
} | ||
if r == nil { | ||
t.Fatalf("test request construction failed: %s", err) | ||
} | ||
if r.URL.Path != "/v3/foo" { | ||
t.Fatalf("test request wrong path. expected 'v3/foo', constructed '%s'", r.URL.Path) | ||
} | ||
} |