Horses and people › A horse's career record
A horse's career record
Find a horse, then read its career record: runs, wins, places and the span of them. Its breeding comes in the same reply where it has been fetched.
// A horse's career record
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
)
func get(path string, q url.Values) any {
req, _ := http.NewRequest("GET", "https://api.apihorseracing.com"+path+"?"+q.Encode(), nil)
req.Header.Set("X-API-Key", os.Getenv("AHR_KEY"))
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
var body map[string]any
json.NewDecoder(res.Body).Decode(&body)
if res.StatusCode != 200 {
panic(fmt.Sprint(body["error"]))
}
return body["data"]
}
func main() {
first := get("/v1/horses/search", url.Values{"name": {"Constitution Hill"}}).([]any)[0].(map[string]any)
data := get("/v1/horses/"+fmt.Sprint(first["horse_id"]), url.Values{})
d := data.(map[string]any)
fmt.Println("Horse:", d["name"])
fmt.Println("Runs:", d["career"].(map[string]any)["runs"])
fmt.Println("Wins:", d["career"].(map[string]any)["wins"])
fmt.Println("Seconds:", d["career"].(map[string]any)["seconds"])
fmt.Println("First run:", d["career"].(map[string]any)["first_run"])
}
What the reply holds
- career
- Runs, wins, places and the span of them.
- pedigree
- Sire, dam and damsire, each with its own id.
When it says no
401no_key The key is missing or mistyped. Every sample reads it from AHR_KEY.403plan_needed Your plan does not reach this. The reply names the plan that does.429rate_limited Too fast. Wait the seconds in Retry-After, then try again.