From d550f859a7525c1baa87a303f0d52f0e2028a0d0 Mon Sep 17 00:00:00 2001 From: nite Date: Sun, 6 Apr 2025 00:28:00 +1100 Subject: [PATCH] u --- .gitignore | 1 + README.md | 107 ++++-- assert.go | 41 ++ client.go | 197 ++++++++++ .../age_rating_categories.go | 10 +- .../age_rating_content_descriptions.go | 10 +- .../age_rating_content_descriptions_v2.go | 10 +- .../age_rating_organizations.go | 8 +- age_ratings.go => endpoint/age_ratings.go | 8 +- .../alternative_names.go | 8 +- artworks.go => endpoint/artworks.go | 8 +- endpoint/base.go | 38 ++ .../character_genders.go | 8 +- .../character_mug_shots.go | 8 +- .../character_species.go | 8 +- characters.go => endpoint/characters.go | 8 +- .../collection_membership_types.go | 8 +- .../collection_memberships.go | 8 +- .../collection_relation_types.go | 8 +- .../collection_relations.go | 8 +- .../collection_types.go | 8 +- collections.go => endpoint/collections.go | 8 +- companies.go => endpoint/companies.go | 8 +- company_logos.go => endpoint/company_logos.go | 8 +- .../company_statuses.go | 8 +- .../company_websites.go | 8 +- covers.go => endpoint/covers.go | 8 +- date_formats.go => endpoint/date_formats.go | 8 +- endpoint/endpoint.go | 151 -------- endpoint/endpoint_name.go | 151 ++++++++ event_logos.go => endpoint/event_logos.go | 8 +- .../event_networks.go | 8 +- events.go => endpoint/events.go | 8 +- .../external_game_sources.go | 8 +- .../external_games.go | 8 +- franchises.go => endpoint/franchises.go | 8 +- .../game_engine_logos.go | 8 +- game_engines.go => endpoint/game_engines.go | 8 +- .../game_localizations.go | 8 +- game_modes.go => endpoint/game_modes.go | 8 +- .../game_release_formats.go | 8 +- game_statuses.go => endpoint/game_statuses.go | 8 +- .../game_time_to_beats.go | 8 +- game_types.go => endpoint/game_types.go | 8 +- .../game_version_feature_values.go | 8 +- .../game_version_features.go | 8 +- game_versions.go => endpoint/game_versions.go | 8 +- game_videos.go => endpoint/game_videos.go | 8 +- endpoint/games.go | 29 ++ genres.go => endpoint/genres.go | 8 +- .../involved_companies.go | 8 +- keywords.go => endpoint/keywords.go | 8 +- .../language_support_types.go | 8 +- .../language_supports.go | 8 +- languages.go => endpoint/languages.go | 8 +- .../multiplayer_modes.go | 8 +- network_types.go => endpoint/network_types.go | 8 +- .../platform_families.go | 8 +- .../platform_logos.go | 8 +- .../platform_types.go | 8 +- .../platform_version_companies.go | 8 +- .../platform_version_release_dates.go | 8 +- .../platform_versions.go | 8 +- .../platform_websites.go | 8 +- platforms.go => endpoint/platforms.go | 8 +- .../player_perspectives.go | 8 +- .../popularity_primitives.go | 8 +- .../popularity_types.go | 8 +- regions.go => endpoint/regions.go | 8 +- .../release_date_regions.go | 8 +- .../release_date_statuses.go | 8 +- release_dates.go => endpoint/release_dates.go | 8 +- screenshots.go => endpoint/screenshots.go | 8 +- search.go => endpoint/search.go | 24 +- themes.go => endpoint/themes.go | 8 +- webhooks.go => endpoint/webhooks.go | 10 +- website_types.go => endpoint/website_types.go | 8 +- websites.go => endpoint/websites.go | 8 +- games.go | 55 --- igdb.go | 96 ----- register_endpoints.go | 358 ++++++++++++++++++ 81 files changed, 1264 insertions(+), 544 deletions(-) create mode 100644 assert.go create mode 100644 client.go rename age_rating_categories.go => endpoint/age_rating_categories.go (69%) rename age_rating_content_descriptions.go => endpoint/age_rating_content_descriptions.go (68%) rename age_rating_content_descriptions_v2.go => endpoint/age_rating_content_descriptions_v2.go (68%) rename age_rating_organizations.go => endpoint/age_rating_organizations.go (68%) rename age_ratings.go => endpoint/age_ratings.go (70%) rename alternative_names.go => endpoint/alternative_names.go (69%) rename artworks.go => endpoint/artworks.go (70%) create mode 100644 endpoint/base.go rename character_genders.go => endpoint/character_genders.go (69%) rename character_mug_shots.go => endpoint/character_mug_shots.go (69%) rename character_species.go => endpoint/character_species.go (69%) rename characters.go => endpoint/characters.go (70%) rename collection_membership_types.go => endpoint/collection_membership_types.go (68%) rename collection_memberships.go => endpoint/collection_memberships.go (69%) rename collection_relation_types.go => endpoint/collection_relation_types.go (68%) rename collection_relations.go => endpoint/collection_relations.go (69%) rename collection_types.go => endpoint/collection_types.go (69%) rename collections.go => endpoint/collections.go (70%) rename companies.go => endpoint/companies.go (70%) rename company_logos.go => endpoint/company_logos.go (70%) rename company_statuses.go => endpoint/company_statuses.go (69%) rename company_websites.go => endpoint/company_websites.go (69%) rename covers.go => endpoint/covers.go (71%) rename date_formats.go => endpoint/date_formats.go (70%) delete mode 100644 endpoint/endpoint.go create mode 100644 endpoint/endpoint_name.go rename event_logos.go => endpoint/event_logos.go (70%) rename event_networks.go => endpoint/event_networks.go (70%) rename events.go => endpoint/events.go (71%) rename external_game_sources.go => endpoint/external_game_sources.go (69%) rename external_games.go => endpoint/external_games.go (70%) rename franchises.go => endpoint/franchises.go (70%) rename game_engine_logos.go => endpoint/game_engine_logos.go (69%) rename game_engines.go => endpoint/game_engines.go (70%) rename game_localizations.go => endpoint/game_localizations.go (69%) rename game_modes.go => endpoint/game_modes.go (70%) rename game_release_formats.go => endpoint/game_release_formats.go (69%) rename game_statuses.go => endpoint/game_statuses.go (70%) rename game_time_to_beats.go => endpoint/game_time_to_beats.go (69%) rename game_types.go => endpoint/game_types.go (70%) rename game_version_feature_values.go => endpoint/game_version_feature_values.go (68%) rename game_version_features.go => endpoint/game_version_features.go (69%) rename game_versions.go => endpoint/game_versions.go (70%) rename game_videos.go => endpoint/game_videos.go (70%) create mode 100644 endpoint/games.go rename genres.go => endpoint/genres.go (71%) rename involved_companies.go => endpoint/involved_companies.go (69%) rename keywords.go => endpoint/keywords.go (70%) rename language_support_types.go => endpoint/language_support_types.go (69%) rename language_supports.go => endpoint/language_supports.go (69%) rename languages.go => endpoint/languages.go (70%) rename multiplayer_modes.go => endpoint/multiplayer_modes.go (69%) rename network_types.go => endpoint/network_types.go (70%) rename platform_families.go => endpoint/platform_families.go (69%) rename platform_logos.go => endpoint/platform_logos.go (70%) rename platform_types.go => endpoint/platform_types.go (70%) rename platform_version_companies.go => endpoint/platform_version_companies.go (68%) rename platform_version_release_dates.go => endpoint/platform_version_release_dates.go (68%) rename platform_versions.go => endpoint/platform_versions.go (69%) rename platform_websites.go => endpoint/platform_websites.go (69%) rename platforms.go => endpoint/platforms.go (70%) rename player_perspectives.go => endpoint/player_perspectives.go (69%) rename popularity_primitives.go => endpoint/popularity_primitives.go (69%) rename popularity_types.go => endpoint/popularity_types.go (69%) rename regions.go => endpoint/regions.go (70%) rename release_date_regions.go => endpoint/release_date_regions.go (69%) rename release_date_statuses.go => endpoint/release_date_statuses.go (69%) rename release_dates.go => endpoint/release_dates.go (70%) rename screenshots.go => endpoint/screenshots.go (70%) rename search.go => endpoint/search.go (81%) rename themes.go => endpoint/themes.go (71%) rename webhooks.go => endpoint/webhooks.go (67%) rename website_types.go => endpoint/website_types.go (70%) rename websites.go => endpoint/websites.go (70%) delete mode 100644 games.go delete mode 100644 igdb.go create mode 100644 register_endpoints.go diff --git a/.gitignore b/.gitignore index 70ca8c0..bd89c95 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *test.go test/ +.vscode/ diff --git a/README.md b/README.md index e5a7a88..012d118 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ # go-igdb -A Go client library for the IGDB (Internet Game Database) API v4. This library provides a simple and efficient way to interact with IGDB's protobuf-based API. +A Go client library for the IGDB (Internet Game Database) API v4. This library provides a convenient way to interact with IGDB's protobuf-based API endpoints. ## Features -- Full support for IGDB API v4 -- Protobuf-based communication for better performance +- Full support for IGDB API v4 endpoints +- Protobuf-based communication for efficient data transfer +- Rate limiting support - Automatic token management for Twitch authentication -- Built-in retry mechanism for failed requests -- Optional Cloudflare bypass support via FlareSolverr -- All endpoints are supported +- Retry mechanism for failed requests +- Optional FlareSolverr integration for handling Cloudflare protection ## Installation @@ -20,41 +20,94 @@ go get github.com/bestnite/go-igdb ## Quick Start ```go -// Create a new IGDB client -client := igdb.New("your-client-id", "your-client-secret") +package main -// Get a game by ID -game, err := client.GetGameByID(1942) -if err != nil { - log.Fatal(err) +import ( + "log" + + "github.com/bestnite/go-igdb" + pb "github.com/bestnite/go-igdb/proto" +) + +func Test1(c *igdb.Client) { + game, err := igdb.GetItemByID[pb.Game](1942, c.Games.Query) + if err != nil { + log.Fatal(err) + } + log.Printf("Name of game %d: %s\n", 1942, game.Name) } -fmt.Printf("Game: %s\n", game.Name) -// Search games with custom query -games, err := client.GetGames("search \"Zelda\"; fields name,rating,release_dates.*;") -if err != nil { - log.Fatal(err) +func Test2(c *igdb.Client) { + games, err := igdb.GetItemsByIDs[pb.Game]([]uint64{119171, 119133}, c.Games.Query) + if err != nil { + log.Fatal(err) + } + log.Printf("Names of games %d and %d: %s and %s\n", 119171, 119133, games[0].Name, games[1].Name) +} + +func Test3(c *igdb.Client) { + total, err := igdb.GetItemsLength[pb.Game](c.Games.Query) + if err != nil { + log.Fatal(err) + } + log.Printf("Total number of games: %d\n", total) +} + +func Test4(c *igdb.Client) { + games, err := igdb.GetItemsPagniated[pb.Game](0, 10, c.Games.Query) + if err != nil { + log.Fatal(err) + } + log.Printf("Names of ids 0 to 10 games:\n") + for _, game := range games { + log.Println(game.Name) + } +} + +func Test5(c *igdb.Client) { + game, err := igdb.AssertSingle[pb.Game](c.Games.Query("fields name,rating; sort rating desc; limit 1;")) + if err != nil { + log.Fatalf("failed to get game: %s", err) + } + log.Printf("Name of first game with highest rating: %s\n", game.Name) +} + +func Test6(c *igdb.Client) { + games, err := igdb.AssertSlice[pb.Game](c.Games.Query("fields *; where rating > 70; limit 10;")) + if err != nil { + panic(err) + } + log.Printf("Names of games with rating > 70 limit 10:\n") + for _, game := range games { + log.Println(game.Name) + } +} + +func main() { + client := igdb.New("your-client-id", "your-client-secret") + Test1(client) + Test2(client) + Test3(client) + Test4(client) + Test5(client) + Test6(client) } ``` ## Advanced Usage -### Query Format - -The library uses IGDB's query syntax. For example: +### Using with FlareSolverr ```go -// Get games released in 2023 -games, err := client.GetGames("where release_dates.y = 2023; fields name,rating;") +import "github.com/bestnite/go-flaresolverr" -// Get specific fields for a company -companies, err := client.GetCompanies("where id = 1234; fields name,description,country;") +flaresolverr := flaresolverr.New("http://localhost:8191") +client := igdb.NewWithFlaresolverr("your-client-id", "your-client-secret", flaresolverr) ``` -## Requirements +### Rate Limiting -- Go 1.24 or higher -- IGDB/Twitch API credentials (Client ID and Client Secret) +The client automatically handles rate limiting with a default of 4 requests per second. This helps prevent hitting IGDB's rate limits. ## Contributing diff --git a/assert.go b/assert.go new file mode 100644 index 0000000..b6e277b --- /dev/null +++ b/assert.go @@ -0,0 +1,41 @@ +package igdb + +import "fmt" + +func AssertSingle[T any](data any, err error) (*T, error) { + if err != nil { + return nil, err + } + + if data == nil { + return nil, fmt.Errorf("data is nil") + } + + datas, ok := data.([]*T) + if !ok { + return nil, fmt.Errorf("failed to convert to []*T") + } + + if len(datas) == 0 { + return nil, fmt.Errorf("no results") + } + + return datas[0], nil +} + +func AssertSlice[T any](data any, err error) ([]*T, error) { + if err != nil { + return nil, err + } + + if data == nil { + return nil, fmt.Errorf("data is nil") + } + + datas, ok := data.([]*T) + if !ok { + return nil, fmt.Errorf("failed to convert to []*T") + } + + return datas, nil +} diff --git a/client.go b/client.go new file mode 100644 index 0000000..aa5ca0a --- /dev/null +++ b/client.go @@ -0,0 +1,197 @@ +package igdb + +import ( + "fmt" + "strings" + + "github.com/bestnite/go-flaresolverr" + "github.com/bestnite/go-igdb/endpoint" + + "github.com/go-resty/resty/v2" +) + +type Client struct { + clientID string + token *twitchToken + flaresolverr *flaresolverr.Flaresolverr + limiter *rateLimiter + + EntityEndpoints map[endpoint.EndpointName]endpoint.EntityEndpoint + + AgeRatingCategories *endpoint.AgeRatingCategories + AgeRatingContentDescriptions *endpoint.AgeRatingContentDescriptions + AgeRatingContentDescriptionsV2 *endpoint.AgeRatingContentDescriptionsV2 + AgeRatingOrganizations *endpoint.AgeRatingOrganizations + AgeRatings *endpoint.AgeRatings + AlternativeNames *endpoint.AlternativeNames + Artworks *endpoint.Artworks + CharacterGenders *endpoint.CharacterGenders + CharacterMugShots *endpoint.CharacterMugShots + Characters *endpoint.Characters + CharacterSpecies *endpoint.CharacterSpecies + CollectionMemberships *endpoint.CollectionMemberships + CollectionMembershipTypes *endpoint.CollectionMembershipTypes + CollectionRelations *endpoint.CollectionRelations + CollectionRelationTypes *endpoint.CollectionRelationTypes + Collections *endpoint.Collections + CollectionTypes *endpoint.CollectionTypes + Companies *endpoint.Companies + CompanyLogos *endpoint.CompanyLogos + CompanyStatuses *endpoint.CompanyStatuses + CompanyWebsites *endpoint.CompanyWebsites + Covers *endpoint.Covers + DateFormats *endpoint.DateFormats + EventLogos *endpoint.EventLogos + EventNetworks *endpoint.EventNetworks + Events *endpoint.Events + ExternalGames *endpoint.ExternalGames + ExternalGameSources *endpoint.ExternalGameSources + Franchises *endpoint.Franchises + GameEngineLogos *endpoint.GameEngineLogos + GameEngines *endpoint.GameEngines + GameLocalizations *endpoint.GameLocalizations + GameModes *endpoint.GameModes + GameReleaseFormats *endpoint.GameReleaseFormats + Games *endpoint.Games + GameStatuses *endpoint.GameStatuses + GameTimeToBeats *endpoint.GameTimeToBeats + GameTypes *endpoint.GameTypes + GameVersionFeatures *endpoint.GameVersionFeatures + GameVersionFeatureValues *endpoint.GameVersionFeatureValues + GameVersions *endpoint.GameVersions + GameVideos *endpoint.GameVideos + Genres *endpoint.Genres + InvolvedCompanies *endpoint.InvolvedCompanies + Keywords *endpoint.Keywords + Languages *endpoint.Languages + LanguageSupports *endpoint.LanguageSupports + LanguageSupportTypes *endpoint.LanguageSupportTypes + MultiplayerModes *endpoint.MultiplayerModes + NetworkTypes *endpoint.NetworkTypes + PlatformFamilies *endpoint.PlatformFamilies + PlatformLogos *endpoint.PlatformLogos + Platforms *endpoint.Platforms + PlatformTypes *endpoint.PlatformTypes + PlatformVersionCompanies *endpoint.PlatformVersionCompanies + PlatformVersionReleaseDates *endpoint.PlatformVersionReleaseDates + PlatformVersions *endpoint.PlatformVersions + PlatformWebsites *endpoint.PlatformWebsites + PlayerPerspectives *endpoint.PlayerPerspectives + PopularityPrimitives *endpoint.PopularityPrimitives + PopularityTypes *endpoint.PopularityTypes + Regions *endpoint.Regions + ReleaseDateRegions *endpoint.ReleaseDateRegions + ReleaseDates *endpoint.ReleaseDates + ReleaseDateStatuses *endpoint.ReleaseDateStatuses + Screenshots *endpoint.Screenshots + Search *endpoint.Search + Themes *endpoint.Themes + Webhooks *endpoint.Webhooks + Websites *endpoint.Websites + WebsiteTypes *endpoint.WebsiteTypes +} + +func New(clientID, clientSecret string) *Client { + c := &Client{ + clientID: clientID, + limiter: newRateLimiter(4), + token: NewTwitchToken(clientID, clientSecret), + flaresolverr: nil, + EntityEndpoints: make(map[endpoint.EndpointName]endpoint.EntityEndpoint), + } + + registerAllEndpoints(c) + + return c +} + +func NewWithFlaresolverr(clientID, clientSecret string, f *flaresolverr.Flaresolverr) *Client { + c := New(clientID, clientSecret) + c.flaresolverr = f + return c +} + +func (g *Client) Request(URL string, dataBody any) (*resty.Response, error) { + g.limiter.wait() + + t, err := g.token.getToken() + if err != nil { + return nil, fmt.Errorf("failed to get twitch token: %w", err) + } + + resp, err := request().SetBody(dataBody).SetHeaders(map[string]string{ + "Client-ID": g.clientID, + "Authorization": "Bearer " + t, + "User-Agent": "", + "Content-Type": "text/plain", + }).Post(URL) + + if err != nil { + return nil, fmt.Errorf("failed to request: %s: %w", URL, err) + } + return resp, nil +} + +func GetItemsPagniated[T any](offset, limit int, f func(string) ([]*T, error)) ([]*T, error) { + query := fmt.Sprintf("offset %d; limit %d; f *; sort id asc;", offset, limit) + items, err := f(query) + if err != nil { + return nil, err + } + + return items, nil +} + +func GetItemsLength[T any](f func(string) ([]*T, error)) (uint64, error) { + query := "fields id; sort id desc; limit 1;" + items, err := f(query) + if err != nil { + return 0, err + } + + if len(items) == 0 { + return 0, fmt.Errorf("no results: %s", query) + } + + type Iid interface { + GetId() uint64 + } + + item, ok := any(items[0]).(Iid) + + if !ok { + return 0, fmt.Errorf("failed to convert") + } + + return item.GetId(), nil +} + +func GetItemByID[T any](id uint64, f func(string) ([]*T, error)) (*T, error) { + query := fmt.Sprintf("where id = %d; fields *;", id) + items, err := f(query) + if err != nil { + return nil, err + } + + if len(items) == 0 { + return nil, fmt.Errorf("no results: %s", query) + } + + return items[0], nil +} + +func GetItemsByIDs[T any](ids []uint64, f func(string) ([]*T, error)) ([]*T, error) { + idStrSlice := make([]string, len(ids)) + for i, id := range ids { + idStrSlice[i] = fmt.Sprintf("%d", id) + } + + idStr := fmt.Sprintf(`where id = (%s); fields *;`, strings.Join(idStrSlice, ",")) + + items, err := f(idStr) + if err != nil { + return nil, err + } + + return items, nil +} diff --git a/age_rating_categories.go b/endpoint/age_rating_categories.go similarity index 69% rename from age_rating_categories.go rename to endpoint/age_rating_categories.go index 7f370ef..106a375 100644 --- a/age_rating_categories.go +++ b/endpoint/age_rating_categories.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,12 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetAgeRatingCategories(query string) ([]*pb.AgeRatingCategory, error) { - resp, err := g.Request("https://api.igdb.com/v4/age_rating_categories.pb", query) +type AgeRatingCategories struct { + BaseEndpoint +} + +func (a *AgeRatingCategories) Query(query string) ([]*pb.AgeRatingCategory, error) { + resp, err := a.request("https://api.igdb.com/v4/age_rating_categories.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/age_rating_content_descriptions.go b/endpoint/age_rating_content_descriptions.go similarity index 68% rename from age_rating_content_descriptions.go rename to endpoint/age_rating_content_descriptions.go index 646102a..8db43b4 100644 --- a/age_rating_content_descriptions.go +++ b/endpoint/age_rating_content_descriptions.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,12 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetAgeRatingContentDescriptions(query string) ([]*pb.AgeRatingContentDescription, error) { - resp, err := g.Request("https://api.igdb.com/v4/age_rating_content_descriptions.pb", query) +type AgeRatingContentDescriptions struct { + BaseEndpoint +} + +func (a *AgeRatingContentDescriptions) Query(query string) ([]*pb.AgeRatingContentDescription, error) { + resp, err := a.request("https://api.igdb.com/v4/age_rating_content_descriptions.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/age_rating_content_descriptions_v2.go b/endpoint/age_rating_content_descriptions_v2.go similarity index 68% rename from age_rating_content_descriptions_v2.go rename to endpoint/age_rating_content_descriptions_v2.go index 92907cd..033e5dc 100644 --- a/age_rating_content_descriptions_v2.go +++ b/endpoint/age_rating_content_descriptions_v2.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,12 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetAgeRatingContentDescriptionsV2(query string) ([]*pb.AgeRatingContentDescriptionV2, error) { - resp, err := g.Request("https://api.igdb.com/v4/age_rating_content_descriptions_v2.pb", query) +type AgeRatingContentDescriptionsV2 struct { + BaseEndpoint +} + +func (a *AgeRatingContentDescriptionsV2) Query(query string) ([]*pb.AgeRatingContentDescriptionV2, error) { + resp, err := a.request("https://api.igdb.com/v4/age_rating_content_descriptions_v2.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/age_rating_organizations.go b/endpoint/age_rating_organizations.go similarity index 68% rename from age_rating_organizations.go rename to endpoint/age_rating_organizations.go index 177d4d0..7a72f83 100644 --- a/age_rating_organizations.go +++ b/endpoint/age_rating_organizations.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetAgeRatingOrganizations(query string) ([]*pb.AgeRatingOrganization, error) { - resp, err := g.Request("https://api.igdb.com/v4/age_rating_organizations.pb", query) +type AgeRatingOrganizations struct{ BaseEndpoint } + +func (a *AgeRatingOrganizations) Query(query string) ([]*pb.AgeRatingOrganization, error) { + resp, err := a.request("https://api.igdb.com/v4/age_rating_organizations.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/age_ratings.go b/endpoint/age_ratings.go similarity index 70% rename from age_ratings.go rename to endpoint/age_ratings.go index f5c619c..f840085 100644 --- a/age_ratings.go +++ b/endpoint/age_ratings.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetAgeRatings(query string) ([]*pb.AgeRating, error) { - resp, err := g.Request("https://api.igdb.com/v4/age_ratings.pb", query) +type AgeRatings struct{ BaseEndpoint } + +func (a *AgeRatings) Query(query string) ([]*pb.AgeRating, error) { + resp, err := a.request("https://api.igdb.com/v4/age_ratings.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/alternative_names.go b/endpoint/alternative_names.go similarity index 69% rename from alternative_names.go rename to endpoint/alternative_names.go index 6f24bd7..c005110 100644 --- a/alternative_names.go +++ b/endpoint/alternative_names.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetAlternativeNames(query string) ([]*pb.AlternativeName, error) { - resp, err := g.Request("https://api.igdb.com/v4/alternative_names.pb", query) +type AlternativeNames struct{ BaseEndpoint } + +func (a *AlternativeNames) Query(query string) ([]*pb.AlternativeName, error) { + resp, err := a.request("https://api.igdb.com/v4/alternative_names.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/artworks.go b/endpoint/artworks.go similarity index 70% rename from artworks.go rename to endpoint/artworks.go index d0c8f31..23361c8 100644 --- a/artworks.go +++ b/endpoint/artworks.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetArtworks(query string) ([]*pb.Artwork, error) { - resp, err := g.Request("https://api.igdb.com/v4/artworks.pb", query) +type Artworks struct{ BaseEndpoint } + +func (a *Artworks) Query(query string) ([]*pb.Artwork, error) { + resp, err := a.request("https://api.igdb.com/v4/artworks.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/endpoint/base.go b/endpoint/base.go new file mode 100644 index 0000000..86bf58e --- /dev/null +++ b/endpoint/base.go @@ -0,0 +1,38 @@ +package endpoint + +import ( + "github.com/go-resty/resty/v2" +) + +type BaseEndpoint struct { + request func(URL string, dataBody any) (*resty.Response, error) + endpointName EndpointName +} + +func NewBaseEndpoint(request func(URL string, dataBody any) (*resty.Response, error), endpointName EndpointName) *BaseEndpoint { + return &BaseEndpoint{ + request: request, + endpointName: endpointName, + } +} + +func (b *BaseEndpoint) GetEndpointName() EndpointName { + return b.endpointName +} + +func (b *BaseEndpoint) Query(query string) (any, error) { + return nil, nil +} + +func (b *BaseEndpoint) QueryAny(query string) (any, error) { + return b.Query(query) +} + +type Endpoint interface { + GetEndpointName() EndpointName +} + +type EntityEndpoint interface { + QueryAny(query string) (any, error) + GetEndpointName() EndpointName +} diff --git a/character_genders.go b/endpoint/character_genders.go similarity index 69% rename from character_genders.go rename to endpoint/character_genders.go index 9e5e1bc..215f2b7 100644 --- a/character_genders.go +++ b/endpoint/character_genders.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCharacterGenders(query string) ([]*pb.CharacterGender, error) { - resp, err := g.Request("https://api.igdb.com/v4/character_genders.pb", query) +type CharacterGenders struct{ BaseEndpoint } + +func (a *CharacterGenders) Query(query string) ([]*pb.CharacterGender, error) { + resp, err := a.request("https://api.igdb.com/v4/character_genders.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/character_mug_shots.go b/endpoint/character_mug_shots.go similarity index 69% rename from character_mug_shots.go rename to endpoint/character_mug_shots.go index ee50bda..fa5632d 100644 --- a/character_mug_shots.go +++ b/endpoint/character_mug_shots.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCharacterMugShots(query string) ([]*pb.CharacterMugShot, error) { - resp, err := g.Request("https://api.igdb.com/v4/character_mug_shots.pb", query) +type CharacterMugShots struct{ BaseEndpoint } + +func (a *CharacterMugShots) Query(query string) ([]*pb.CharacterMugShot, error) { + resp, err := a.request("https://api.igdb.com/v4/character_mug_shots.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/character_species.go b/endpoint/character_species.go similarity index 69% rename from character_species.go rename to endpoint/character_species.go index c2137bd..c1a9ca5 100644 --- a/character_species.go +++ b/endpoint/character_species.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCharacterSpecies(query string) ([]*pb.CharacterSpecie, error) { - resp, err := g.Request("https://api.igdb.com/v4/character_species.pb", query) +type CharacterSpecies struct{ BaseEndpoint } + +func (a *CharacterSpecies) Query(query string) ([]*pb.CharacterSpecie, error) { + resp, err := a.request("https://api.igdb.com/v4/character_species.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/characters.go b/endpoint/characters.go similarity index 70% rename from characters.go rename to endpoint/characters.go index 6c160dc..7fca8bf 100644 --- a/characters.go +++ b/endpoint/characters.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCharacters(query string) ([]*pb.Character, error) { - resp, err := g.Request("https://api.igdb.com/v4/characters.pb", query) +type Characters struct{ BaseEndpoint } + +func (a *Characters) Query(query string) ([]*pb.Character, error) { + resp, err := a.request("https://api.igdb.com/v4/characters.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/collection_membership_types.go b/endpoint/collection_membership_types.go similarity index 68% rename from collection_membership_types.go rename to endpoint/collection_membership_types.go index 0b121ed..aba42b5 100644 --- a/collection_membership_types.go +++ b/endpoint/collection_membership_types.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCollectionMembershipTypes(query string) ([]*pb.CollectionMembershipType, error) { - resp, err := g.Request("https://api.igdb.com/v4/collection_membership_types.pb", query) +type CollectionMembershipTypes struct{ BaseEndpoint } + +func (a *CollectionMembershipTypes) Query(query string) ([]*pb.CollectionMembershipType, error) { + resp, err := a.request("https://api.igdb.com/v4/collection_membership_types.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/collection_memberships.go b/endpoint/collection_memberships.go similarity index 69% rename from collection_memberships.go rename to endpoint/collection_memberships.go index 565d2f4..07ab197 100644 --- a/collection_memberships.go +++ b/endpoint/collection_memberships.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCollectionMemberships(query string) ([]*pb.CollectionMembership, error) { - resp, err := g.Request("https://api.igdb.com/v4/collection_memberships.pb", query) +type CollectionMemberships struct{ BaseEndpoint } + +func (a *CollectionMemberships) Query(query string) ([]*pb.CollectionMembership, error) { + resp, err := a.request("https://api.igdb.com/v4/collection_memberships.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/collection_relation_types.go b/endpoint/collection_relation_types.go similarity index 68% rename from collection_relation_types.go rename to endpoint/collection_relation_types.go index 89b35a2..18239a4 100644 --- a/collection_relation_types.go +++ b/endpoint/collection_relation_types.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCollectionRelationTypes(query string) ([]*pb.CollectionRelationType, error) { - resp, err := g.Request("https://api.igdb.com/v4/collection_relation_types.pb", query) +type CollectionRelationTypes struct{ BaseEndpoint } + +func (a *CollectionRelationTypes) Query(query string) ([]*pb.CollectionRelationType, error) { + resp, err := a.request("https://api.igdb.com/v4/collection_relation_types.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/collection_relations.go b/endpoint/collection_relations.go similarity index 69% rename from collection_relations.go rename to endpoint/collection_relations.go index 3357412..44df7c6 100644 --- a/collection_relations.go +++ b/endpoint/collection_relations.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCollectionRelations(query string) ([]*pb.CollectionRelation, error) { - resp, err := g.Request("https://api.igdb.com/v4/collection_relations.pb", query) +type CollectionRelations struct{ BaseEndpoint } + +func (a *CollectionRelations) Query(query string) ([]*pb.CollectionRelation, error) { + resp, err := a.request("https://api.igdb.com/v4/collection_relations.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/collection_types.go b/endpoint/collection_types.go similarity index 69% rename from collection_types.go rename to endpoint/collection_types.go index 0508388..c2f42a7 100644 --- a/collection_types.go +++ b/endpoint/collection_types.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCollectionTypes(query string) ([]*pb.CollectionType, error) { - resp, err := g.Request("https://api.igdb.com/v4/collection_types.pb", query) +type CollectionTypes struct{ BaseEndpoint } + +func (a *CollectionTypes) Query(query string) ([]*pb.CollectionType, error) { + resp, err := a.request("https://api.igdb.com/v4/collection_types.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/collections.go b/endpoint/collections.go similarity index 70% rename from collections.go rename to endpoint/collections.go index 8853acb..274b4da 100644 --- a/collections.go +++ b/endpoint/collections.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCollections(query string) ([]*pb.Collection, error) { - resp, err := g.Request("https://api.igdb.com/v4/collections.pb", query) +type Collections struct{ BaseEndpoint } + +func (a *Collections) Query(query string) ([]*pb.Collection, error) { + resp, err := a.request("https://api.igdb.com/v4/collections.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/companies.go b/endpoint/companies.go similarity index 70% rename from companies.go rename to endpoint/companies.go index 921b24e..94b0304 100644 --- a/companies.go +++ b/endpoint/companies.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "errors" @@ -9,8 +9,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCompanies(query string) ([]*pb.Company, error) { - resp, err := g.Request("https://api.igdb.com/v4/companies.pb", query) +type Companies struct{ BaseEndpoint } + +func (a *Companies) Query(query string) ([]*pb.Company, error) { + resp, err := a.request("https://api.igdb.com/v4/companies.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/company_logos.go b/endpoint/company_logos.go similarity index 70% rename from company_logos.go rename to endpoint/company_logos.go index e3b7bd4..28b472d 100644 --- a/company_logos.go +++ b/endpoint/company_logos.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCompanyLogos(query string) ([]*pb.CompanyLogo, error) { - resp, err := g.Request("https://api.igdb.com/v4/company_logos.pb", query) +type CompanyLogos struct{ BaseEndpoint } + +func (a *CompanyLogos) Query(query string) ([]*pb.CompanyLogo, error) { + resp, err := a.request("https://api.igdb.com/v4/company_logos.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/company_statuses.go b/endpoint/company_statuses.go similarity index 69% rename from company_statuses.go rename to endpoint/company_statuses.go index 49e1aa2..b0bddc7 100644 --- a/company_statuses.go +++ b/endpoint/company_statuses.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCompanyStatuses(query string) ([]*pb.CompanyStatus, error) { - resp, err := g.Request("https://api.igdb.com/v4/company_statuses.pb", query) +type CompanyStatuses struct{ BaseEndpoint } + +func (a *CompanyStatuses) Query(query string) ([]*pb.CompanyStatus, error) { + resp, err := a.request("https://api.igdb.com/v4/company_statuses.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/company_websites.go b/endpoint/company_websites.go similarity index 69% rename from company_websites.go rename to endpoint/company_websites.go index 88cea23..b6ef1f0 100644 --- a/company_websites.go +++ b/endpoint/company_websites.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCompanyWebsites(query string) ([]*pb.CompanyWebsite, error) { - resp, err := g.Request("https://api.igdb.com/v4/company_websites.pb", query) +type CompanyWebsites struct{ BaseEndpoint } + +func (a *CompanyWebsites) Query(query string) ([]*pb.CompanyWebsite, error) { + resp, err := a.request("https://api.igdb.com/v4/company_websites.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/covers.go b/endpoint/covers.go similarity index 71% rename from covers.go rename to endpoint/covers.go index 1cd1c64..e7b09a8 100644 --- a/covers.go +++ b/endpoint/covers.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetCovers(query string) ([]*pb.Cover, error) { - resp, err := g.Request("https://api.igdb.com/v4/covers.pb", query) +type Covers struct{ BaseEndpoint } + +func (a *Covers) Query(query string) ([]*pb.Cover, error) { + resp, err := a.request("https://api.igdb.com/v4/covers.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/date_formats.go b/endpoint/date_formats.go similarity index 70% rename from date_formats.go rename to endpoint/date_formats.go index 21b9354..4ebcc3c 100644 --- a/date_formats.go +++ b/endpoint/date_formats.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetDateFormats(query string) ([]*pb.DateFormat, error) { - resp, err := g.Request("https://api.igdb.com/v4/date_formats.pb", query) +type DateFormats struct{ BaseEndpoint } + +func (a *DateFormats) Query(query string) ([]*pb.DateFormat, error) { + resp, err := a.request("https://api.igdb.com/v4/date_formats.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/endpoint/endpoint.go b/endpoint/endpoint.go deleted file mode 100644 index 5c11e2d..0000000 --- a/endpoint/endpoint.go +++ /dev/null @@ -1,151 +0,0 @@ -package endpoint - -type Endpoint string - -var ( - AgeRatingCategories Endpoint = "age_rating_categories" - AgeRatingContentDescriptions Endpoint = "age_rating_content_descriptions" - AgeRatingContentDescriptionsV2 Endpoint = "age_rating_content_descriptions_v2" - AgeRatingOrganizations Endpoint = "age_rating_organizations" - AgeRatings Endpoint = "age_ratings" - AlternativeNames Endpoint = "alternative_names" - Artworks Endpoint = "artworks" - CharacterGenders Endpoint = "character_genders" - CharacterMugShots Endpoint = "character_mug_shots" - Characters Endpoint = "characters" - CharacterSpecies Endpoint = "character_species" - CollectionMemberships Endpoint = "collection_memberships" - CollectionMembershipTypes Endpoint = "collection_membership_types" - CollectionRelations Endpoint = "collection_relations" - CollectionRelationTypes Endpoint = "collection_relation_types" - Collections Endpoint = "collections" - CollectionTypes Endpoint = "collection_types" - Companies Endpoint = "companies" - CompanyLogos Endpoint = "company_logos" - CompanyStatuses Endpoint = "company_statuses" - CompanyWebsites Endpoint = "company_websites" - Covers Endpoint = "covers" - DateFormats Endpoint = "date_formats" - EventLogos Endpoint = "event_logos" - EventNetworks Endpoint = "event_networks" - Events Endpoint = "events" - ExternalGames Endpoint = "external_games" - ExternalGameSources Endpoint = "external_game_sources" - Franchises Endpoint = "franchises" - GameEngineLogos Endpoint = "game_engine_logos" - GameEngines Endpoint = "game_engines" - GameLocalizations Endpoint = "game_localizations" - GameModes Endpoint = "game_modes" - GameReleaseFormats Endpoint = "game_release_formats" - Games Endpoint = "games" - GameStatuses Endpoint = "game_statuses" - GameTimeToBeats Endpoint = "game_time_to_beats" - GameTypes Endpoint = "game_types" - GameVersionFeatures Endpoint = "game_version_features" - GameVersionFeatureValues Endpoint = "game_version_feature_values" - GameVersions Endpoint = "game_versions" - GameVideos Endpoint = "game_videos" - Genres Endpoint = "genres" - InvolvedCompanies Endpoint = "involved_companies" - Keywords Endpoint = "keywords" - Languages Endpoint = "languages" - LanguageSupports Endpoint = "language_supports" - LanguageSupportTypes Endpoint = "language_support_types" - MultiplayerModes Endpoint = "multiplayer_modes" - NetworkTypes Endpoint = "network_types" - PlatformFamilies Endpoint = "platform_families" - PlatformLogos Endpoint = "platform_logos" - Platforms Endpoint = "platforms" - PlatformTypes Endpoint = "platform_types" - PlatformVersionCompanies Endpoint = "platform_version_companies" - PlatformVersionReleaseDates Endpoint = "platform_version_release_dates" - PlatformVersions Endpoint = "platform_versions" - PlatformWebsites Endpoint = "platform_websites" - PlayerPerspectives Endpoint = "player_perspectives" - PopularityPrimitives Endpoint = "popularity_primitives" - PopularityTypes Endpoint = "popularity_types" - Regions Endpoint = "regions" - ReleaseDateRegions Endpoint = "release_date_regions" - ReleaseDates Endpoint = "release_dates" - ReleaseDateStatuses Endpoint = "release_date_statuses" - Screenshots Endpoint = "screenshots" - Search Endpoint = "search" - Themes Endpoint = "themes" - Webhooks Endpoint = "webhooks" - Websites Endpoint = "websites" - WebsiteTypes Endpoint = "website_types" -) - -var AllEndpoints = []Endpoint{ - AgeRatingCategories, - AgeRatingContentDescriptions, - AgeRatingContentDescriptionsV2, - AgeRatingOrganizations, - AgeRatings, - AlternativeNames, - Artworks, - CharacterGenders, - CharacterMugShots, - Characters, - CharacterSpecies, - CollectionMemberships, - CollectionMembershipTypes, - CollectionRelations, - CollectionRelationTypes, - Collections, - CollectionTypes, - Companies, - CompanyLogos, - CompanyStatuses, - CompanyWebsites, - Covers, - DateFormats, - EventLogos, - EventNetworks, - Events, - ExternalGames, - ExternalGameSources, - Franchises, - GameEngineLogos, - GameEngines, - GameLocalizations, - GameModes, - GameReleaseFormats, - Games, - GameStatuses, - GameTimeToBeats, - GameTypes, - GameVersionFeatures, - GameVersionFeatureValues, - GameVersions, - GameVideos, - Genres, - InvolvedCompanies, - Keywords, - Languages, - LanguageSupports, - LanguageSupportTypes, - MultiplayerModes, - NetworkTypes, - PlatformFamilies, - PlatformLogos, - Platforms, - PlatformTypes, - PlatformVersionCompanies, - PlatformVersionReleaseDates, - PlatformVersions, - PlatformWebsites, - PlayerPerspectives, - PopularityPrimitives, - PopularityTypes, - Regions, - ReleaseDateRegions, - ReleaseDates, - ReleaseDateStatuses, - Screenshots, - Search, - Themes, - Webhooks, - Websites, - WebsiteTypes, -} diff --git a/endpoint/endpoint_name.go b/endpoint/endpoint_name.go new file mode 100644 index 0000000..91df4e3 --- /dev/null +++ b/endpoint/endpoint_name.go @@ -0,0 +1,151 @@ +package endpoint + +type EndpointName string + +var ( + EPAgeRatingCategories EndpointName = "age_rating_categories" + EPAgeRatingContentDescriptions EndpointName = "age_rating_content_descriptions" + EPAgeRatingContentDescriptionsV2 EndpointName = "age_rating_content_descriptions_v2" + EPAgeRatingOrganizations EndpointName = "age_rating_organizations" + EPAgeRatings EndpointName = "age_ratings" + EPAlternativeNames EndpointName = "alternative_names" + EPArtworks EndpointName = "artworks" + EPCharacterGenders EndpointName = "character_genders" + EPCharacterMugShots EndpointName = "character_mug_shots" + EPCharacters EndpointName = "characters" + EPCharacterSpecies EndpointName = "character_species" + EPCollectionMemberships EndpointName = "collection_memberships" + EPCollectionMembershipTypes EndpointName = "collection_membership_types" + EPCollectionRelations EndpointName = "collection_relations" + EPCollectionRelationTypes EndpointName = "collection_relation_types" + EPCollections EndpointName = "collections" + EPCollectionTypes EndpointName = "collection_types" + EPCompanies EndpointName = "companies" + EPCompanyLogos EndpointName = "company_logos" + EPCompanyStatuses EndpointName = "company_statuses" + EPCompanyWebsites EndpointName = "company_websites" + EPCovers EndpointName = "covers" + EPDateFormats EndpointName = "date_formats" + EPEventLogos EndpointName = "event_logos" + EPEventNetworks EndpointName = "event_networks" + EPEvents EndpointName = "events" + EPExternalGames EndpointName = "external_games" + EPExternalGameSources EndpointName = "external_game_sources" + EPFranchises EndpointName = "franchises" + EPGameEngineLogos EndpointName = "game_engine_logos" + EPGameEngines EndpointName = "game_engines" + EPGameLocalizations EndpointName = "game_localizations" + EPGameModes EndpointName = "game_modes" + EPGameReleaseFormats EndpointName = "game_release_formats" + EPGames EndpointName = "games" + EPGameStatuses EndpointName = "game_statuses" + EPGameTimeToBeats EndpointName = "game_time_to_beats" + EPGameTypes EndpointName = "game_types" + EPGameVersionFeatures EndpointName = "game_version_features" + EPGameVersionFeatureValues EndpointName = "game_version_feature_values" + EPGameVersions EndpointName = "game_versions" + EPGameVideos EndpointName = "game_videos" + EPGenres EndpointName = "genres" + EPInvolvedCompanies EndpointName = "involved_companies" + EPKeywords EndpointName = "keywords" + EPLanguages EndpointName = "languages" + EPLanguageSupports EndpointName = "language_supports" + EPLanguageSupportTypes EndpointName = "language_support_types" + EPMultiplayerModes EndpointName = "multiplayer_modes" + EPNetworkTypes EndpointName = "network_types" + EPPlatformFamilies EndpointName = "platform_families" + EPPlatformLogos EndpointName = "platform_logos" + EPPlatforms EndpointName = "platforms" + EPPlatformTypes EndpointName = "platform_types" + EPPlatformVersionCompanies EndpointName = "platform_version_companies" + EPPlatformVersionReleaseDates EndpointName = "platform_version_release_dates" + EPPlatformVersions EndpointName = "platform_versions" + EPPlatformWebsites EndpointName = "platform_websites" + EPPlayerPerspectives EndpointName = "player_perspectives" + EPPopularityPrimitives EndpointName = "popularity_primitives" + EPPopularityTypes EndpointName = "popularity_types" + EPRegions EndpointName = "regions" + EPReleaseDateRegions EndpointName = "release_date_regions" + EPReleaseDates EndpointName = "release_dates" + EPReleaseDateStatuses EndpointName = "release_date_statuses" + EPScreenshots EndpointName = "screenshots" + EPSearch EndpointName = "search" + EPThemes EndpointName = "themes" + EPWebhooks EndpointName = "webhooks" + EPWebsites EndpointName = "websites" + EPWebsiteTypes EndpointName = "website_types" +) + +var AllEndpoints = []EndpointName{ + EPAgeRatingCategories, + EPAgeRatingContentDescriptions, + EPAgeRatingContentDescriptionsV2, + EPAgeRatingOrganizations, + EPAgeRatings, + EPAlternativeNames, + EPArtworks, + EPCharacterGenders, + EPCharacterMugShots, + EPCharacters, + EPCharacterSpecies, + EPCollectionMemberships, + EPCollectionMembershipTypes, + EPCollectionRelations, + EPCollectionRelationTypes, + EPCollections, + EPCollectionTypes, + EPCompanies, + EPCompanyLogos, + EPCompanyStatuses, + EPCompanyWebsites, + EPCovers, + EPDateFormats, + EPEventLogos, + EPEventNetworks, + EPEvents, + EPExternalGames, + EPExternalGameSources, + EPFranchises, + EPGameEngineLogos, + EPGameEngines, + EPGameLocalizations, + EPGameModes, + EPGameReleaseFormats, + EPGames, + EPGameStatuses, + EPGameTimeToBeats, + EPGameTypes, + EPGameVersionFeatures, + EPGameVersionFeatureValues, + EPGameVersions, + EPGameVideos, + EPGenres, + EPInvolvedCompanies, + EPKeywords, + EPLanguages, + EPLanguageSupports, + EPLanguageSupportTypes, + EPMultiplayerModes, + EPNetworkTypes, + EPPlatformFamilies, + EPPlatformLogos, + EPPlatforms, + EPPlatformTypes, + EPPlatformVersionCompanies, + EPPlatformVersionReleaseDates, + EPPlatformVersions, + EPPlatformWebsites, + EPPlayerPerspectives, + EPPopularityPrimitives, + EPPopularityTypes, + EPRegions, + EPReleaseDateRegions, + EPReleaseDates, + EPReleaseDateStatuses, + EPScreenshots, + EPSearch, + EPThemes, + EPWebhooks, + EPWebsites, + EPWebsiteTypes, +} diff --git a/event_logos.go b/endpoint/event_logos.go similarity index 70% rename from event_logos.go rename to endpoint/event_logos.go index 27ba1cd..0bc7c74 100644 --- a/event_logos.go +++ b/endpoint/event_logos.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetEventLogos(query string) ([]*pb.EventLogo, error) { - resp, err := g.Request("https://api.igdb.com/v4/event_logos.pb", query) +type EventLogos struct{ BaseEndpoint } + +func (a *EventLogos) Query(query string) ([]*pb.EventLogo, error) { + resp, err := a.request("https://api.igdb.com/v4/event_logos.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/event_networks.go b/endpoint/event_networks.go similarity index 70% rename from event_networks.go rename to endpoint/event_networks.go index cc6d040..0561087 100644 --- a/event_networks.go +++ b/endpoint/event_networks.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetEventNetworks(query string) ([]*pb.EventNetwork, error) { - resp, err := g.Request("https://api.igdb.com/v4/event_networks.pb", query) +type EventNetworks struct{ BaseEndpoint } + +func (a *EventNetworks) Query(query string) ([]*pb.EventNetwork, error) { + resp, err := a.request("https://api.igdb.com/v4/event_networks.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/events.go b/endpoint/events.go similarity index 71% rename from events.go rename to endpoint/events.go index 584edf2..28b6a37 100644 --- a/events.go +++ b/endpoint/events.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetEvents(query string) ([]*pb.Event, error) { - resp, err := g.Request("https://api.igdb.com/v4/events.pb", query) +type Events struct{ BaseEndpoint } + +func (a *Events) Query(query string) ([]*pb.Event, error) { + resp, err := a.request("https://api.igdb.com/v4/events.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/external_game_sources.go b/endpoint/external_game_sources.go similarity index 69% rename from external_game_sources.go rename to endpoint/external_game_sources.go index 6a8819f..9999854 100644 --- a/external_game_sources.go +++ b/endpoint/external_game_sources.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetExternalGameSources(query string) ([]*pb.ExternalGameSource, error) { - resp, err := g.Request("https://api.igdb.com/v4/external_game_sources.pb", query) +type ExternalGameSources struct{ BaseEndpoint } + +func (a *ExternalGameSources) Query(query string) ([]*pb.ExternalGameSource, error) { + resp, err := a.request("https://api.igdb.com/v4/external_game_sources.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/external_games.go b/endpoint/external_games.go similarity index 70% rename from external_games.go rename to endpoint/external_games.go index d2810ee..4b57f1e 100644 --- a/external_games.go +++ b/endpoint/external_games.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetExternalGames(query string) ([]*pb.ExternalGame, error) { - resp, err := g.Request("https://api.igdb.com/v4/external_games.pb", query) +type ExternalGames struct{ BaseEndpoint } + +func (a *ExternalGames) Query(query string) ([]*pb.ExternalGame, error) { + resp, err := a.request("https://api.igdb.com/v4/external_games.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/franchises.go b/endpoint/franchises.go similarity index 70% rename from franchises.go rename to endpoint/franchises.go index b44bbca..7b0db84 100644 --- a/franchises.go +++ b/endpoint/franchises.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetFranchises(query string) ([]*pb.Franchise, error) { - resp, err := g.Request("https://api.igdb.com/v4/franchises.pb", query) +type Franchises struct{ BaseEndpoint } + +func (a *Franchises) Query(query string) ([]*pb.Franchise, error) { + resp, err := a.request("https://api.igdb.com/v4/franchises.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_engine_logos.go b/endpoint/game_engine_logos.go similarity index 69% rename from game_engine_logos.go rename to endpoint/game_engine_logos.go index 409f9ef..d11a4b7 100644 --- a/game_engine_logos.go +++ b/endpoint/game_engine_logos.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameEngineLogos(query string) ([]*pb.GameEngineLogo, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_engine_logos.pb", query) +type GameEngineLogos struct{ BaseEndpoint } + +func (a *GameEngineLogos) Query(query string) ([]*pb.GameEngineLogo, error) { + resp, err := a.request("https://api.igdb.com/v4/game_engine_logos.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_engines.go b/endpoint/game_engines.go similarity index 70% rename from game_engines.go rename to endpoint/game_engines.go index e9232cc..109e417 100644 --- a/game_engines.go +++ b/endpoint/game_engines.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameEngines(query string) ([]*pb.GameEngine, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_engines.pb", query) +type GameEngines struct{ BaseEndpoint } + +func (a *GameEngines) Query(query string) ([]*pb.GameEngine, error) { + resp, err := a.request("https://api.igdb.com/v4/game_engines.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_localizations.go b/endpoint/game_localizations.go similarity index 69% rename from game_localizations.go rename to endpoint/game_localizations.go index 58121f1..3b07aa8 100644 --- a/game_localizations.go +++ b/endpoint/game_localizations.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameLocalizations(query string) ([]*pb.GameLocalization, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_localizations.pb", query) +type GameLocalizations struct{ BaseEndpoint } + +func (a *GameLocalizations) Query(query string) ([]*pb.GameLocalization, error) { + resp, err := a.request("https://api.igdb.com/v4/game_localizations.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_modes.go b/endpoint/game_modes.go similarity index 70% rename from game_modes.go rename to endpoint/game_modes.go index b556f5c..ed525be 100644 --- a/game_modes.go +++ b/endpoint/game_modes.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameModes(query string) ([]*pb.GameMode, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_modes.pb", query) +type GameModes struct{ BaseEndpoint } + +func (a *GameModes) Query(query string) ([]*pb.GameMode, error) { + resp, err := a.request("https://api.igdb.com/v4/game_modes.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_release_formats.go b/endpoint/game_release_formats.go similarity index 69% rename from game_release_formats.go rename to endpoint/game_release_formats.go index d9170d3..44010ab 100644 --- a/game_release_formats.go +++ b/endpoint/game_release_formats.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameReleaseFormats(query string) ([]*pb.GameReleaseFormat, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_release_formats.pb", query) +type GameReleaseFormats struct{ BaseEndpoint } + +func (a *GameReleaseFormats) Query(query string) ([]*pb.GameReleaseFormat, error) { + resp, err := a.request("https://api.igdb.com/v4/game_release_formats.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_statuses.go b/endpoint/game_statuses.go similarity index 70% rename from game_statuses.go rename to endpoint/game_statuses.go index 737344e..f857ada 100644 --- a/game_statuses.go +++ b/endpoint/game_statuses.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameStatuses(query string) ([]*pb.GameStatus, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_statuses.pb", query) +type GameStatuses struct{ BaseEndpoint } + +func (a *GameStatuses) Query(query string) ([]*pb.GameStatus, error) { + resp, err := a.request("https://api.igdb.com/v4/game_statuses.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_time_to_beats.go b/endpoint/game_time_to_beats.go similarity index 69% rename from game_time_to_beats.go rename to endpoint/game_time_to_beats.go index be87ea3..b305f0d 100644 --- a/game_time_to_beats.go +++ b/endpoint/game_time_to_beats.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameTimeToBeats(query string) ([]*pb.GameTimeToBeat, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_time_to_beats.pb", query) +type GameTimeToBeats struct{ BaseEndpoint } + +func (a *GameTimeToBeats) Query(query string) ([]*pb.GameTimeToBeat, error) { + resp, err := a.request("https://api.igdb.com/v4/game_time_to_beats.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_types.go b/endpoint/game_types.go similarity index 70% rename from game_types.go rename to endpoint/game_types.go index 062acd1..0161c92 100644 --- a/game_types.go +++ b/endpoint/game_types.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameTypes(query string) ([]*pb.GameType, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_types.pb", query) +type GameTypes struct{ BaseEndpoint } + +func (a *GameTypes) Query(query string) ([]*pb.GameType, error) { + resp, err := a.request("https://api.igdb.com/v4/game_types.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_version_feature_values.go b/endpoint/game_version_feature_values.go similarity index 68% rename from game_version_feature_values.go rename to endpoint/game_version_feature_values.go index 62745b9..654947c 100644 --- a/game_version_feature_values.go +++ b/endpoint/game_version_feature_values.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameVersionFeatureValues(query string) ([]*pb.GameVersionFeatureValue, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_version_feature_values.pb", query) +type GameVersionFeatureValues struct{ BaseEndpoint } + +func (a *GameVersionFeatureValues) Query(query string) ([]*pb.GameVersionFeatureValue, error) { + resp, err := a.request("https://api.igdb.com/v4/game_version_feature_values.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_version_features.go b/endpoint/game_version_features.go similarity index 69% rename from game_version_features.go rename to endpoint/game_version_features.go index bf4fcd7..1e7cd6c 100644 --- a/game_version_features.go +++ b/endpoint/game_version_features.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameVersionFeatures(query string) ([]*pb.GameVersionFeature, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_version_features.pb", query) +type GameVersionFeatures struct{ BaseEndpoint } + +func (a *GameVersionFeatures) Query(query string) ([]*pb.GameVersionFeature, error) { + resp, err := a.request("https://api.igdb.com/v4/game_version_features.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_versions.go b/endpoint/game_versions.go similarity index 70% rename from game_versions.go rename to endpoint/game_versions.go index 5ce792d..90945bd 100644 --- a/game_versions.go +++ b/endpoint/game_versions.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameVersions(query string) ([]*pb.GameVersion, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_versions.pb", query) +type GameVersions struct{ BaseEndpoint } + +func (a *GameVersions) Query(query string) ([]*pb.GameVersion, error) { + resp, err := a.request("https://api.igdb.com/v4/game_versions.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/game_videos.go b/endpoint/game_videos.go similarity index 70% rename from game_videos.go rename to endpoint/game_videos.go index 3adc2e3..063ed3b 100644 --- a/game_videos.go +++ b/endpoint/game_videos.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGameVideos(query string) ([]*pb.GameVideo, error) { - resp, err := g.Request("https://api.igdb.com/v4/game_videos.pb", query) +type GameVideos struct{ BaseEndpoint } + +func (a *GameVideos) Query(query string) ([]*pb.GameVideo, error) { + resp, err := a.request("https://api.igdb.com/v4/game_videos.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/endpoint/games.go b/endpoint/games.go new file mode 100644 index 0000000..1455b6b --- /dev/null +++ b/endpoint/games.go @@ -0,0 +1,29 @@ +package endpoint + +import ( + "fmt" + + pb "github.com/bestnite/go-igdb/proto" + + "google.golang.org/protobuf/proto" +) + +type Games struct{ BaseEndpoint } + +func (a *Games) Query(query string) ([]*pb.Game, error) { + resp, err := a.request("https://api.igdb.com/v4/games.pb", query) + if err != nil { + return nil, fmt.Errorf("failed to request: %w", err) + } + + data := pb.GameResult{} + if err = proto.Unmarshal(resp.Body(), &data); err != nil { + return nil, fmt.Errorf("failed to unmarshal: %w", err) + } + + if len(data.Games) == 0 { + return nil, fmt.Errorf("no results: %s", query) + } + + return data.Games, nil +} diff --git a/genres.go b/endpoint/genres.go similarity index 71% rename from genres.go rename to endpoint/genres.go index 8c6b956..489e683 100644 --- a/genres.go +++ b/endpoint/genres.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetGenres(query string) ([]*pb.Genre, error) { - resp, err := g.Request("https://api.igdb.com/v4/genres.pb", query) +type Genres struct{ BaseEndpoint } + +func (a *Genres) Query(query string) ([]*pb.Genre, error) { + resp, err := a.request("https://api.igdb.com/v4/genres.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/involved_companies.go b/endpoint/involved_companies.go similarity index 69% rename from involved_companies.go rename to endpoint/involved_companies.go index 9ea7e51..acc3605 100644 --- a/involved_companies.go +++ b/endpoint/involved_companies.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetInvolvedCompanies(query string) ([]*pb.InvolvedCompany, error) { - resp, err := g.Request("https://api.igdb.com/v4/involved_companies.pb", query) +type InvolvedCompanies struct{ BaseEndpoint } + +func (a *InvolvedCompanies) Query(query string) ([]*pb.InvolvedCompany, error) { + resp, err := a.request("https://api.igdb.com/v4/involved_companies.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/keywords.go b/endpoint/keywords.go similarity index 70% rename from keywords.go rename to endpoint/keywords.go index 267b843..3e801bd 100644 --- a/keywords.go +++ b/endpoint/keywords.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetKeywords(query string) ([]*pb.Keyword, error) { - resp, err := g.Request("https://api.igdb.com/v4/keywords.pb", query) +type Keywords struct{ BaseEndpoint } + +func (a *Keywords) Query(query string) ([]*pb.Keyword, error) { + resp, err := a.request("https://api.igdb.com/v4/keywords.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/language_support_types.go b/endpoint/language_support_types.go similarity index 69% rename from language_support_types.go rename to endpoint/language_support_types.go index e4c08db..620e0cf 100644 --- a/language_support_types.go +++ b/endpoint/language_support_types.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetLanguageSupportTypes(query string) ([]*pb.LanguageSupportType, error) { - resp, err := g.Request("https://api.igdb.com/v4/language_support_types.pb", query) +type LanguageSupportTypes struct{ BaseEndpoint } + +func (a *LanguageSupportTypes) Query(query string) ([]*pb.LanguageSupportType, error) { + resp, err := a.request("https://api.igdb.com/v4/language_support_types.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/language_supports.go b/endpoint/language_supports.go similarity index 69% rename from language_supports.go rename to endpoint/language_supports.go index bbe0cff..95ae4f3 100644 --- a/language_supports.go +++ b/endpoint/language_supports.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetLanguageSupports(query string) ([]*pb.LanguageSupport, error) { - resp, err := g.Request("https://api.igdb.com/v4/language_supports.pb", query) +type LanguageSupports struct{ BaseEndpoint } + +func (a *LanguageSupports) Query(query string) ([]*pb.LanguageSupport, error) { + resp, err := a.request("https://api.igdb.com/v4/language_supports.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/languages.go b/endpoint/languages.go similarity index 70% rename from languages.go rename to endpoint/languages.go index 8734ecc..c27e093 100644 --- a/languages.go +++ b/endpoint/languages.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetLanguages(query string) ([]*pb.Language, error) { - resp, err := g.Request("https://api.igdb.com/v4/languages.pb", query) +type Languages struct{ BaseEndpoint } + +func (a *Languages) Query(query string) ([]*pb.Language, error) { + resp, err := a.request("https://api.igdb.com/v4/languages.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/multiplayer_modes.go b/endpoint/multiplayer_modes.go similarity index 69% rename from multiplayer_modes.go rename to endpoint/multiplayer_modes.go index 71308d8..2244440 100644 --- a/multiplayer_modes.go +++ b/endpoint/multiplayer_modes.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetMultiplayerModes(query string) ([]*pb.MultiplayerMode, error) { - resp, err := g.Request("https://api.igdb.com/v4/multiplayer_modes.pb", query) +type MultiplayerModes struct{ BaseEndpoint } + +func (a *MultiplayerModes) Query(query string) ([]*pb.MultiplayerMode, error) { + resp, err := a.request("https://api.igdb.com/v4/multiplayer_modes.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/network_types.go b/endpoint/network_types.go similarity index 70% rename from network_types.go rename to endpoint/network_types.go index 8e03733..88425c6 100644 --- a/network_types.go +++ b/endpoint/network_types.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetNetworkTypes(query string) ([]*pb.NetworkType, error) { - resp, err := g.Request("https://api.igdb.com/v4/network_types.pb", query) +type NetworkTypes struct{ BaseEndpoint } + +func (a *NetworkTypes) Query(query string) ([]*pb.NetworkType, error) { + resp, err := a.request("https://api.igdb.com/v4/network_types.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/platform_families.go b/endpoint/platform_families.go similarity index 69% rename from platform_families.go rename to endpoint/platform_families.go index 619d80a..1a9309f 100644 --- a/platform_families.go +++ b/endpoint/platform_families.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPlatformFamilies(query string) ([]*pb.PlatformFamily, error) { - resp, err := g.Request("https://api.igdb.com/v4/platform_families.pb", query) +type PlatformFamilies struct{ BaseEndpoint } + +func (a *PlatformFamilies) Query(query string) ([]*pb.PlatformFamily, error) { + resp, err := a.request("https://api.igdb.com/v4/platform_families.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/platform_logos.go b/endpoint/platform_logos.go similarity index 70% rename from platform_logos.go rename to endpoint/platform_logos.go index acbc1f3..a99d777 100644 --- a/platform_logos.go +++ b/endpoint/platform_logos.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPlatformLogos(query string) ([]*pb.PlatformLogo, error) { - resp, err := g.Request("https://api.igdb.com/v4/platform_logos.pb", query) +type PlatformLogos struct{ BaseEndpoint } + +func (a *PlatformLogos) Query(query string) ([]*pb.PlatformLogo, error) { + resp, err := a.request("https://api.igdb.com/v4/platform_logos.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/platform_types.go b/endpoint/platform_types.go similarity index 70% rename from platform_types.go rename to endpoint/platform_types.go index 2f806e3..ca6c583 100644 --- a/platform_types.go +++ b/endpoint/platform_types.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPlatformTypes(query string) ([]*pb.PlatformType, error) { - resp, err := g.Request("https://api.igdb.com/v4/platform_types.pb", query) +type PlatformTypes struct{ BaseEndpoint } + +func (a *PlatformTypes) Query(query string) ([]*pb.PlatformType, error) { + resp, err := a.request("https://api.igdb.com/v4/platform_types.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/platform_version_companies.go b/endpoint/platform_version_companies.go similarity index 68% rename from platform_version_companies.go rename to endpoint/platform_version_companies.go index f8da230..cc7751c 100644 --- a/platform_version_companies.go +++ b/endpoint/platform_version_companies.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPlatformVersionCompanies(query string) ([]*pb.PlatformVersionCompany, error) { - resp, err := g.Request("https://api.igdb.com/v4/platform_version_companies.pb", query) +type PlatformVersionCompanies struct{ BaseEndpoint } + +func (a *PlatformVersionCompanies) Query(query string) ([]*pb.PlatformVersionCompany, error) { + resp, err := a.request("https://api.igdb.com/v4/platform_version_companies.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/platform_version_release_dates.go b/endpoint/platform_version_release_dates.go similarity index 68% rename from platform_version_release_dates.go rename to endpoint/platform_version_release_dates.go index 5167cf1..8edbd50 100644 --- a/platform_version_release_dates.go +++ b/endpoint/platform_version_release_dates.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPlatformVersionReleaseDates(query string) ([]*pb.PlatformVersionReleaseDate, error) { - resp, err := g.Request("https://api.igdb.com/v4/platform_version_release_dates.pb", query) +type PlatformVersionReleaseDates struct{ BaseEndpoint } + +func (a *PlatformVersionReleaseDates) Query(query string) ([]*pb.PlatformVersionReleaseDate, error) { + resp, err := a.request("https://api.igdb.com/v4/platform_version_release_dates.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/platform_versions.go b/endpoint/platform_versions.go similarity index 69% rename from platform_versions.go rename to endpoint/platform_versions.go index 9de3d2c..c01d802 100644 --- a/platform_versions.go +++ b/endpoint/platform_versions.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPlatformVersions(query string) ([]*pb.PlatformVersion, error) { - resp, err := g.Request("https://api.igdb.com/v4/platform_versions.pb", query) +type PlatformVersions struct{ BaseEndpoint } + +func (a *PlatformVersions) Query(query string) ([]*pb.PlatformVersion, error) { + resp, err := a.request("https://api.igdb.com/v4/platform_versions.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/platform_websites.go b/endpoint/platform_websites.go similarity index 69% rename from platform_websites.go rename to endpoint/platform_websites.go index eee633a..e5aaff8 100644 --- a/platform_websites.go +++ b/endpoint/platform_websites.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPlatformWebsites(query string) ([]*pb.PlatformWebsite, error) { - resp, err := g.Request("https://api.igdb.com/v4/platform_websites.pb", query) +type PlatformWebsites struct{ BaseEndpoint } + +func (a *PlatformWebsites) Query(query string) ([]*pb.PlatformWebsite, error) { + resp, err := a.request("https://api.igdb.com/v4/platform_websites.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/platforms.go b/endpoint/platforms.go similarity index 70% rename from platforms.go rename to endpoint/platforms.go index 5bb17d9..9ec9bb8 100644 --- a/platforms.go +++ b/endpoint/platforms.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPlatforms(query string) ([]*pb.Platform, error) { - resp, err := g.Request("https://api.igdb.com/v4/platforms.pb", query) +type Platforms struct{ BaseEndpoint } + +func (a *Platforms) Query(query string) ([]*pb.Platform, error) { + resp, err := a.request("https://api.igdb.com/v4/platforms.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/player_perspectives.go b/endpoint/player_perspectives.go similarity index 69% rename from player_perspectives.go rename to endpoint/player_perspectives.go index 77e81f9..8a33ffe 100644 --- a/player_perspectives.go +++ b/endpoint/player_perspectives.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPlayerPerspectives(query string) ([]*pb.PlayerPerspective, error) { - resp, err := g.Request("https://api.igdb.com/v4/player_perspectives.pb", query) +type PlayerPerspectives struct{ BaseEndpoint } + +func (a *PlayerPerspectives) Query(query string) ([]*pb.PlayerPerspective, error) { + resp, err := a.request("https://api.igdb.com/v4/player_perspectives.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/popularity_primitives.go b/endpoint/popularity_primitives.go similarity index 69% rename from popularity_primitives.go rename to endpoint/popularity_primitives.go index 46e66d5..dfa830e 100644 --- a/popularity_primitives.go +++ b/endpoint/popularity_primitives.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPopularityPrimitives(query string) ([]*pb.PopularityPrimitive, error) { - resp, err := g.Request("https://api.igdb.com/v4/popularity_primitives.pb", query) +type PopularityPrimitives struct{ BaseEndpoint } + +func (a *PopularityPrimitives) Query(query string) ([]*pb.PopularityPrimitive, error) { + resp, err := a.request("https://api.igdb.com/v4/popularity_primitives.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/popularity_types.go b/endpoint/popularity_types.go similarity index 69% rename from popularity_types.go rename to endpoint/popularity_types.go index c1f3a9a..d2941ac 100644 --- a/popularity_types.go +++ b/endpoint/popularity_types.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetPopularityTypes(query string) ([]*pb.PopularityType, error) { - resp, err := g.Request("https://api.igdb.com/v4/popularity_types.pb", query) +type PopularityTypes struct{ BaseEndpoint } + +func (a *PopularityTypes) Query(query string) ([]*pb.PopularityType, error) { + resp, err := a.request("https://api.igdb.com/v4/popularity_types.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/regions.go b/endpoint/regions.go similarity index 70% rename from regions.go rename to endpoint/regions.go index b4e487a..b7b8d17 100644 --- a/regions.go +++ b/endpoint/regions.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetRegions(query string) ([]*pb.Region, error) { - resp, err := g.Request("https://api.igdb.com/v4/regions.pb", query) +type Regions struct{ BaseEndpoint } + +func (a *Regions) Query(query string) ([]*pb.Region, error) { + resp, err := a.request("https://api.igdb.com/v4/regions.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/release_date_regions.go b/endpoint/release_date_regions.go similarity index 69% rename from release_date_regions.go rename to endpoint/release_date_regions.go index 8a3a8db..b130fca 100644 --- a/release_date_regions.go +++ b/endpoint/release_date_regions.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetReleaseDateRegions(query string) ([]*pb.ReleaseDateRegion, error) { - resp, err := g.Request("https://api.igdb.com/v4/release_date_regions.pb", query) +type ReleaseDateRegions struct{ BaseEndpoint } + +func (a *ReleaseDateRegions) Query(query string) ([]*pb.ReleaseDateRegion, error) { + resp, err := a.request("https://api.igdb.com/v4/release_date_regions.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/release_date_statuses.go b/endpoint/release_date_statuses.go similarity index 69% rename from release_date_statuses.go rename to endpoint/release_date_statuses.go index 61aa06f..8097f44 100644 --- a/release_date_statuses.go +++ b/endpoint/release_date_statuses.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetReleaseDateStatuses(query string) ([]*pb.ReleaseDateStatus, error) { - resp, err := g.Request("https://api.igdb.com/v4/release_date_statuses.pb", query) +type ReleaseDateStatuses struct{ BaseEndpoint } + +func (a *ReleaseDateStatuses) Query(query string) ([]*pb.ReleaseDateStatus, error) { + resp, err := a.request("https://api.igdb.com/v4/release_date_statuses.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/release_dates.go b/endpoint/release_dates.go similarity index 70% rename from release_dates.go rename to endpoint/release_dates.go index b9dcc97..f4b91f3 100644 --- a/release_dates.go +++ b/endpoint/release_dates.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetReleaseDates(query string) ([]*pb.ReleaseDate, error) { - resp, err := g.Request("https://api.igdb.com/v4/release_dates.pb", query) +type ReleaseDates struct{ BaseEndpoint } + +func (a *ReleaseDates) Query(query string) ([]*pb.ReleaseDate, error) { + resp, err := a.request("https://api.igdb.com/v4/release_dates.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/screenshots.go b/endpoint/screenshots.go similarity index 70% rename from screenshots.go rename to endpoint/screenshots.go index c6368dc..7c4b20c 100644 --- a/screenshots.go +++ b/endpoint/screenshots.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetScreenshots(query string) ([]*pb.Screenshot, error) { - resp, err := g.Request("https://api.igdb.com/v4/screenshots.pb", query) +type Screenshots struct{ BaseEndpoint } + +func (a *Screenshots) Query(query string) ([]*pb.Screenshot, error) { + resp, err := a.request("https://api.igdb.com/v4/screenshots.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/search.go b/endpoint/search.go similarity index 81% rename from search.go rename to endpoint/search.go index a6c8b57..e6f3822 100644 --- a/search.go +++ b/endpoint/search.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "encoding/json" @@ -21,8 +21,13 @@ var webSearchCFCookies struct { expires time.Time } -func (g *Client) Search(query string) ([]*pb.Search, error) { - resp, err := g.Request("https://api.igdb.com/v4/search.pb", query) +type Search struct { + BaseEndpoint + flaresolverr *flaresolverr.Flaresolverr +} + +func (a *Search) Search(query string) ([]*pb.Search, error) { + resp, err := a.request("https://api.igdb.com/v4/search.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } @@ -39,13 +44,20 @@ func (g *Client) Search(query string) ([]*pb.Search, error) { return data.Searches, nil } -func (g *Client) WebSearchGames(name string) ([]*pb.Game, error) { +func (a *Search) getFlaresolverr() (*flaresolverr.Flaresolverr, error) { + if a.flaresolverr == nil { + return nil, fmt.Errorf("flaresolverr is not initialized") + } + return a.flaresolverr, nil +} + +func (a *Search) WebSearchGameIDs(name string) ([]uint64, error) { params := url.Values{} params.Add("q", name) params.Add("utf8", "✓") Url := fmt.Sprintf("%s?%s", "https://www.igdb.com/search", params.Encode()) - f, err := g.getFlaresolverr() + f, err := a.getFlaresolverr() if err != nil { return nil, fmt.Errorf("failed to get flaresolverr: %w", err) } @@ -93,5 +105,5 @@ func (g *Client) WebSearchGames(name string) ([]*pb.Game, error) { ids[i] = game.Id } - return GetItemsByIDs(ids, g.GetGames) + return ids, nil } diff --git a/themes.go b/endpoint/themes.go similarity index 71% rename from themes.go rename to endpoint/themes.go index 60029e6..f813a2d 100644 --- a/themes.go +++ b/endpoint/themes.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetThemes(query string) ([]*pb.Theme, error) { - resp, err := g.Request("https://api.igdb.com/v4/themes.pb", query) +type Themes struct{ BaseEndpoint } + +func (a *Themes) Query(query string) ([]*pb.Theme, error) { + resp, err := a.request("https://api.igdb.com/v4/themes.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/webhooks.go b/endpoint/webhooks.go similarity index 67% rename from webhooks.go rename to endpoint/webhooks.go index a14cdfb..8b7a968 100644 --- a/webhooks.go +++ b/endpoint/webhooks.go @@ -1,19 +1,19 @@ -package igdb +package endpoint import ( "fmt" "net/http" "net/url" - - "github.com/bestnite/go-igdb/endpoint" ) -func (g *Client) ActiveWebhook(endpoint endpoint.Endpoint, secret, callbackUrl string) error { +type Webhooks struct{ BaseEndpoint } + +func (a *Webhooks) Register(endpoint EndpointName, secret, callbackUrl string) error { dataBody := url.Values{} dataBody.Set("url", callbackUrl) dataBody.Set("secret", secret) dataBody.Set("method", "update") - resp, err := g.Request(fmt.Sprintf("https://api.igdb.com/v4/%s/webhooks/", endpoint), dataBody.Encode()) + resp, err := a.request(fmt.Sprintf("https://api.igdb.com/v4/%s/webhooks/", endpoint), dataBody.Encode()) if err != nil { return fmt.Errorf("failed to make request: %s: %w", callbackUrl, err) diff --git a/website_types.go b/endpoint/website_types.go similarity index 70% rename from website_types.go rename to endpoint/website_types.go index 62dbfd0..6b9a269 100644 --- a/website_types.go +++ b/endpoint/website_types.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetWebsiteTypes(query string) ([]*pb.WebsiteType, error) { - resp, err := g.Request("https://api.igdb.com/v4/website_types.pb", query) +type WebsiteTypes struct{ BaseEndpoint } + +func (a *WebsiteTypes) Query(query string) ([]*pb.WebsiteType, error) { + resp, err := a.request("https://api.igdb.com/v4/website_types.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/websites.go b/endpoint/websites.go similarity index 70% rename from websites.go rename to endpoint/websites.go index 4f75975..55a1900 100644 --- a/websites.go +++ b/endpoint/websites.go @@ -1,4 +1,4 @@ -package igdb +package endpoint import ( "fmt" @@ -8,8 +8,10 @@ import ( "google.golang.org/protobuf/proto" ) -func (g *Client) GetWebsites(query string) ([]*pb.Website, error) { - resp, err := g.Request("https://api.igdb.com/v4/websites.pb", query) +type Websites struct{ BaseEndpoint } + +func (a *Websites) Query(query string) ([]*pb.Website, error) { + resp, err := a.request("https://api.igdb.com/v4/websites.pb", query) if err != nil { return nil, fmt.Errorf("failed to request: %w", err) } diff --git a/games.go b/games.go deleted file mode 100644 index 0fc8940..0000000 --- a/games.go +++ /dev/null @@ -1,55 +0,0 @@ -package igdb - -import ( - "fmt" - - pb "github.com/bestnite/go-igdb/proto" - - "google.golang.org/protobuf/proto" -) - -func (g *Client) GetGames(query string) ([]*pb.Game, error) { - resp, err := g.Request("https://api.igdb.com/v4/games.pb", query) - if err != nil { - return nil, fmt.Errorf("failed to request: %w", err) - } - - data := pb.GameResult{} - if err = proto.Unmarshal(resp.Body(), &data); err != nil { - return nil, fmt.Errorf("failed to unmarshal: %w", err) - } - - if len(data.Games) == 0 { - return nil, fmt.Errorf("no results: %s", query) - } - - return data.Games, nil -} - -func (g *Client) GetParentGameID(id uint64) (uint64, error) { - detail, err := GetItemByID(id, g.GetGames) - if err != nil { - return 0, fmt.Errorf("failed to fetch IGDB app detail for parent: %d: %w", id, err) - } - hasParent := false - if detail.ParentGame != nil && detail.ParentGame.Id != 0 { - hasParent = true - detail, err = GetItemByID(detail.ParentGame.Id, g.GetGames) - if err != nil { - return 0, fmt.Errorf("failed to fetch IGDB version parent: %d: %w", detail.VersionParent.Id, err) - } - } - for detail.VersionParent != nil && detail.VersionParent.Id != 0 { - hasParent = true - detail, err = GetItemByID(detail.VersionParent.Id, g.GetGames) - if err != nil { - return 0, fmt.Errorf("failed to fetch IGDB version parent: %d: %w", detail.VersionParent.Id, err) - } - } - - if hasParent { - return detail.Id, nil - } - - return id, nil -} diff --git a/igdb.go b/igdb.go deleted file mode 100644 index 4487244..0000000 --- a/igdb.go +++ /dev/null @@ -1,96 +0,0 @@ -package igdb - -import ( - "fmt" - "strings" - - "github.com/bestnite/go-flaresolverr" - "github.com/go-resty/resty/v2" -) - -type Client struct { - clientID string - token *twitchToken - flaresolverr *flaresolverr.Flaresolverr - limiter *rateLimiter -} - -func New(clientID, clientSecret string) *Client { - return &Client{ - clientID: clientID, - limiter: newRateLimiter(4), - token: NewTwitchToken(clientID, clientSecret), - flaresolverr: nil, - } -} - -func NewWithFlaresolverr(clientID, clientSecret string, f *flaresolverr.Flaresolverr) *Client { - return &Client{ - clientID: clientID, - limiter: newRateLimiter(4), - token: NewTwitchToken(clientID, clientSecret), - flaresolverr: f, - } -} - -func (g *Client) Request(URL string, dataBody any) (*resty.Response, error) { - g.limiter.wait() - - t, err := g.token.getToken() - if err != nil { - return nil, fmt.Errorf("failed to get twitch token: %w", err) - } - - resp, err := request().SetBody(dataBody).SetHeaders(map[string]string{ - "Client-ID": g.clientID, - "Authorization": "Bearer " + t, - "User-Agent": "", - "Content-Type": "text/plain", - }).Post(URL) - - if err != nil { - return nil, fmt.Errorf("failed to request: %s: %w", URL, err) - } - return resp, nil -} - -func (g *Client) getFlaresolverr() (*flaresolverr.Flaresolverr, error) { - if g.flaresolverr == nil { - return nil, fmt.Errorf("flaresolverr is not initialized") - } - return g.flaresolverr, nil -} - -func GetItemByID[T any](id uint64, f func(string) ([]*T, error)) (*T, error) { - query := fmt.Sprintf("where id = %d; fields *;", id) - items, err := f(query) - if err != nil { - return nil, err - } - return items[0], nil -} - -func GetItemsByIDs[T any](ids []uint64, f func(string) ([]*T, error)) ([]*T, error) { - idStrSlice := make([]string, len(ids)) - for i, id := range ids { - idStrSlice[i] = fmt.Sprintf("%d", id) - } - - idStr := fmt.Sprintf(`where id = (%s); fields *;`, strings.Join(idStrSlice, ",")) - - return f(idStr) -} - -func GetItemsPagniated[T any](offset, limit int, f func(string) ([]*T, error)) ([]*T, error) { - query := fmt.Sprintf("offset %d; limit %d; f *; sort id asc;", offset, limit) - return f(query) -} - -func GetItemsLength[T any](f func(string) ([]*T, error)) (int, error) { - query := "fields id; sort id desc; limit 1;" - items, err := f(query) - if err != nil { - return 0, err - } - return len(items), nil -} diff --git a/register_endpoints.go b/register_endpoints.go new file mode 100644 index 0000000..6f19689 --- /dev/null +++ b/register_endpoints.go @@ -0,0 +1,358 @@ +package igdb + +import "github.com/bestnite/go-igdb/endpoint" + +func registerAllEndpoints(c *Client) { + c.AgeRatingCategories = &endpoint.AgeRatingCategories{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPAgeRatingCategories), + } + c.EntityEndpoints[endpoint.EPAgeRatingCategories] = c.AgeRatingCategories + + c.AgeRatingContentDescriptions = &endpoint.AgeRatingContentDescriptions{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPAgeRatingContentDescriptions), + } + c.EntityEndpoints[endpoint.EPAgeRatingContentDescriptions] = c.AgeRatingContentDescriptions + + c.AgeRatingContentDescriptionsV2 = &endpoint.AgeRatingContentDescriptionsV2{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPAgeRatingContentDescriptionsV2), + } + c.EntityEndpoints[endpoint.EPAgeRatingContentDescriptionsV2] = c.AgeRatingContentDescriptionsV2 + + c.AgeRatingOrganizations = &endpoint.AgeRatingOrganizations{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPAgeRatingOrganizations), + } + c.EntityEndpoints[endpoint.EPAgeRatingOrganizations] = c.AgeRatingOrganizations + + c.AgeRatings = &endpoint.AgeRatings{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPAgeRatings), + } + c.EntityEndpoints[endpoint.EPAgeRatings] = c.AgeRatings + + c.AlternativeNames = &endpoint.AlternativeNames{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPAlternativeNames), + } + c.EntityEndpoints[endpoint.EPAlternativeNames] = c.AlternativeNames + + c.Artworks = &endpoint.Artworks{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPArtworks), + } + c.EntityEndpoints[endpoint.EPArtworks] = c.Artworks + + c.CharacterGenders = &endpoint.CharacterGenders{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCharacterGenders), + } + c.EntityEndpoints[endpoint.EPCharacterGenders] = c.CharacterGenders + + c.CharacterMugShots = &endpoint.CharacterMugShots{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCharacterMugShots), + } + c.EntityEndpoints[endpoint.EPCharacterMugShots] = c.CharacterMugShots + + c.Characters = &endpoint.Characters{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCharacters), + } + c.EntityEndpoints[endpoint.EPCharacters] = c.Characters + + c.CharacterSpecies = &endpoint.CharacterSpecies{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCharacterSpecies), + } + c.EntityEndpoints[endpoint.EPCharacterSpecies] = c.CharacterSpecies + + c.CollectionMemberships = &endpoint.CollectionMemberships{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCollectionMemberships), + } + c.EntityEndpoints[endpoint.EPCollectionMemberships] = c.CollectionMemberships + + c.CollectionMembershipTypes = &endpoint.CollectionMembershipTypes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCollectionMembershipTypes), + } + c.EntityEndpoints[endpoint.EPCollectionMembershipTypes] = c.CollectionMembershipTypes + + c.CollectionRelations = &endpoint.CollectionRelations{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCollectionRelations), + } + c.EntityEndpoints[endpoint.EPCollectionRelations] = c.CollectionRelations + + c.CollectionRelationTypes = &endpoint.CollectionRelationTypes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCollectionRelationTypes), + } + c.EntityEndpoints[endpoint.EPCollectionRelationTypes] = c.CollectionRelationTypes + + c.Collections = &endpoint.Collections{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCollections), + } + c.EntityEndpoints[endpoint.EPCollections] = c.Collections + + c.CollectionTypes = &endpoint.CollectionTypes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCollectionTypes), + } + c.EntityEndpoints[endpoint.EPCollectionTypes] = c.CollectionTypes + + c.Companies = &endpoint.Companies{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCompanies), + } + c.EntityEndpoints[endpoint.EPCompanies] = c.Companies + + c.CompanyLogos = &endpoint.CompanyLogos{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCompanyLogos), + } + c.EntityEndpoints[endpoint.EPCompanyLogos] = c.CompanyLogos + + c.CompanyStatuses = &endpoint.CompanyStatuses{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCompanyStatuses), + } + c.EntityEndpoints[endpoint.EPCompanyStatuses] = c.CompanyStatuses + + c.CompanyWebsites = &endpoint.CompanyWebsites{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCompanyWebsites), + } + c.EntityEndpoints[endpoint.EPCompanyWebsites] = c.CompanyWebsites + + c.Covers = &endpoint.Covers{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPCovers), + } + c.EntityEndpoints[endpoint.EPCovers] = c.Covers + + c.DateFormats = &endpoint.DateFormats{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPDateFormats), + } + c.EntityEndpoints[endpoint.EPDateFormats] = c.DateFormats + + c.EventLogos = &endpoint.EventLogos{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPEventLogos), + } + c.EntityEndpoints[endpoint.EPEventLogos] = c.EventLogos + + c.EventNetworks = &endpoint.EventNetworks{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPEventNetworks), + } + c.EntityEndpoints[endpoint.EPEventNetworks] = c.EventNetworks + + c.Events = &endpoint.Events{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPEvents), + } + c.EntityEndpoints[endpoint.EPEvents] = c.Events + + c.ExternalGames = &endpoint.ExternalGames{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPExternalGames), + } + c.EntityEndpoints[endpoint.EPExternalGames] = c.ExternalGames + + c.ExternalGameSources = &endpoint.ExternalGameSources{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPExternalGameSources), + } + c.EntityEndpoints[endpoint.EPExternalGameSources] = c.ExternalGameSources + + c.Franchises = &endpoint.Franchises{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPFranchises), + } + c.EntityEndpoints[endpoint.EPFranchises] = c.Franchises + + c.GameEngineLogos = &endpoint.GameEngineLogos{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameEngineLogos), + } + c.EntityEndpoints[endpoint.EPGameEngineLogos] = c.GameEngineLogos + + c.GameEngines = &endpoint.GameEngines{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameEngines), + } + c.EntityEndpoints[endpoint.EPGameEngines] = c.GameEngines + + c.GameLocalizations = &endpoint.GameLocalizations{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameLocalizations), + } + c.EntityEndpoints[endpoint.EPGameLocalizations] = c.GameLocalizations + + c.GameModes = &endpoint.GameModes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameModes), + } + c.EntityEndpoints[endpoint.EPGameModes] = c.GameModes + + c.GameReleaseFormats = &endpoint.GameReleaseFormats{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameReleaseFormats), + } + c.EntityEndpoints[endpoint.EPGameReleaseFormats] = c.GameReleaseFormats + + c.Games = &endpoint.Games{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGames), + } + c.EntityEndpoints[endpoint.EPGames] = c.Games + + c.GameStatuses = &endpoint.GameStatuses{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameStatuses), + } + c.EntityEndpoints[endpoint.EPGameStatuses] = c.GameStatuses + + c.GameTimeToBeats = &endpoint.GameTimeToBeats{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameTimeToBeats), + } + c.EntityEndpoints[endpoint.EPGameTimeToBeats] = c.GameTimeToBeats + + c.GameTypes = &endpoint.GameTypes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameTypes), + } + c.EntityEndpoints[endpoint.EPGameTypes] = c.GameTypes + + c.GameVersionFeatures = &endpoint.GameVersionFeatures{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameVersionFeatures), + } + c.EntityEndpoints[endpoint.EPGameVersionFeatures] = c.GameVersionFeatures + + c.GameVersionFeatureValues = &endpoint.GameVersionFeatureValues{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameVersionFeatureValues), + } + c.EntityEndpoints[endpoint.EPGameVersionFeatureValues] = c.GameVersionFeatureValues + + c.GameVersions = &endpoint.GameVersions{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameVersions), + } + c.EntityEndpoints[endpoint.EPGameVersions] = c.GameVersions + + c.GameVideos = &endpoint.GameVideos{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGameVideos), + } + c.EntityEndpoints[endpoint.EPGameVideos] = c.GameVideos + + c.Genres = &endpoint.Genres{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPGenres), + } + c.EntityEndpoints[endpoint.EPGenres] = c.Genres + + c.InvolvedCompanies = &endpoint.InvolvedCompanies{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPInvolvedCompanies), + } + c.EntityEndpoints[endpoint.EPInvolvedCompanies] = c.InvolvedCompanies + + c.Keywords = &endpoint.Keywords{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPKeywords), + } + c.EntityEndpoints[endpoint.EPKeywords] = c.Keywords + + c.Languages = &endpoint.Languages{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPLanguages), + } + c.EntityEndpoints[endpoint.EPLanguages] = c.Languages + + c.LanguageSupports = &endpoint.LanguageSupports{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPLanguageSupports), + } + c.EntityEndpoints[endpoint.EPLanguageSupports] = c.LanguageSupports + + c.LanguageSupportTypes = &endpoint.LanguageSupportTypes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPLanguageSupportTypes), + } + c.EntityEndpoints[endpoint.EPLanguageSupportTypes] = c.LanguageSupportTypes + + c.MultiplayerModes = &endpoint.MultiplayerModes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPMultiplayerModes), + } + c.EntityEndpoints[endpoint.EPMultiplayerModes] = c.MultiplayerModes + + c.NetworkTypes = &endpoint.NetworkTypes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPNetworkTypes), + } + c.EntityEndpoints[endpoint.EPNetworkTypes] = c.NetworkTypes + + c.PlatformFamilies = &endpoint.PlatformFamilies{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPlatformFamilies), + } + c.EntityEndpoints[endpoint.EPPlatformFamilies] = c.PlatformFamilies + + c.PlatformLogos = &endpoint.PlatformLogos{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPlatformLogos), + } + c.EntityEndpoints[endpoint.EPPlatformLogos] = c.PlatformLogos + + c.Platforms = &endpoint.Platforms{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPlatforms), + } + c.EntityEndpoints[endpoint.EPPlatforms] = c.Platforms + + c.PlatformTypes = &endpoint.PlatformTypes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPlatformTypes), + } + c.EntityEndpoints[endpoint.EPPlatformTypes] = c.PlatformTypes + + c.PlatformVersionCompanies = &endpoint.PlatformVersionCompanies{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPlatformVersionCompanies), + } + c.EntityEndpoints[endpoint.EPPlatformVersionCompanies] = c.PlatformVersionCompanies + + c.PlatformVersionReleaseDates = &endpoint.PlatformVersionReleaseDates{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPlatformVersionReleaseDates), + } + c.EntityEndpoints[endpoint.EPPlatformVersionReleaseDates] = c.PlatformVersionReleaseDates + + c.PlatformVersions = &endpoint.PlatformVersions{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPlatformVersions), + } + c.EntityEndpoints[endpoint.EPPlatformVersions] = c.PlatformVersions + + c.PlatformWebsites = &endpoint.PlatformWebsites{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPlatformWebsites), + } + c.EntityEndpoints[endpoint.EPPlatformWebsites] = c.PlatformWebsites + + c.PlayerPerspectives = &endpoint.PlayerPerspectives{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPlayerPerspectives), + } + c.EntityEndpoints[endpoint.EPPlayerPerspectives] = c.PlayerPerspectives + + c.PopularityPrimitives = &endpoint.PopularityPrimitives{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPopularityPrimitives), + } + c.EntityEndpoints[endpoint.EPPopularityPrimitives] = c.PopularityPrimitives + + c.PopularityTypes = &endpoint.PopularityTypes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPPopularityTypes), + } + c.EntityEndpoints[endpoint.EPPopularityTypes] = c.PopularityTypes + + c.Regions = &endpoint.Regions{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPRegions), + } + c.EntityEndpoints[endpoint.EPRegions] = c.Regions + + c.ReleaseDateRegions = &endpoint.ReleaseDateRegions{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPReleaseDateRegions), + } + c.EntityEndpoints[endpoint.EPReleaseDateRegions] = c.ReleaseDateRegions + + c.ReleaseDates = &endpoint.ReleaseDates{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPReleaseDates), + } + c.EntityEndpoints[endpoint.EPReleaseDates] = c.ReleaseDates + + c.ReleaseDateStatuses = &endpoint.ReleaseDateStatuses{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPReleaseDateStatuses), + } + c.EntityEndpoints[endpoint.EPReleaseDateStatuses] = c.ReleaseDateStatuses + + c.Screenshots = &endpoint.Screenshots{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPScreenshots), + } + c.EntityEndpoints[endpoint.EPScreenshots] = c.Screenshots + + c.Themes = &endpoint.Themes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPThemes), + } + c.EntityEndpoints[endpoint.EPThemes] = c.Themes + + c.Websites = &endpoint.Websites{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPWebsites), + } + c.EntityEndpoints[endpoint.EPWebsites] = c.Websites + + c.WebsiteTypes = &endpoint.WebsiteTypes{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPWebsiteTypes), + } + c.EntityEndpoints[endpoint.EPWebsiteTypes] = c.WebsiteTypes + + c.Webhooks = &endpoint.Webhooks{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPWebhooks), + } + + c.Search = &endpoint.Search{ + BaseEndpoint: *endpoint.NewBaseEndpoint(c.Request, endpoint.EPSearch), + } +}