Skip to content

Commit

Permalink
Remove appName requirement
Browse files Browse the repository at this point in the history
This isn't needed as the token is registered with an app or user.
  • Loading branch information
lildude committed Dec 22, 2020
1 parent fe9b70a commit 7c84729
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 50 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ go get -u 'github.com/lildude/runalyze'

You will need an access token to query the API. You can generate tokens in your account at [Personal API](https://runalyze.com/settings/personal-api).

When using the client, you will also need to supply a name of your application. This is used in the user agent when querying the API and is used to identify applications that are accessing the API and enable Runalyze to contact the application author if there are problems. So pick a name that stands out!

With your token and application name, you can then access the API using approach similar to this:
With your token, you can then access the API using approach similar to this:

```go
package main
Expand All @@ -36,11 +34,7 @@ import (
func main() {
godotenv.Load(".env")
ctx := context.Background()
cfg := runalyze.Configuration{
AppName: "My Cool App/3.2.1",
Token: os.Getenv("RUNALYZE_ACCESS_TOKEN"),
}
cl := runalyze.NewClient(cfg)
cl := runalyze.NewClient(os.Getenv("RUNALYZE_ACCESS_TOKEN"))

startSleep, _ := time.Parse(time.RFC3339, "2020-11-07T23:00:00Z")
sleep := runalyze.Sleep{
Expand Down
14 changes: 3 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
var (
// BaseURLV1 is Runalyze's v1 API endpoint
BaseURLV1 = "https://runalyze.com/api/v1/"
version = "dev"
userAgent = fmt.Sprintf("go-runalyze/%s", version)
userAgent = "go-runalyze"
)

// Client holds configuration items for the Runalyze client and provides methods that interact with the Runalyze API.
Expand All @@ -29,19 +28,12 @@ type Client struct {
client *http.Client
}

// Configuration holds the client information used when initialising the client
type Configuration struct {
AppName string
Token string
}

// NewClient returns a new Runalyze API client.
func NewClient(cfg Configuration) *Client {
func NewClient(token string) *Client {
cc := http.DefaultClient
baseURL, _ := url.Parse(BaseURLV1)
ua := fmt.Sprintf("%s (%s)", cfg.AppName, userAgent)

c := &Client{baseURL: baseURL, UserAgent: ua, apiToken: cfg.Token, client: cc}
c := &Client{baseURL: baseURL, UserAgent: userAgent, apiToken: token, client: cc}
return c
}

Expand Down
20 changes: 4 additions & 16 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@ import (
// TestNewClient confirms that a client can be created with the default baseURL
// and default User-Agent.
func TestNewClient(t *testing.T) {
cfg := Configuration{
AppName: "Testing/0.0.1",
Token: "123456",
}
c := NewClient(cfg)
c := NewClient("123456")

assert.Equal(t, BaseURLV1, c.baseURL.String(), "should configure the client to use the default url")
assert.Equal(t, fmt.Sprintf("Testing/0.0.1 (go-runalyze/%s)", version), c.UserAgent)
assert.Equal(t, "go-runalyze", c.UserAgent)
assert.Equal(t, "123456", c.apiToken, "should configure the client to use the API token")
}

// TestNewRequest confirms that NewRequest returns an API request with the
// correct URL, a correctly encoded body and the correct User-Agent and
// Content-Type headers set.
func TestNewRequest(t *testing.T) {
cfg := Configuration{
AppName: "Testing/0.0.1",
Token: "123456",
}
c := NewClient(cfg)
c := NewClient("123456")

t.Run("valid request", func(tc *testing.T) {

Expand Down Expand Up @@ -214,11 +206,7 @@ func setup() (client *Client, mux *http.ServeMux, serverURL string, teardown fun
mux = http.NewServeMux()
server := httptest.NewServer(mux)

cfg := Configuration{
AppName: "Testing/0.0.1",
Token: "123456",
}
c := NewClient(cfg)
c := NewClient("123456")
url, _ := url.Parse(server.URL + "/")
c.baseURL = url

Expand Down
6 changes: 1 addition & 5 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ different functions of the Runalyze API. All of the API calls require an access
token which should be passed when initialising the client. For example:
ctx := context.Background()
cfg := runalyze.Configuration{
AppName: "My Cool App/3.2.1",
Token: "RUNALYZE_ACCESS_TOKEN",
}
client := runalyze.NewClient(cfg)
client := runalyze.NewClient(os.Getenv("RUNALYZE_ACCESS_TOKEN"))
// Create a new sleep entry
startSleep, _ := time.Parse(time.RFC3339, "2020-11-07T23:00:00Z")
Expand Down
12 changes: 2 additions & 10 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import (
func Example_sleep() {
godotenv.Load(".env")
ctx := context.Background()
cfg := runalyze.Configuration{
AppName: "go-runalyze Testing",
Token: os.Getenv("RUNALYZE_ACCESS_TOKEN"),
}
cl := runalyze.NewClient(cfg)
cl := runalyze.NewClient(os.Getenv("RUNALYZE_ACCESS_TOKEN"))

startSleep, _ := time.Parse(time.RFC3339, "2020-11-07T23:00:00Z")
sleep := runalyze.Sleep{
Expand All @@ -43,11 +39,7 @@ func Example_sleep() {
func Example_bloodPressure() {
godotenv.Load(".env")
ctx := context.Background()
cfg := runalyze.Configuration{
AppName: "go-runalyze Testing",
Token: os.Getenv("RUNALYZE_ACCESS_TOKEN"),
}
cl := runalyze.NewClient(cfg)
cl := runalyze.NewClient(os.Getenv("RUNALYZE_ACCESS_TOKEN"))

date, _ := time.Parse(time.RFC3339, "2020-11-07T23:00:00Z")
bp := runalyze.BloodPressure{
Expand Down

0 comments on commit 7c84729

Please sign in to comment.