move extractpatches function to api

This commit is contained in:
filifa
2024-07-14 11:32:09 -05:00
parent a8e055d094
commit 500bf7baa4
2 changed files with 30 additions and 28 deletions

View File

@@ -1,9 +1,12 @@
package statsapi
import (
"encoding/json"
"io"
"net/http"
"net/url"
"github.com/evanphx/json-patch/v5"
)
var DefaultClient = NewClient(http.DefaultClient)
@@ -35,6 +38,32 @@ func NewClient(c *http.Client) *Client {
}
}
func ExtractPatches(diffPatch []byte) ([]jsonpatch.Patch, error) {
var patches []jsonpatch.Patch
var objs []map[string]any
err := json.Unmarshal(diffPatch, &objs)
if err != nil {
return patches, err
}
for _, obj := range objs {
diff := obj["diff"]
patch, err := json.Marshal(diff)
if err != nil {
break
}
p, err := jsonpatch.DecodePatch(patch)
if err != nil {
break
}
patches = append(patches, p)
}
return patches, err
}
func (c *Client) RequestSchedule(sportId, teamId string) ([]byte, error) {
endpoint := url.URL{Path: "api/v1/schedule"}
query := endpoint.Query()