|
| 1 | +# Strava API Client - AI Coding Agent Instructions |
| 2 | + |
| 3 | +This is a **generated Go client wrapper** around the Strava API v3, built using code generation from OpenAPI specs and containerized tooling. |
| 4 | + |
| 5 | +## Architecture Overview |
| 6 | + |
| 7 | +### Generated Client + Wrapper Pattern |
| 8 | +- **Generated code**: `internal/gen/strava-api-go/` contains swagger-generated client (DO NOT EDIT) |
| 9 | +- **Wrapper layer**: `client/*.go` provides user-friendly interfaces over generated code |
| 10 | +- **Pattern**: Each API group (Activities, Athletes, Clubs, etc.) has: |
| 11 | + - Interface in wrapper (e.g., `ActivitiesAPI`) |
| 12 | + - Implementation struct (e.g., `activitiesService`) |
| 13 | + - Constructor function (e.g., `newActivitiesApiService()`) |
| 14 | + |
| 15 | +### Code Generation Workflow |
| 16 | +```bash |
| 17 | +# Update API spec |
| 18 | +make update-swagger-spec # Downloads latest from Strava |
| 19 | + |
| 20 | +# Regenerate client |
| 21 | +make codegen # Runs: gen → sync-vendor → format-code → vet |
| 22 | +``` |
| 23 | + |
| 24 | +Key files: |
| 25 | +- `internal/generate.go` - Contains `//go:generate` directive for swagger codegen |
| 26 | +- `docs/swagger.json` - Strava API specification (source of truth) |
| 27 | +- Generated client uses go-openapi runtime with Bearer token auth |
| 28 | + |
| 29 | +## Development Workflow |
| 30 | + |
| 31 | +### Docker-Based Tooling |
| 32 | +All linting/formatting/testing uses containerized tools via `ghcr.io/obalunenko/go-tools`: |
| 33 | +```bash |
| 34 | +# Primary commands (use these, not direct go commands) |
| 35 | +make lint-full # Full linting suite |
| 36 | +make fmt # Format code |
| 37 | +make imports # Fix import sorting |
| 38 | +make vet # Static analysis |
| 39 | + |
| 40 | +# Docker compose wraps all tools |
| 41 | +docker compose -f deployments/docker-compose/go-tools-docker-compose.yml up --exit-code-from lint-full lint-full |
| 42 | +``` |
| 43 | + |
| 44 | +### Testing Pattern |
| 45 | +- Tests in `examples/client_example_test.go` serve as integration tests and usage documentation |
| 46 | +- Tests require `STRAVA_ACCESS_TOKEN` environment variable |
| 47 | +- Helper functions: `getToken(t)` for auth, `printJSON(t, v)` for debug output |
| 48 | +- Uses `github.com/obalunenko/getenv` for environment variable handling |
| 49 | + |
| 50 | +## Code Patterns & Conventions |
| 51 | + |
| 52 | +### Wrapper Interface Design |
| 53 | +```go |
| 54 | +// Each API group follows this pattern: |
| 55 | +type ActivitiesAPI interface { |
| 56 | + // Method with context first, required params, then options |
| 57 | + GetActivityById(ctx context.Context, id int64, opts ...GetActivityByIdOpts) (models.DetailedActivity, error) |
| 58 | +} |
| 59 | + |
| 60 | +// Options structs for optional parameters |
| 61 | +type GetActivityByIdOpts struct { |
| 62 | + IncludeAllEfforts *bool `json:"include_all_efforts,omitempty"` |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### Error Handling |
| 67 | +- Custom errors in `client/errors.go` (e.g., `ErrTooManyOptions`) |
| 68 | +- Generated client errors passed through from go-openapi |
| 69 | +- No error wrapping - direct return of underlying errors |
| 70 | + |
| 71 | +### Dependencies & Integration |
| 72 | +- **Auth**: Bearer token via `httptransport.BearerToken(token)` |
| 73 | +- **Generated models**: Use `internal/gen/strava-api-go/models.*` types directly |
| 74 | +- **Time handling**: `strfmt.DateTime` for API compatibility |
| 75 | +- **Testing**: `github.com/stretchr/testify` with `require.*` assertions |
| 76 | + |
| 77 | +## File Organization |
| 78 | + |
| 79 | +``` |
| 80 | +client/ # User-facing wrapper interfaces |
| 81 | +├── client.go # Main APIClient struct and constructor |
| 82 | +├── activities.go # Activities API wrapper |
| 83 | +├── athletes.go # Athletes API wrapper |
| 84 | +└── errors.go # Custom error types |
| 85 | +
|
| 86 | +internal/gen/ # Generated code (DO NOT EDIT) |
| 87 | +examples/ # Integration tests & usage documentation |
| 88 | +scripts/ # Build and development scripts |
| 89 | +deployments/ # Docker compose configurations |
| 90 | +``` |
| 91 | + |
| 92 | +## Key Constraints |
| 93 | + |
| 94 | +1. **Never edit generated code** in `internal/gen/` - regenerate via `make codegen` |
| 95 | +2. **Use containerized tools** - don't run go tools directly, use Make targets |
| 96 | +3. **Follow wrapper pattern** - new APIs need interface + implementation + constructor |
| 97 | +4. **Context-first parameters** in all API methods |
| 98 | +5. **Options structs** for optional parameters instead of many function parameters |
| 99 | + |
| 100 | +## Common Tasks |
| 101 | + |
| 102 | +- **Add new API method**: Implement in relevant `client/*.go` file following existing patterns |
| 103 | +- **Update API spec**: Run `make update-swagger-spec` then `make codegen` |
| 104 | +- **Debug API calls**: Add test in `examples/` with `printJSON()` helper |
| 105 | +- **Fix formatting**: Run `make format-code` (includes fmt + imports) |
0 commit comments