Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fmt:

## Format code and sort imports.
format-code: fmt imports
.PHONY: format-project
.PHONY: format-code

## Installs vendored tools.
install-tools:
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Go Strava API client generated from [API spec](https://developers.strava.com/swa

[Usage examples](examples/client_example_test.go)

The examples are live integration tests and require a Strava access token:

```shell
STRAVA_ACCESS_TOKEN=... go test -tags=integration ./examples
```

### Usage

```go
Expand Down Expand Up @@ -46,4 +52,3 @@ func main() {
}

```

2 changes: 1 addition & 1 deletion client/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (a activitiesService) CreateActivity(ctx context.Context, name string, acti
}

if sportType != "" {
params.SetType(&sportType)
params.SetSportType(sportType)
}

params.SetStartDateLocal(strfmt.DateTime(startDateLocal))
Expand Down
18 changes: 17 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package client

import (
openapiruntime "github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"

apiclient "github.com/obalunenko/strava-api/internal/gen/strava-api-go/client"
Expand All @@ -24,7 +25,8 @@ type APIClient struct {

// NewAPIClient creates a new APIClient. Requires a Strava API token.
func NewAPIClient(token string) (*APIClient, error) {
client := apiclient.Default
client := apiclient.NewHTTPClient(nil)
registerRouteExportConsumers(client.Transport)

// Set the bearer token for auth.
auth := httptransport.BearerToken(token)
Expand All @@ -41,3 +43,17 @@ func NewAPIClient(token string) (*APIClient, error) {
Uploads: newUploadsApiService(client, auth),
}, nil
}

func registerRouteExportConsumers(transport openapiruntime.ClientTransport) {
runtimeTransport, ok := transport.(*httptransport.Runtime)
if !ok {
return
}

consumer := openapiruntime.ByteStreamConsumer()
runtimeTransport.Consumers["application/gpx+xml"] = consumer
runtimeTransport.Consumers["application/tcx+xml"] = consumer
runtimeTransport.Consumers["application/vnd.garmin.tcx+xml"] = consumer
runtimeTransport.Consumers[openapiruntime.XMLMime] = consumer
runtimeTransport.Consumers[openapiruntime.TextMime] = consumer
}
30 changes: 16 additions & 14 deletions client/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"bytes"
"context"

"github.com/go-openapi/runtime"
Expand All @@ -13,13 +14,13 @@ import (
// RoutesAPI is an interface for interacting with routes endpoints of Strava API
type RoutesAPI interface {
// GetRouteAsGPX returns a GPX file of the route
GetRouteAsGPX(ctx context.Context, id int64) error
GetRouteAsGPX(ctx context.Context, id int64) ([]byte, error)
// GetRouteAsTCX returns a TCX file of the route
GetRouteAsTCX(ctx context.Context, id int64) error
GetRouteAsTCX(ctx context.Context, id int64) ([]byte, error)
// GetRouteById returns a route with given id
GetRouteById(ctx context.Context, id int64) (models.Route, error)
// GetRoutesByAthleteId returns a list of routes for the given athlete
GetRoutesByAthleteId(ctx context.Context, opts ...GetRoutesByAthleteIdOpts) ([]models.Route, error)
GetRoutesByAthleteId(ctx context.Context, id int64, opts ...GetRoutesByAthleteIdOpts) ([]models.Route, error)
}

type GetRoutesByAthleteIdOpts struct {
Expand All @@ -39,34 +40,34 @@ type routesService struct {
auth runtime.ClientAuthInfoWriter
}

func (r routesService) GetRouteAsGPX(ctx context.Context, id int64) error {
func (r routesService) GetRouteAsGPX(ctx context.Context, id int64) ([]byte, error) {
params := routes.NewGetRouteAsGPXParams()

params.SetDefaults()
params.SetContext(ctx)
params.SetID(id)

_, err := r.client.Routes.GetRouteAsGPX(params, r.auth)
if err != nil {
return err
var buf bytes.Buffer
if _, err := r.client.Routes.GetRouteAsGPX(params, r.auth, &buf, routes.WithAcceptApplicationGpxXML); err != nil {
return nil, err
}

return nil
return buf.Bytes(), nil
}

func (r routesService) GetRouteAsTCX(ctx context.Context, id int64) error {
func (r routesService) GetRouteAsTCX(ctx context.Context, id int64) ([]byte, error) {
params := routes.NewGetRouteAsTCXParams()

params.SetDefaults()
params.SetContext(ctx)
params.SetID(id)

_, err := r.client.Routes.GetRouteAsTCX(params, r.auth)
if err != nil {
return err
var buf bytes.Buffer
if _, err := r.client.Routes.GetRouteAsTCX(params, r.auth, &buf, routes.WithAcceptApplicationVndGarminTcxXML); err != nil {
return nil, err
}

return nil
return buf.Bytes(), nil
}

func (r routesService) GetRouteById(ctx context.Context, id int64) (models.Route, error) {
Expand All @@ -84,11 +85,12 @@ func (r routesService) GetRouteById(ctx context.Context, id int64) (models.Route
return convertToModelsRoute(route.GetPayload()), nil
}

func (r routesService) GetRoutesByAthleteId(ctx context.Context, opts ...GetRoutesByAthleteIdOpts) ([]models.Route, error) {
func (r routesService) GetRoutesByAthleteId(ctx context.Context, id int64, opts ...GetRoutesByAthleteIdOpts) ([]models.Route, error) {
params := routes.NewGetRoutesByAthleteIDParams()

params.SetDefaults()
params.SetContext(ctx)
params.SetID(id)

for _, opt := range opts {
if opt.Page != nil {
Expand Down
56 changes: 0 additions & 56 deletions deployments/docker-compose/go-tools-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,68 +23,12 @@ services:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/style/fmt.sh'

run-tests:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/tests/run.sh'

run-tests-coverage:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/tests/coverage.sh'

prepare-cover-report:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/tests/prepare-cover-report.sh'

update-readme-coverage:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/update-readme-coverage.sh'

lint-full:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/linting/run-linters.sh'

lint-pipeline:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/linting/golangci-pipeline.sh'

lint-sonar:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/linting/golangci-sonar.sh'

go-generate:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/codegen/go-generate.sh'

release-local-snapshot:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/release/local-snapshot-release.sh'
environment:
APP_NAME: ${APP_NAME}
GOOS: ${GOOS}
GOARCH: ${GOARCH}
GITHUB_TOKEN: ${GITHUB_TOKEN:-}

release-check-config:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/release/check.sh'
environment:
APP_NAME: ${APP_NAME}
GITHUB_TOKEN: ${GITHUB_TOKEN:-}

release:
extends:
service: tools
entrypoint: /bin/sh -c 'git config --global --add safe.directory /app && ./scripts/release/release.sh'
environment:
APP_NAME: ${APP_NAME}
GITHUB_TOKEN: ${GITHUB_TOKEN:-}
31 changes: 28 additions & 3 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,14 @@
"summary": "List Athlete Routes",
"operationId": "getRoutesByAthleteId",
"parameters": [
{
"type": "integer",
"format": "int64",
"description": "The identifier of the athlete.",
"name": "id",
"in": "path",
"required": true
},
{
"type": "integer",
"description": "Page number. Defaults to 1.",
Expand Down Expand Up @@ -1875,6 +1883,11 @@
],
"summary": "Export Route GPX",
"operationId": "getRouteAsGPX",
"produces": [
"application/gpx+xml",
"application/xml",
"text/xml"
],
"parameters": [
{
"type": "integer",
Expand All @@ -1887,7 +1900,10 @@
],
"responses": {
"200": {
"description": "A GPX file with the route."
"description": "A GPX file with the route.",
"schema": {
"type": "file"
}
},
"default": {
"description": "Unexpected error.",
Expand All @@ -1906,6 +1922,12 @@
],
"summary": "Export Route TCX",
"operationId": "getRouteAsTCX",
"produces": [
"application/tcx+xml",
"application/vnd.garmin.tcx+xml",
"application/xml",
"text/xml"
],
"parameters": [
{
"type": "integer",
Expand All @@ -1918,7 +1940,10 @@
],
"responses": {
"200": {
"description": "A TCX file with the route."
"description": "A TCX file with the route.",
"schema": {
"type": "file"
}
},
"default": {
"description": "Unexpected error.",
Expand Down Expand Up @@ -5407,4 +5432,4 @@
]
}
]
}
}
Loading