Skip to content

Commit

Permalink
Merge pull request #6 from bzimmer/with-baseurl
Browse files Browse the repository at this point in the history
improved library testability
  • Loading branch information
bzimmer authored Oct 17, 2021
2 parents da6e32f + 4ad40b6 commit 1d8561b
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 23 deletions.
22 changes: 17 additions & 5 deletions cyclinganalytics/cyclinganalytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
"golang.org/x/oauth2"
)

const baseURL = "https://www.cyclinganalytics.com/api"
const _baseURL = "https://www.cyclinganalytics.com/api"

// Client for accessing Cycling Analytics' API
type Client struct {
config oauth2.Config
token *oauth2.Token
client *http.Client
config oauth2.Config
token *oauth2.Token
client *http.Client
baseURL string

User *UserService
Rides *RidesService
Expand All @@ -39,6 +40,17 @@ func withServices() Option {
return func(c *Client) error {
c.User = &UserService{client: c}
c.Rides = &RidesService{client: c}
if c.baseURL == "" {
c.baseURL = _baseURL
}
return nil
}
}

// WithBaseURL specifies the base url
func WithBaseURL(baseURL string) Option {
return func(c *Client) error {
c.baseURL = baseURL
return nil
}
}
Expand All @@ -47,7 +59,7 @@ func (c *Client) newAPIRequest(ctx context.Context, method, uri string, values *
if c.token.AccessToken == "" {
return nil, errors.New("accessToken required")
}
q := fmt.Sprintf("%s/%s", baseURL, uri)
q := fmt.Sprintf("%s/%s", c.baseURL, uri)
if values != nil {
q = fmt.Sprintf("%s?%s", q, values.Encode())
}
Expand Down
15 changes: 14 additions & 1 deletion cyclinganalytics/cyclinganalytics_with.go

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

2 changes: 1 addition & 1 deletion cyclinganalytics/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (r *Ride) GPX() (*gpx.GPX, error) {
}

trk := gpx.NewTrkType(mls)
trk.Src = baseURL
trk.Src = _baseURL

return &gpx.GPX{
Creator: activity.UserAgent,
Expand Down
1 change: 1 addition & 0 deletions cyclinganalytics/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// Fault represents an error response
type Fault struct {
Code int `json:"code"`
Message string `json:"error"`
}

Expand Down
2 changes: 1 addition & 1 deletion rwgps/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (t *Trip) GeoJSON() (*geojson.Feature, error) {
Properties: map[string]interface{}{
"type": t.Type,
"name": t.Name,
"source": baseURL,
"source": _baseURL,
},
}
return g, nil
Expand Down
1 change: 1 addition & 0 deletions rwgps/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (

// Fault is an error
type Fault struct {
Code int `json:"code"`
Message string `json:"message"`
}

Expand Down
22 changes: 17 additions & 5 deletions rwgps/rwgps.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import (

const (
apiVersion = "2"
baseURL = "https://ridewithgps.com"
_baseURL = "https://ridewithgps.com"
)

// Client for communicating with RWGPS
type Client struct {
config oauth2.Config
token *oauth2.Token
client *http.Client
config oauth2.Config
token *oauth2.Token
client *http.Client
baseURL string

Users *UsersService
Trips *TripsService
Expand All @@ -38,12 +39,23 @@ func withServices() Option {
return func(c *Client) error {
c.Users = &UsersService{client: c}
c.Trips = &TripsService{client: c}
if c.baseURL == "" {
c.baseURL = _baseURL
}
return nil
}
}

// WithBaseURL specifies the base url
func WithBaseURL(baseURL string) Option {
return func(c *Client) error {
c.baseURL = baseURL
return nil
}
}

func (c *Client) newAPIRequest(ctx context.Context, uri string, params map[string]string) (*http.Request, error) {
u, err := url.Parse(fmt.Sprintf("%s/%s", baseURL, uri))
u, err := url.Parse(fmt.Sprintf("%s/%s", c.baseURL, uri))
if err != nil {
return nil, err
}
Expand Down
15 changes: 14 additions & 1 deletion rwgps/rwgps_with.go

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

2 changes: 1 addition & 1 deletion rwgps/trips.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (s *TripsService) Upload(ctx context.Context, file *activity.File) (*Upload
return nil, err
}

uri := fmt.Sprintf("%s/trips.json", baseURL)
uri := fmt.Sprintf("%s/trips.json", s.client.baseURL)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, &b)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions strava/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Error struct {

// Fault contains errors
type Fault struct {
Code int `json:"code"`
Message string `json:"message"`
Errors []*Error `json:"errors"`
}
Expand Down
15 changes: 14 additions & 1 deletion strava/strava_with.go

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

1 change: 1 addition & 0 deletions zwift/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const datetimeFormat = `"2006-01-02T15:04:05+0000"`

// Fault represents a Zwift error
type Fault struct {
Code int `json:"code"`
Message string `json:"message"`
}

Expand Down
4 changes: 2 additions & 2 deletions zwift/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func (s *ProfileService) Profile(ctx context.Context, profileID string) (*Profil
if err != nil {
return nil, err
}
var profile *Profile
var profile Profile
if err = s.client.do(req, &profile); err != nil {
return nil, err
}
return profile, err
return &profile, err
}
20 changes: 16 additions & 4 deletions zwift/zwift.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

//go:generate genwith --do --client --token --ratelimit --package zwift

const baseURL = "https://us-or-rly101.zwift.com"
const _baseURL = "https://us-or-rly101.zwift.com"
const userAgent = "CNL/3.4.1 (Darwin Kernel 20.3.0) zwift/1.0.61590 curl/7.64.1"

// Endpoint is Zwifts's OAuth 2.0 endpoint
Expand All @@ -26,8 +26,9 @@ func Endpoint() oauth2.Endpoint {

// Client for communicating with Zwift
type Client struct {
token *oauth2.Token
client *http.Client
token *oauth2.Token
client *http.Client
baseURL string

Auth *AuthService
Activity *ActivityService
Expand All @@ -44,6 +45,17 @@ func withServices() Option {
c.Profile = &ProfileService{c}
c.Activity = &ActivityService{c}
c.token.TokenType = "bearer"
if c.baseURL == "" {
c.baseURL = _baseURL
}
return nil
}
}

// WithBaseURL specifies the base url
func WithBaseURL(baseURL string) Option {
return func(c *Client) error {
c.baseURL = baseURL
return nil
}
}
Expand All @@ -52,7 +64,7 @@ func (c *Client) newAPIRequest(ctx context.Context, method, uri string) (*http.R
if c.token.AccessToken == "" {
return nil, errors.New("accessToken required")
}
q := fmt.Sprintf("%s/%s", baseURL, uri)
q := fmt.Sprintf("%s/%s", c.baseURL, uri)
u, err := url.Parse(q)
if err != nil {
return nil, err
Expand Down
15 changes: 14 additions & 1 deletion zwift/zwift_with.go

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

0 comments on commit 1d8561b

Please sign in to comment.