Skip to content

Commit

Permalink
Add Account API endpoint[ch28502] (#41)
Browse files Browse the repository at this point in the history
* Add Account API endpoint[ch28502]

* code review, removed omitempty and error field from account struct
  • Loading branch information
MariaBraganca authored Mar 12, 2021
1 parent a401318 commit 651a11c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ api.MetricsListActivities(&Cursor{}, "customerUUID")
```


### Account

Availiable methods:

```go
api.RetrieveAccount()
```


### Errors

The library returns parsed errors inside the structs as from the REST API,
Expand Down
20 changes: 20 additions & 0 deletions account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package chartmogul

const (
accountEndpoint = "account"
)

// Account details in ChartMogul
type Account struct {
Name string `json:"name"`
Currency string `json:"currency"`
TimeZone string `json:"time_zone"`
WeekStartOn string `json:"week_start_on"`
}

// RetrieveAccount returns details of current account.
func (api API) RetrieveAccount() (*Account, error) {
result := &Account{}
accountUUID := ""
return result, api.retrieve(accountEndpoint, accountUUID, result)
}
51 changes: 51 additions & 0 deletions account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package chartmogul

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/davecgh/go-spew/spew"
)

const accountExample = `{
"name": "Example Test Company",
"currency": "EUR",
"time_zone": "Europe/Berlin",
"week_start_on": "sunday"
}`

func TestRetrieveAccount(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
expectedMethod := "GET"
if r.Method != expectedMethod {
t.Errorf("Requested method expected: %v, actual: %v", expectedMethod, r.Method)
}
expected := "/v/account"
path := r.URL.Path
if path != expected {
t.Errorf("Requested path expected: %v, actual: %v", expected, path)
w.WriteHeader(http.StatusNotFound)
}
w.Write([]byte(accountExample)) //nolint
}))
defer server.Close()
SetURL(server.URL + "/v/%v")

var tested IApi = &API{
AccountToken: "token",
AccessKey: "key",
}
account, err := tested.RetrieveAccount()

if account.Name != "Example Test Company" || account.Currency != "EUR" || account.TimeZone != "Europe/Berlin" || account.WeekStartOn != "sunday" {
spew.Dump(account)
t.Error("Unexpected account details")
}
if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
}
3 changes: 3 additions & 0 deletions chartmogul.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ type IApi interface {
// Metrics - Subscriptions & Activities
MetricsListSubscriptions(cursor *Cursor, customerUUID string) (*MetricsSubscriptions, error)
MetricsListActivities(cursor *Cursor, customerUUID string) (*MetricsActivities, error)

// Account
RetrieveAccount() (*Account, error)
}

// API is the handle for communicating with Chartmogul.
Expand Down
5 changes: 4 additions & 1 deletion generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ func (api API) list(path string, output interface{}, query ...interface{}) error
return wrapErrors(res, body, errs)
}

// RETRIEVE
func (api API) retrieve(path string, uuid string, output interface{}) error {
var res gorequest.Response
var body []byte
var errs []error
path = strings.Replace(path, ":uuid", uuid, 1)
if uuid != "" {
path = strings.Replace(path, ":uuid", uuid, 1)
}

// nolint:errcheck
backoff.Retry(func() error {
Expand Down

0 comments on commit 651a11c

Please sign in to comment.