Skip to content

Commit

Permalink
feat: convert strava over to use generic request handler
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Dec 1, 2024
1 parent a82fd41 commit eb24644
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 117 deletions.
109 changes: 14 additions & 95 deletions internal/apis/strava/activities.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package strava

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"

Expand Down Expand Up @@ -68,32 +65,13 @@ type activity struct {
}

func fetchActivities(minioClient minio.Client, tokens tokens) ([]activity, error) {
req, err := http.NewRequest("GET", "https://www.strava.com/api/v3/athlete/activities", nil)
if err != nil {
lumber.Error(err, "creating request failed")
return nil, err
}
req.Header.Set("Authorization", "Bearer "+tokens.Access)

resp, err := http.DefaultClient.Do(req)
if err != nil {
lumber.Error(err, "sending request for Strava activities failed")
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
lumber.Error(err, "reading response body failed")
return nil, err
}

var stravaActivities []stravaActivity
err = json.Unmarshal(body, &stravaActivities)
stravaActivities, err := sendStravaAPIRequest[[]stravaActivity](
"api/v3/athlete/activities",
tokens,
)
if err != nil {
lumber.Error(err, "failed to parse json")
lumber.Debug(string(body))
return nil, err
lumber.Error(err, "failed to send request to Strava API to get activities")
return []activity{}, err
}

var activities []activity
Expand Down Expand Up @@ -148,85 +126,26 @@ func fetchHeartrate(id uint64, tokens tokens) []int {
"keys": {"heartrate"},
"resolution": {"low"},
}
req, err := http.NewRequest(
"GET",
fmt.Sprintf("https://www.strava.com/api/v3/activities/%d/streams?"+params.Encode(), id),
nil,
stream, err := sendStravaAPIRequest[struct{ Heartrate activityStream }](
fmt.Sprintf("api/v3/activities/%d/streams?%s", id, params.Encode()),
tokens,
)
if err != nil {
lumber.Error(err, "creating request failed")
return nil
}
req.Header.Set("Authorization", "Bearer "+tokens.Access)

resp, err := http.DefaultClient.Do(req)
if err != nil {
lumber.Error(err, "Failed to send request for HR data for", id)
return nil
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
lumber.Error(err, "reading response body failed")
return nil
}

if resp.StatusCode != http.StatusOK {
lumber.ErrorMsg("status code of", resp.StatusCode)
lumber.Debug(string(body))
return nil
}

var stream struct{ Heartrate activityStream }
err = json.Unmarshal(body, &stream)
if err != nil {
lumber.Error(err, "failed to parse json")
lumber.Debug(string(body))
return nil
lumber.Error(err, "failed to send request for HR data from activity with ID of", id)
}

return stream.Heartrate.Data
}

func fetchActivityDetails(id uint64, tokens tokens) (detailedStravaActivity, error) {
req, err := http.NewRequest(
"GET",
fmt.Sprintf("https://www.strava.com/api/v3/activities/%d", id),
nil,
details, err := sendStravaAPIRequest[detailedStravaActivity](
fmt.Sprintf("api/v3/activities/%d", id),
tokens,
)
if err != nil {
lumber.Error(err, "creating request failed")
lumber.Error(err, "failed to request detailed activity data for", id)
return detailedStravaActivity{}, err
}
req.Header.Set("Authorization", "Bearer "+tokens.Access)

resp, err := http.DefaultClient.Do(req)
if err != nil {
lumber.Error(err, "Failed to send request for activity details with an ID of", id)
return detailedStravaActivity{}, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
lumber.Error(err, "reading response body failed")
return detailedStravaActivity{}, err
}

if resp.StatusCode != http.StatusOK {
lumber.ErrorMsg("status code of", resp.StatusCode)
lumber.Debug(string(body))
return detailedStravaActivity{}, nil
}

var details detailedStravaActivity
err = json.Unmarshal(body, &details)
if err != nil {
lumber.Error(err, "failed to parse json")
lumber.Debug(string(body))
return detailedStravaActivity{}, nil
}

return details, nil
}
32 changes: 32 additions & 0 deletions internal/apis/strava/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package strava

import (
"net/http"
"net/url"

"github.com/gleich/lcp-v2/internal/apis"
"github.com/gleich/lumber/v3"
)

func sendStravaAPIRequest[T any](path string, tokens tokens) (T, error) {
var zeroValue T
u, err := url.JoinPath("https://www.strava.com/", path)
if err != nil {
lumber.Error(err, "failed to join URL")
return zeroValue, err
}

req, err := http.NewRequest("GET", u, nil)
if err != nil {
lumber.Error(err, "failed to create request")
return zeroValue, err
}
req.Header.Set("Authorization", "Bearer "+tokens.Access)

resp, err := apis.SendRequest[T](req)
if err != nil {
lumber.Error(err, "failed to make strava API request")
return zeroValue, err
}
return resp, nil
}
25 changes: 3 additions & 22 deletions internal/apis/strava/tokens.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package strava

import (
"encoding/json"
"io"
"net/http"
"net/url"
"time"

"github.com/gleich/lcp-v2/internal/apis"
"github.com/gleich/lcp-v2/internal/secrets"
"github.com/gleich/lumber/v3"
)
Expand Down Expand Up @@ -43,28 +42,10 @@ func (t *tokens) refreshIfNeeded() {
lumber.Error(err, "creating request for new token failed")
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
lumber.Error(err, "sending request for new data token failed")
return
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
lumber.Error(err, "reading response body failed")
return
}
if resp.StatusCode != http.StatusOK {
lumber.ErrorMsg(resp.StatusCode, "when trying to get new token data:", string(body))
return
}

var tokens tokens
err = json.Unmarshal(body, &tokens)
tokens, err := apis.SendRequest[tokens](req)
if err != nil {
lumber.Error(err, "failed to parse json")
lumber.Debug("body:", string(body))
lumber.Error(err, "failed to refresh tokens")
return
}

Expand Down

0 comments on commit eb24644

Please sign in to comment.