From 3ddb5e26326edeb46754ae48accc2c9887ba2bbe Mon Sep 17 00:00:00 2001 From: filifa Date: Tue, 8 Apr 2025 00:52:21 -0400 Subject: [PATCH] refactor --- cmd/subscribe.go | 69 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/cmd/subscribe.go b/cmd/subscribe.go index a7b18e8..3021031 100644 --- a/cmd/subscribe.go +++ b/cmd/subscribe.go @@ -32,6 +32,20 @@ import ( var gamePk int32 +func extractTimestamp(body []byte) (any, error) { + var v any + err := json.Unmarshal(body, &v) + if err != nil { + return nil, err + } + + vobj := v.(map[string]any) + metaData := vobj["metaData"].(map[string]any) + timestamp := metaData["timeStamp"] + + return timestamp, nil +} + func extractPatches(resp []byte) ([]jsonpatch.Patch, error) { var patches []jsonpatch.Patch @@ -49,31 +63,30 @@ func extractPatches(resp []byte) ([]jsonpatch.Patch, error) { return patches, nil } -func patch(body []byte, client *api.APIClient) ([]byte, error) { - var v any - err := json.Unmarshal(body, &v) - if err != nil { - return body, err - } - - vobj := v.(map[string]any) - metaData := vobj["metaData"].(map[string]any) - timestamp := metaData["timeStamp"] - +func requestPatches(timestamp any, client *api.APIClient) ([]jsonpatch.Patch, error) { req := client.GameAPI.LiveGameDiffPatchV1(context.Background(), gamePk) req = req.StartTimecode(timestamp) resp, err := req.Execute() if err != nil { - return body, err + return nil, err } diffPatch, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + return extractPatches(diffPatch) +} + +func patch(body []byte, client *api.APIClient) ([]byte, error) { + timestamp, err := extractTimestamp(body) if err != nil { return body, err } - patches, err := extractPatches(diffPatch) + patches, err := requestPatches(timestamp, client) if err != nil { return body, err } @@ -100,40 +113,42 @@ func newWebsocket(gamePk int32) (*gamedayWebsocket, <-chan error, error) { return ws, ch, nil } -func handleUnexpectedClose(body []byte, client *api.APIClient) ([]byte, *gamedayWebsocket, error) { +func handleUnexpectedClose(client *api.APIClient) ([]byte, *gamedayWebsocket, error) { ws, _, err := newWebsocket(gamePk) if err != nil { - return body, nil, err + return nil, nil, err } req := client.GameAPI.LiveGameV1(context.Background(), gamePk) resp, err := req.Execute() if err != nil { - return body, ws, err + return nil, ws, err } - body, err = io.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) return body, ws, err } -func updateFeed(body []byte, ws *gamedayWebsocket, client *api.APIClient) ([]byte, error) { +func handlePush(ws *gamedayWebsocket, client *api.APIClient) ([]byte, error) { var p push err := ws.ReadJSON(&p) if websocket.IsUnexpectedCloseError(err, GameFinalCode, GameUnavailableCode) { log.Println(err) - body, newWs, err := handleUnexpectedClose(body, client) + body, newWs, err := handleUnexpectedClose(client) if err != nil { return body, err } *ws = *newWs return body, nil - } else if err != nil { - return body, err } - body, err = patch(body, client) + return nil, err +} + +func updateFeed(body []byte, client *api.APIClient) ([]byte, error) { + body, err := patch(body, client) if err != nil { req := client.GameAPI.LiveGameV1(context.Background(), gamePk) resp, err := req.Execute() @@ -172,7 +187,15 @@ func subscribe(cmd *cobra.Command, args []string) { for { fmt.Println(string(body)) - body, err = updateFeed(body, ws, statsAPIClient) + newBody, err := handlePush(ws, statsAPIClient) + if err != nil { + log.Fatal(err) + } else if newBody != nil { + body = newBody + continue + } + + body, err = updateFeed(body, statsAPIClient) if err != nil { log.Fatal(err) }