Go quickstart
clients/go is a thin client on the standard library. Getting the token is your
app's job (OAuth 2.1 with PKCE, found from the server's 401); for trying it out, borrow one.
A token
-
Template server: its fixed token,
dev-token(see Where to run them). -
Sandbox: sign in with the example app and print the token it gets:
cd partner-app && npm install npm start -- https://lifterclub-api.runmighty.dev --print-tokenIt lasts an hour.
Run it
mkdir ows-go && cd ows-go
go mod init example.com/ows-go
go get github.com/openexercise/open-workout/clients/go
main.go:
package main
import (
"context"
"fmt"
"log"
"os"
"time"
openworkout "github.com/openexercise/open-workout/clients/go"
)
func main() {
ctx := context.Background()
c := openworkout.New(os.Getenv("OWS_BASE_URL"), os.Getenv("OWS_TOKEN"))
reps, load := 5, 100.0
logged, err := c.LogWorkout(ctx, openworkout.WorkoutInput{
ExternalID: fmt.Sprintf("go-quickstart-%d", time.Now().Unix()),
Exercises: []openworkout.ExerciseInput{{
Name: "back squat",
Sets: []openworkout.SetInput{{Reps: &reps, Load: &load, Unit: openworkout.Kg}},
}},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(logged.Summary)
// Everything that changed in the last day, oldest first; deleted workouts as tombstones.
for ch, err := range c.Changes(ctx, openworkout.ChangesOptions{UpdatedSince: time.Now().Add(-24 * time.Hour), IncludeDeleted: true}) {
if err != nil {
log.Fatal(err)
}
if ch.Deleted != nil {
fmt.Println("deleted", ch.Deleted.ID)
} else {
fmt.Println("changed", ch.Workout.ID)
}
}
}
OWS_BASE_URL=https://lifterclub-api.runmighty.dev OWS_TOKEN=lca_… go run .
OWS_BASE_URL=http://localhost:8080 OWS_TOKEN=dev-token go run .
Next
- Errors are
*openworkout.Errorwith the problem'sCode;openworkout.HasCode(err, openworkout.CodeRateLimited)andRetryAftertell you when to try again. openworkout.VerifySignaturechecks a webhook delivery'sOWS-Signature.- To check a server you're building, run
ows conformance; itsconformancepackage also runs inside Go tests.