move extractpatches function to api
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user