Skip to content

Commit

Permalink
Support further endpoints and simplify boilerplate code
Browse files Browse the repository at this point in the history
  • Loading branch information
000xE committed Dec 27, 2024
1 parent ac69caf commit 5df1bfa
Show file tree
Hide file tree
Showing 14 changed files with 756 additions and 33 deletions.
23 changes: 9 additions & 14 deletions endpoints/classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,24 @@ type Class struct {
const Classes = "classes"

func (client *RestClient) GetClasses(ctx context.Context, sport string) ([]Class, error) {
classes := []Class{}

err := Get(client, ctx, sport, Classes, url.Values{}, &classes)

return classes, err
return getClasses(client, ctx, sport, "", nil)
}

func (client *RestClient) GetClassesByID(ctx context.Context, sport string, id int) ([]Class, error) {
classes := []Class{}

values := url.Values{}
values.Set("id", fmt.Sprintf("eq.%v", id))

err := Get(client, ctx, sport, Classes, values, &classes)

return classes, err
return getClasses(client, ctx, sport, "id", id)
}

func (client *RestClient) GetClassesByAlpha(ctx context.Context, sport string, alpha string) ([]Class, error) {
return getClasses(client, ctx, sport, "alpha", alpha)
}

func getClasses(client *RestClient, ctx context.Context, sport string, query string, id any) ([]Class, error) {
classes := []Class{}

values := url.Values{}
values.Set("alpha", fmt.Sprintf("eq.%v", alpha))
if query != "" && id != nil {
values.Set(query, fmt.Sprintf("eq.%v", id))
}

err := Get(client, ctx, sport, Classes, values, &classes)

Expand Down
15 changes: 10 additions & 5 deletions endpoints/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func Get[T any](client *RestClient, ctx context.Context, sport string, resource
path := fmt.Sprintf("https://%v.sportdevs.com/%v", sport, resource)

url := url.URL{Path: path, RawQuery: values.Encode()}
req, err := http.NewRequest("GET", url.RequestURI(), nil)
urlStr := url.RequestURI()

req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
return err
}
Expand All @@ -69,11 +71,14 @@ func Get[T any](client *RestClient, ctx context.Context, sport string, resource
return err
}

if res.StatusCode != http.StatusOK {
return fmt.Errorf("%v failed: %v", urlStr, res.StatusCode)
}

defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
if body, err := io.ReadAll(res.Body); err != nil {
return err
} else {
return json.Unmarshal(body, &response)
}

return json.Unmarshal(body, &response)
}
23 changes: 9 additions & 14 deletions endpoints/countries.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,24 @@ type Country struct {
const Countries = "countries"

func (client *RestClient) GetCountries(ctx context.Context, sport string) ([]Country, error) {
countries := []Country{}

err := Get(client, ctx, sport, Countries, url.Values{}, &countries)

return countries, err
return getCountries(client, ctx, sport, "", nil)
}

func (client *RestClient) GetCountriesByID(ctx context.Context, sport string, id int) ([]Country, error) {
countries := []Country{}

values := url.Values{}
values.Set("id", fmt.Sprintf("eq.%v", id))

err := Get(client, ctx, sport, Countries, values, &countries)

return countries, err
return getCountries(client, ctx, sport, "id", id)
}

func (client *RestClient) GetCountriesByAlpha(ctx context.Context, sport string, alpha string) ([]Country, error) {
return getCountries(client, ctx, sport, "alpha", alpha)
}

func getCountries(client *RestClient, ctx context.Context, sport string, query string, id any) ([]Country, error) {
countries := []Country{}

values := url.Values{}
values.Set("alpha", fmt.Sprintf("eq.%v", alpha))
if query != "" && id != nil {
values.Set(query, fmt.Sprintf("eq.%v", id))
}

err := Get(client, ctx, sport, Countries, values, &countries)

Expand Down
49 changes: 49 additions & 0 deletions endpoints/injuries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package endpoints

import (
"context"
"fmt"
"net/url"
"time"
)

type Injury struct {
PlayerID int `json:"player_id"`
PlayerName string `json:"player_name"`
PlayerHashImage string `json:"player_hash_image"`
Type string `json:"type"`
Reason string `json:"reason"`
MatchID int `json:"match_id"`
SeasonID int `json:"season_id"`
TournamentID int `json:"tournament_id"`
StartTimestamp time.Time `json:"start_timestamp"`
}

const Injuries = "injuries"

func (client *RestClient) GetInjuriesByPlayerID(ctx context.Context, sport string, id int) ([]Injury, error) {
return getInjuries(client, ctx, sport, "player_id", id)
}

func (client *RestClient) GetInjuriesByMatchID(ctx context.Context, sport string, id int) ([]Injury, error) {
return getInjuries(client, ctx, sport, "match_id", id)
}

func (client *RestClient) GetInjuriesBySeasonID(ctx context.Context, sport string, id int) ([]Injury, error) {
return getInjuries(client, ctx, sport, "season_id", id)
}

func (client *RestClient) GetInjuriesByTournamentID(ctx context.Context, sport string, id int) ([]Injury, error) {
return getInjuries(client, ctx, sport, "tournament_id", id)
}

func getInjuries(client *RestClient, ctx context.Context, sport string, query string, id int) ([]Injury, error) {
injuries := []Injury{}

values := url.Values{}
values.Set(query, fmt.Sprintf("eq.%v", id))

err := Get(client, ctx, sport, Injuries, values, &injuries)

return injuries, err
}
50 changes: 50 additions & 0 deletions endpoints/media.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package endpoints

import (
"context"
"fmt"
"net/url"
"time"
)

type Media struct {
ID int `json:"id"`
TeamID int `json:"team_id,omitempty"`
PlayerID int `json:"player_id,omitempty"`
LeagueID int `json:"league_id,omitempty"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
URL string `json:"url"`
ThumbnailURL string `json:"thumbnail_url"`
DatePublished time.Time `json:"date_published"`
ChannelURL string `json:"channel_url"`
}

const (
MediaTeams = "media-teams"
MediaPlayers = "media-players"
MediaLeagues = "media-leagues"
)

func (client *RestClient) GetMediaByTeamID(ctx context.Context, sport string, id int) ([]Media, error) {
return getMedia(client, ctx, sport, MediaTeams, "team_id", id)
}

func (client *RestClient) GetMediaByPlayerID(ctx context.Context, sport string, id int) ([]Media, error) {
return getMedia(client, ctx, sport, MediaPlayers, "player_id", id)
}

func (client *RestClient) GetMediaByLeagueID(ctx context.Context, sport string, id int) ([]Media, error) {
return getMedia(client, ctx, sport, MediaLeagues, "league_id", id)
}

func getMedia(client *RestClient, ctx context.Context, sport string, resource string, query string, id int) ([]Media, error) {
media := []Media{}

values := url.Values{}
values.Set(query, fmt.Sprintf("eq.%v", id))

err := Get(client, ctx, sport, resource, values, &media)

return media, err
}
83 changes: 83 additions & 0 deletions endpoints/news.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package endpoints

import (
"context"
"fmt"
"net/url"
"time"
)

type (
Subtitle struct {
Text string `json:"text"`
Subtitle string `json:"subtitle"`
}

News struct {
MatchID int `json:"match_id"`
Date time.Time `json:"date"`
Title string `json:"title"`
Subtitles []Subtitle `json:"subtitles"`
}
)

type AggNews struct {
ID int `json:"id"`
TeamID int `json:"team_id,omitempty"`
MatchID int `json:"match_id,omitempty"`
PlayerID int `json:"player_id,omitempty"`
LeagueID int `json:"league_id,omitempty"`
Title string `json:"title"`
Link string `json:"link"`
ThumbnailURL string `json:"thumbnail_url"`
Description string `json:"description"`
PublishedDate time.Time `json:"published_date"`
SourceURL string `json:"source_url"`
Source string `json:"source"`
}

const (
NewsMatches = "news-matches"
AggNewsTeams = "news-agg-teams"
AggNewsMatches = "news-agg-matches"
AggNewsLeagues = "news-agg-leagues"
AggNewsPlayers = "news-agg-players"
)

func (client *RestClient) GetNewsByMatchID(ctx context.Context, sport string, id int) ([]News, error) {
news := []News{}

values := url.Values{}
values.Set("match_id", fmt.Sprintf("eq.%v", id))

err := Get(client, ctx, sport, NewsMatches, values, &news)

return news, err
}

func (client *RestClient) GetAggNewsByTeamID(ctx context.Context, sport string, id int) ([]AggNews, error) {
return getAggNews(client, ctx, sport, AggNewsTeams, "team_id", id)
}

func (client *RestClient) GetAggNewsByMatchID(ctx context.Context, sport string, id int) ([]AggNews, error) {
return getAggNews(client, ctx, sport, AggNewsMatches, "match_id", id)
}

func (client *RestClient) GetAggNewsByLeagueID(ctx context.Context, sport string, id int) ([]AggNews, error) {
return getAggNews(client, ctx, sport, AggNewsLeagues, "league_id", id)
}

func (client *RestClient) GetAggNewsByPlayerID(ctx context.Context, sport string, id int) ([]AggNews, error) {
return getAggNews(client, ctx, sport, AggNewsPlayers, "player_id", id)
}

func getAggNews(client *RestClient, ctx context.Context, sport string, resource string, query string, id int) ([]AggNews, error) {
news := []AggNews{}

values := url.Values{}
values.Set(query, fmt.Sprintf("eq.%v", id))

err := Get(client, ctx, sport, resource, values, &news)

return news, err
}
81 changes: 81 additions & 0 deletions endpoints/standings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package endpoints

import (
"context"
"fmt"
"net/url"
)

type (
Competitor struct {
Wins int `json:"wins"`
Draws int `json:"draws"`
Losses int `json:"losses"`
Points int `json:"points"`
Matches int `json:"matches"`
TeamID int `json:"team_id"`
Position int `json:"position"`
TeamName string `json:"team_name"`
ScoresFor int `json:"scores_for"`
ScoresAgainst int `json:"scores_against"`
TeamHashImage string `json:"team_hash_image"`
}

Standing struct {
ID int `json:"id"`
TournamentID int `json:"tournament_id"`
Type string `json:"type"`
Name string `json:"name"`
SeasonID int `json:"season_id"`
SeasonName string `json:"season_name"`
LeagueID int `json:"league_id"`
LeagueName string `json:"league_name"`
LeagueHashImage string `json:"league_hash_image"`
Competitors []Competitor `json:"competitors"`
}
)

const Standings = "standings"

func (client *RestClient) GetStandings(ctx context.Context, sport string) ([]Standing, error) {
return getStandings(client, ctx, sport, "", nil)
}

func (client *RestClient) GetStandingsByID(ctx context.Context, sport string, id int) ([]Standing, error) {
return getStandings(client, ctx, sport, "id", id)
}

func (client *RestClient) GetStandingsByLeagueSeasonTypeID(ctx context.Context, sport string, id int, seasonID int, standingType string) ([]Standing, error) {
standings := []Standing{}

values := url.Values{}

if id != 0 {
values.Set("league_id", fmt.Sprintf("eq.%v", id))
}

if seasonID != 0 {
values.Set("season_id", fmt.Sprintf("eq.%v", seasonID))
}

if standingType != "" {
values.Set("type", fmt.Sprintf("eq.%v", standingType))
}

err := Get(client, ctx, sport, Standings, values, &standings)

return standings, err
}

func getStandings(client *RestClient, ctx context.Context, sport string, query string, id any) ([]Standing, error) {
standings := []Standing{}

values := url.Values{}
if query != "" && id != nil {
values.Set(query, fmt.Sprintf("eq.%v", id))
}

err := Get(client, ctx, sport, Standings, values, &standings)

return standings, err
}
Loading

0 comments on commit 5df1bfa

Please sign in to comment.