Skip to content

Commit

Permalink
Merge pull request #3 from VeritasOS/feature/initial-client
Browse files Browse the repository at this point in the history
Feature: initial client code & TravisCI pipeline
  • Loading branch information
jesselang authored Dec 8, 2017
2 parents 0e7a6c0 + 341ef68 commit 02a867c
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
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
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Gopkg.toml
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"

68 changes: 68 additions & 0 deletions uptrends/client.go
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
}
95 changes: 95 additions & 0 deletions uptrends/client_test.go
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)
}
}

0 comments on commit 02a867c

Please sign in to comment.