/* Copyright © 2024 filifa This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package statsapi import ( "encoding/json" "io" "net/http" "net/url" "github.com/evanphx/json-patch/v5" ) var DefaultClient = NewClient(http.DefaultClient) func RequestSchedule(sportId, teamId, date string) ([]byte, error) { return DefaultClient.RequestSchedule(sportId, teamId, date) } func RequestFeed(gamePk string) ([]byte, error) { return DefaultClient.RequestFeed(gamePk) } func RequestDiffPatch(gamePk, startTimecode, pushUpdateId string) (DiffPatchResponse, error) { return DefaultClient.RequestDiffPatch(gamePk, startTimecode, pushUpdateId) } func RequestContent(gamePk string) ([]byte, error) { return DefaultClient.RequestContent(gamePk) } func RequestStandings(leagueId string) ([]byte, error) { return DefaultClient.RequestStandings(leagueId) } type DiffPatchResponse []byte type Client struct { baseURL url.URL httpClient *http.Client } func NewClient(c *http.Client) *Client { return &Client{ baseURL: url.URL{ Scheme: "https", Host: "statsapi.mlb.com", }, httpClient: c, } } func (resp *DiffPatchResponse) ExtractPatches() ([]jsonpatch.Patch, error) { var patches []jsonpatch.Patch var objs []map[string]jsonpatch.Patch err := json.Unmarshal([]byte(*resp), &objs) if err != nil { return patches, err } for _, obj := range objs { patch := obj["diff"] patches = append(patches, patch) } return patches, err } func (c *Client) RequestSchedule(sportId, teamId, date string) ([]byte, error) { endpoint := url.URL{Path: "api/v1/schedule"} query := endpoint.Query() query.Add("sportId", sportId) query.Add("teamId", teamId) query.Add("date", date) endpoint.RawQuery = query.Encode() return c.get(&endpoint) } func (c *Client) RequestFeed(gamePk string) ([]byte, error) { endpoint := url.URL{Path: "api/v1.1/game/" + gamePk + "/feed/live"} return c.get(&endpoint) } func (c *Client) RequestDiffPatch(gamePk, startTimecode, pushUpdateId string) (DiffPatchResponse, error) { endpoint := url.URL{Path: "api/v1.1/game/" + gamePk + "/feed/live/diffPatch"} query := endpoint.Query() query.Add("language", "en") query.Add("startTimecode", startTimecode) query.Add("pushUpdateId", pushUpdateId) endpoint.RawQuery = query.Encode() return c.get(&endpoint) } func (c *Client) RequestContent(gamePk string) ([]byte, error) { endpoint := url.URL{Path: "api/v1/game/" + gamePk + "/content"} return c.get(&endpoint) } func (c *Client) RequestStandings(leagueId string) ([]byte, error) { endpoint := url.URL{Path: "api/v1/standings"} query := endpoint.Query() query.Add("leagueId", leagueId) endpoint.RawQuery = query.Encode() return c.get(&endpoint) } func (c *Client) get(endpoint *url.URL) ([]byte, error) { url := c.baseURL.ResolveReference(endpoint) resp, err := c.httpClient.Get(url.String()) if err != nil { return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) return body, err }