Refactored client request parameter naming and enhanced endpoint methods for better validation and idiomatic behavior.
- Renamed `URL` parameter to `requestURL` in `Client.Request` for improved clarity and to avoid potential naming conflicts.
- Added validation to `BaseEndpoint.GetByID` to prevent queries with an ID of 0, returning an error.
- Modified `BaseEndpoint.GetByIDs` to return an empty slice (`[]*T{}`) instead of an error when no IDs are provided, aligning with common Go idioms for empty result sets.
- Enhanced `BaseEndpoint.Count` method to return an error if the API reports a count of 0, ensuring that a successful count operation always yields a positive result.
		
	
		
			
				
	
	
		
			143 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			143 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package igdb
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"fmt"
 | 
						|
	"strings"
 | 
						|
 | 
						|
	"git.nite07.com/nite/go-igdb/endpoint"
 | 
						|
	"github.com/bestnite/go-flaresolverr"
 | 
						|
	"golang.org/x/time/rate"
 | 
						|
 | 
						|
	"github.com/go-resty/resty/v2"
 | 
						|
)
 | 
						|
 | 
						|
type Client struct {
 | 
						|
	clientID     string
 | 
						|
	token        *twitchToken
 | 
						|
	flaresolverr *flaresolverr.Flaresolverr
 | 
						|
	restyClient  *resty.Client
 | 
						|
	limiter      *rate.Limiter
 | 
						|
 | 
						|
	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,
 | 
						|
		restyClient:  NewRestyClient(),
 | 
						|
		token:        newTwitchToken(clientID, clientSecret),
 | 
						|
		flaresolverr: nil,
 | 
						|
		limiter:      rate.NewLimiter(rate.Limit(4), 4),
 | 
						|
	}
 | 
						|
	registerAllEndpoints(c)
 | 
						|
 | 
						|
	return c
 | 
						|
}
 | 
						|
 | 
						|
type RequestFunc func(method string, URL string, dataBody any) (*resty.Response, error)
 | 
						|
 | 
						|
func NewWithFlaresolverr(clientID, clientSecret string, f *flaresolverr.Flaresolverr) *Client {
 | 
						|
	c := New(clientID, clientSecret)
 | 
						|
	c.flaresolverr = f
 | 
						|
	return c
 | 
						|
}
 | 
						|
 | 
						|
func (g *Client) Request(ctx context.Context, method string, requestURL string, dataBody any) (*resty.Response, error) {
 | 
						|
	err := g.limiter.Wait(ctx)
 | 
						|
	if err != nil {
 | 
						|
		return nil, fmt.Errorf("failed to get rate limiter token: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	t, err := g.token.GetToken(ctx)
 | 
						|
	if err != nil {
 | 
						|
		return nil, fmt.Errorf("failed to get twitch token: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	resp, err := g.restyClient.R().SetContext(ctx).SetBody(dataBody).SetHeaders(map[string]string{
 | 
						|
		"Client-ID":     g.clientID,
 | 
						|
		"Authorization": "Bearer " + t,
 | 
						|
		"User-Agent":    "",
 | 
						|
		"Content-Type":  "text/plain",
 | 
						|
	}).Execute(strings.ToUpper(method), requestURL)
 | 
						|
 | 
						|
	if resp.StatusCode() != 200 {
 | 
						|
		return nil, fmt.Errorf("failed to request, expected 200 but got: %v", resp.StatusCode())
 | 
						|
	}
 | 
						|
 | 
						|
	if err != nil {
 | 
						|
		return nil, fmt.Errorf("failed to request: %s: %w", requestURL, err)
 | 
						|
	}
 | 
						|
	return resp, nil
 | 
						|
}
 |