get rid of internal

This commit is contained in:
filifa 2025-04-06 01:15:34 -04:00
parent 0c4f307d29
commit 7526a60c0e
2 changed files with 16 additions and 17 deletions

View File

@ -29,7 +29,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"scm.dairydemon.net/filifa/mlbstats/api" "scm.dairydemon.net/filifa/mlbstats/api"
"scm.dairydemon.net/filifa/mlbstats/api/models" "scm.dairydemon.net/filifa/mlbstats/api/models"
"scm.dairydemon.net/filifa/mlbstats/cmd/internal/statsapi"
) )
var gamePk int32 var gamePk int32
@ -82,19 +81,19 @@ func patch(feed *models.BaseballGameRestObject, client *api.APIClient) error {
return err return err
} }
func newWebsocket(gamePk int32) (*statsapi.GamedayWebsocket, <-chan error, error) { func newWebsocket(gamePk int32) (*gamedayWebsocket, <-chan error, error) {
ws, err := statsapi.NewGamedayWebsocket(gamePk) ws, err := newGamedayWebsocket(gamePk)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
ch := make(chan error) ch := make(chan error)
go ws.KeepAlive(10*time.Second, ch) go ws.keepAlive(10*time.Second, ch)
return ws, ch, err return ws, ch, err
} }
func handleUnexpectedClose(feed *models.BaseballGameRestObject, client *api.APIClient) (*statsapi.GamedayWebsocket, error) { func handleUnexpectedClose(feed *models.BaseballGameRestObject, client *api.APIClient) (*gamedayWebsocket, error) {
ws, _, err := newWebsocket(feed.GamePk) ws, _, err := newWebsocket(feed.GamePk)
if err != nil { if err != nil {
return nil, err return nil, err
@ -105,10 +104,10 @@ func handleUnexpectedClose(feed *models.BaseballGameRestObject, client *api.APIC
return ws, err return ws, err
} }
func updateFeed(feed *models.BaseballGameRestObject, ws *statsapi.GamedayWebsocket, client *api.APIClient) error { func updateFeed(feed *models.BaseballGameRestObject, ws *gamedayWebsocket, client *api.APIClient) error {
var p statsapi.Push var p push
err := ws.ReadJSON(&p) err := ws.ReadJSON(&p)
if websocket.IsUnexpectedCloseError(err, statsapi.GameFinalCode, statsapi.GameUnavailableCode) { if websocket.IsUnexpectedCloseError(err, GameFinalCode, GameUnavailableCode) {
log.Println(err) log.Println(err)
newWs, err := handleUnexpectedClose(feed, client) newWs, err := handleUnexpectedClose(feed, client)

View File

@ -13,7 +13,7 @@ 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 You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package statsapi package cmd
import ( import (
"net/url" "net/url"
@ -25,7 +25,7 @@ import (
// Push is a struct for holding data sent to us through the websocket that we // Push is a struct for holding data sent to us through the websocket that we
// care about. // care about.
type Push struct { type push struct {
UpdateId string UpdateId string
} }
@ -36,15 +36,15 @@ const (
// GamedayWebsocket is a struct for establishing a websocket connection with // GamedayWebsocket is a struct for establishing a websocket connection with
// the Stats API. // the Stats API.
type GamedayWebsocket struct { type gamedayWebsocket struct {
baseURL url.URL baseURL url.URL
*websocket.Conn *websocket.Conn
} }
// NewGamedayWebsocket creates a statsapi.GamedayWebsocket using the Stats API // NewGamedayWebsocket creates a statsapi.GamedayWebsocket using the Stats API
// websocket URL and establishes a connection. // websocket URL and establishes a connection.
func NewGamedayWebsocket(gamePk int32) (*GamedayWebsocket, error) { func newGamedayWebsocket(gamePk int32) (*gamedayWebsocket, error) {
ws := GamedayWebsocket{ ws := gamedayWebsocket{
baseURL: url.URL{ baseURL: url.URL{
Scheme: "wss", Scheme: "wss",
Host: "ws.statsapi.mlb.com", Host: "ws.statsapi.mlb.com",
@ -55,7 +55,7 @@ func NewGamedayWebsocket(gamePk int32) (*GamedayWebsocket, error) {
return &ws, err return &ws, err
} }
func (g *GamedayWebsocket) init(gamePk int32) error { func (g *gamedayWebsocket) init(gamePk int32) error {
endpoint := url.URL{ endpoint := url.URL{
Path: "api/v1/game/push/subscribe/gameday/" + strconv.Itoa(int(gamePk)), Path: "api/v1/game/push/subscribe/gameday/" + strconv.Itoa(int(gamePk)),
} }
@ -69,7 +69,7 @@ func (g *GamedayWebsocket) init(gamePk int32) error {
// SendKeepAlive sends the keep-alive message observed to be sent by MLB // SendKeepAlive sends the keep-alive message observed to be sent by MLB
// Gameday ("Gameday5"). // Gameday ("Gameday5").
func (g *GamedayWebsocket) SendKeepAlive() error { func (g *gamedayWebsocket) sendKeepAlive() error {
msg := []byte("Gameday5") msg := []byte("Gameday5")
err := g.Conn.WriteMessage(websocket.TextMessage, msg) err := g.Conn.WriteMessage(websocket.TextMessage, msg)
return err return err
@ -77,12 +77,12 @@ func (g *GamedayWebsocket) SendKeepAlive() error {
// KeepAlive sends the keep-alive message on a certain interval and sends any // KeepAlive sends the keep-alive message on a certain interval and sends any
// errors to ch. // errors to ch.
func (g *GamedayWebsocket) KeepAlive(interval time.Duration, ch chan<- error) { func (g *gamedayWebsocket) keepAlive(interval time.Duration, ch chan<- error) {
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
defer ticker.Stop() defer ticker.Stop()
for range ticker.C { for range ticker.C {
err := g.SendKeepAlive() err := g.sendKeepAlive()
if err != nil { if err != nil {
ch <- err ch <- err
} }