Skip to content

Commit

Permalink
Merge pull request #99 from gliptak/billingprofile1
Browse files Browse the repository at this point in the history
Implement UserBillingProfile
  • Loading branch information
jamesog committed Jan 8, 2017
2 parents 01acbdd + 5b7666a commit 055e0a9
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
46 changes: 46 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ type UserResponse struct {
Result User `json:"result"`
}

// userBillingProfileResponse wraps a response containing Billing Profile information.
type userBillingProfileResponse struct {
Response
Result UserBillingProfile
}

// UserBillingProfile contains Billing Profile information.
type UserBillingProfile struct {
ID string `json:"id,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Address string `json:"address,omitempty"`
Address2 string `json:"address2,omitempty"`
Company string `json:"company,omitempty"`
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
ZipCode string `json:"zipcode,omitempty"`
Country string `json:"country,omitempty"`
Telephone string `json:"telephone,omitempty"`
CardNumber string `json:"card_number,omitempty"`
CardExpiryYear int `json:"card_expiry_year,omitempty"`
CardExpiryMonth int `json:"card_expiry_month,omitempty"`
VAT string `json:"vat,omitempty"`
CreatedOn *time.Time `json:"created_on,omitempty"`
EditedOn *time.Time `json:"edited_on,omitempty"`
}

// UserDetails provides information about the logged-in user.
// API reference:
// https://api.cloudflare.com/#user-user-details
Expand Down Expand Up @@ -68,3 +95,22 @@ func (api *API) UpdateUser(user *User) (User, error) {

return r.Result, nil
}

// UserBillingProfile returns the billing profile of the user.
// API reference:
// https://api.cloudflare.com/#user-billing-profile
// GET /user/billing/profile
func (api *API) UserBillingProfile() (UserBillingProfile, error) {
var r userBillingProfileResponse
res, err := api.makeRequest("GET", "/user/billing/profile", nil)
if err != nil {
return UserBillingProfile{}, errors.Wrap(err, errMakeRequestError)
}

err = json.Unmarshal(res, &r)
if err != nil {
return UserBillingProfile{}, errors.Wrap(err, errUnmarshalError)
}

return r.Result, nil
}
63 changes: 63 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,66 @@ func TestUser_UpdateUser(t *testing.T) {
assert.Equal(t, userOut, want, "structs not equal")
}
}

func TestUser_UserBillingProfile(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/user/billing/profile", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "0020c268dbf54e975e7fe8563df49d52",
"first_name": "Bob",
"last_name": "Smith",
"address": "123 3rd St.",
"address2": "Apt 123",
"company": "Cloudflare",
"city": "San Francisco",
"state": "CA",
"zipcode": "12345",
"country": "US",
"telephone": "+1 111-867-5309",
"card_number": "xxxx-xxxx-xxxx-1234",
"card_expiry_year": 2015,
"card_expiry_month": 4,
"vat": "aaa-123-987",
"edited_on": "2014-04-01T12:21:02.0000Z",
"created_on": "2014-03-01T12:21:02.0000Z"
}
}`)
})

createdOn, _ := time.Parse(time.RFC3339, "2014-03-01T12:21:02.0000Z")
editedOn, _ := time.Parse(time.RFC3339, "2014-04-01T12:21:02.0000Z")

userBillingProfile, err := client.UserBillingProfile()

want := UserBillingProfile{
ID: "0020c268dbf54e975e7fe8563df49d52",
FirstName: "Bob",
LastName: "Smith",
Address: "123 3rd St.",
Address2: "Apt 123",
Company: "Cloudflare",
City: "San Francisco",
State: "CA",
ZipCode: "12345",
Country: "US",
Telephone: "+1 111-867-5309",
CardNumber: "xxxx-xxxx-xxxx-1234",
CardExpiryYear: 2015,
CardExpiryMonth: 4,
VAT: "aaa-123-987",
CreatedOn: &createdOn,
EditedOn: &editedOn,
}

if assert.NoError(t, err) {
assert.Equal(t, userBillingProfile, want, "structs not equal")
}
}

0 comments on commit 055e0a9

Please sign in to comment.