-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions + tests for handling coinbasepro profiles aka portfolios (
#100)
- Loading branch information
Showing
2 changed files
with
125 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,49 @@ | ||
package coinbasepro | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type Profile struct { | ||
ID string `json:"id"` | ||
UserID string `json:"user_id"` | ||
Name string `json:"name"` | ||
Active bool `json:"active"` | ||
IsDefault bool `json:"is_default"` | ||
CreatedAt Time `json:"created_at,string"` | ||
} | ||
|
||
type ProfileTransfer struct { | ||
From string `json:"from"` | ||
To string `json:"to"` | ||
Currency string `json:"currency"` | ||
Amount string `json:"amount"` | ||
} | ||
|
||
// Client Funcs | ||
|
||
// GetProfiles retrieves a list of profiles | ||
func (c *Client) GetProfiles() ([]Profile, error) { | ||
var profiles []Profile | ||
|
||
url := fmt.Sprintf("/profiles") | ||
_, err := c.Request("GET", url, nil, &profiles) | ||
return profiles, err | ||
} | ||
|
||
// GetProfile retrieves a single profile | ||
func (c *Client) GetProfile(id string) (Profile, error) { | ||
var profile Profile | ||
|
||
url := fmt.Sprintf("/profiles/%s", id) | ||
_, err := c.Request("GET", url, nil, &profile) | ||
return profile, err | ||
} | ||
|
||
// CreateProfileTransfer transfers a currency amount from one profile to another | ||
func (c *Client) CreateProfileTransfer(newTransfer *ProfileTransfer) error { | ||
url := fmt.Sprintf("/profiles/transfer") | ||
_, err := c.Request("POST", url, newTransfer, nil) | ||
|
||
return 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,76 @@ | ||
package coinbasepro | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestGetProfiles(t *testing.T) { | ||
client := NewTestClient() | ||
profiles, err := client.GetProfiles() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
for _, p := range profiles { | ||
if p.ID == "" { | ||
t.Error(errors.New("profile id missing")) | ||
} | ||
} | ||
} | ||
|
||
func TestGetProfile(t *testing.T) { | ||
client := NewTestClient() | ||
profiles, err := client.GetProfiles() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
profile, err := client.GetProfile(profiles[0].ID) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if profile.ID == "" { | ||
t.Error(errors.New("profile id missing")) | ||
} | ||
} | ||
|
||
func TestCreateProfileTransfer(t *testing.T) { | ||
client := NewTestClient() | ||
profiles, err := client.GetProfiles() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
var fromProfile, toProfile Profile | ||
for _, profile := range profiles { | ||
if profile.IsDefault { | ||
fromProfile = profile | ||
continue | ||
} | ||
|
||
if profile.Active { | ||
toProfile = profile | ||
break | ||
} | ||
} | ||
|
||
if toProfile.ID == "" { | ||
t.Skip(fmt.Sprintf("needed at least two active profiles for this test")) | ||
} | ||
|
||
// Send from first profile to second profile | ||
newTransfer := ProfileTransfer{ | ||
From: fromProfile.ID, | ||
To: toProfile.ID, | ||
Currency: "USD", | ||
Amount: "9.99", | ||
} | ||
|
||
err = client.CreateProfileTransfer(&newTransfer) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |
67d3834
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Philip,
I was wondering if I did break the last build with my commit or might there be a problem with the authentication? Because normally there should always exist at least one profile (default profile).
67d3834
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's an issue with travis-ci. travis-ci is shutting down so I'm going to migrate these tests to github actions.