GithubHelp home page GithubHelp logo

igdb-dotnet's Introduction

IGDB .NET SDK

.NET Nuget

A wrapper around the IGDBv4 API using .NET Core (compatible with .NET Standard 2.0+). Note, IGDBv3 is now deprecated and no longer supported so previous versions of this library (1.x) are no longer supported.

Where is this being used?

I built this library primarily for Keep Track of My Games, an app to help organize and track your game collection. That means I use this library in production and I'm motivated to keep it up-to-date and fix issues.

Usage

Install

via Nuget

# .NET Core
dotnet add package IGDB

# .NET Framework
Install-Package IGDB

# Paket
paket add IGDB

Quickstart

Uses RestEase so you can easily call the API methods. Since IGDB uses APIcalypse query language, you will need to pass the query as a string. TODO: Create a query builder.

Endpoints can be passed using the constants in IGDB.IGDBClient.Endpoints or as a custom string.

Models are domain objects found in IGDB.Models and correspond directly to the Endpoints documentation.

Some fields can be expanded which is handled via the IdentityOrValue and IdentitiesOrValues wrapper.

The IGDB API uses the Twitch Developer program to power its API so it requires an OAuth client app ID and secret, which you can find in your Developer Portal. Passing these to the IGDBClient constructor will handle the OAuth bearer token management for you (see Custom Token Management below for details).

See below for an example:

using IGDB;
using IGDB.Models;

var igdb = new IGDBClient(
  // Found in Twitch Developer portal for your app
  Environment.GetEnvironmentVariable("IGDB_CLIENT_ID"),
  Environment.GetEnvironmentVariable("IGDB_CLIENT_SECRET")
);

// Simple fields
var games = await igdb.QueryAsync<Game>(IGDBClient.Endpoints.Games, query: "fields id,name; where id = 4;");
var game = games.First();
game.Id; // 4
game.Name; // Thief

// Reference fields
var games = await igdb.QueryAsync<Game>(IGDBClient.Endpoints.Games, query: "fields id,name,cover; where id = 4;");
var game = games.First();
game.Cover.Id.HasValue; // true
game.Cover.Id.Value; // 65441

// Expanded fields
var games = await igdb.QueryAsync<Game>(IGDBClient.Endpoints.Games, query: "fields id,name,cover.*; where id = 4;");
var game = games.First();

// Id will not be populated but the full Cover object will be
game.Cover.Id.HasValue; // false
game.Cover.Value.Width; // 756
game.Cover.Value.Height;

Custom Token Management

By default this client will request a OAuth bearer token on your behalf that is valid for 60 days and will store it statically in memory. If an invalid token is used and a 401 response is received, it will automatically acquire a new token and retry the request. See TokenManagement.cs for the default implementation.

To customize this behavior, such as storing the bearer token in persistent storage like SQL or NoSQL, you will have to pass your own ITokenStore implementation.

using IGDB;

class CustomTokenStore : ITokenStore {

  Task<TwitchAccessToken> GetTokenAsync() {

    // Get token from database, etc.
    var token = // ...
    return token;
  }

  Task<TwitchAccessToken> StoreTokenAsync(TwitchAccessToken token) {
    // Store new token in database, etc.
    return token;
  }

}

// Create an IGDB API client, passing custom token store
var api = new IGDBClient(clientId, clientSecret, new CustomTokenStore());
  • GetTokenAsync - Gets a token
  • StoreTokenAsync - Stores a new token

When using dependency injection, the lifetime of the ITokenStore can be whatever you prefer -- a singleton is used by default.

Note: If you build a token store, please open a PR and add a link here so others can use it!

Default Serialization Settings

IGDB uses some interesting patterns to serialize responses. When using field expansion (foo.*) it will expand that JSON property to be an array of objects instead of just an array of IDs. The default serializer settings of this client will handle all of this for you. It also handles serializing/deserializing snake_case naming and Unix timestamps.

To get a reference to the default serializer settings (for example, to serialize/deserialize yourself separately), you can access the static variable IGDBClient.DefaultJsonSerializerSettings.

Images

Use the IGDB.ImageHelper to generate URLs for images. It exposes the raw template URL IGDB_IMAGE_TEMPLATE and mapping ImageSizeMap of ImageSize enum to string value:

var games = await igdb.QueryAsync<Game>(IGDB.IGDBClient.Endpoints.Games, query: "fields artworks.image_id; where id = 4;");
var artworkImageId = games.First().Artworks.Values.First().ImageId;

// Thumbnail
var thumb = IGDB.ImageHelper.GetImageUrl(imageId: artworkImageId, size: ImageSize.Thumb, retina: false);
var thumb2X = IGDB.ImageHelper.GetImageUrl(imageId: artworkImageId, size: ImageSize.Thumb, retina: true);

// Covers
var coverSmall = IGDB.ImageHelper.GetImageUrl(imageId: artworkImageId, size: ImageSize.CoverSmall, retina: false);

Contributing

Prerequisites

  • .NET Core SDK 2.2.5
  • Visual Studio Code or VS 2017+

Local Development

Add environment variables:

export IGDB_CLIENT_ID=[your OAuth app client ID]
export IGDB_CLIENT_SECRET=[your OAuth app client secret]

These are found in your Twitch Developer Console.

You don't need this to be defined globally but it does need to be in scope while running dotnet test.

dotnet build
dotnet test

igdb-dotnet's People

Contributors

hrkings avatar jtone123 avatar kamranayub avatar mcknight89 avatar thundernerd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

igdb-dotnet's Issues

QueryAsync to return Count

A query request returns a count as x-count part of the headers.

Add an optional out parameter to QueryAsync to output this count and remove the need to call "Query" and "Count" when you are doing paginated search.

Support new API fields added Oct 22, 2021

Via Discord: https://discord.com/channels/114736716505546761/548536667884224578/901102704540155945

  • New external Game IDs https://api-docs.igdb.com/#external-game:
    • TheGamesDB - 2
    • Sony / PSN - 16
    • Pushsquare - 6
    • Microsoft (Title ID) - 18
    • Amazon ASINs
    • Amazon Luna
    • Amazon ADG
    • Utomik
    • Epic Game Store
    • Oculus
    • Itch.io
  • New game status: Delisted
  • New release region: Brazil
  • New age ratings https://api-docs.igdb.com/#age-rating: CERO, USK, GRAC, ClassInd, ACB (and of course their corresponding descriptors are available as well)

feat: undocumented external game categories

There's a few undocumented external game enums I noticed:

  • Gamersgate - 8
  • Gamespress - 19
  • Play Asia - 24

Example queries:

  • fields id,external_games.*; where id = (8582);
  • fields id,external_games.*; where id = (27316);

Implementation of 'ITokenStore'

will store it statically in memory., do you mean that the access token is stored in the main memory and destroyed after the system is shut down or the application is shut down?

PS: I didn't Know where to ask that, so I created this Issue?

AgeRatingContentDescription missing some categories

In AgeRatingContentDescription response there are missing categories.
Right now, this is what we have:

public enum AgeRatingContentDescriptionCategory
  {
    PEGI = 1,
    ESRB = 2
  }

But we need to include more:

public enum AgeRatingContentDescriptionCategory
  {
    ESRB = 1,
    PEGI = 2,
    CERO = 3,
    USK = 4,
    GRAC = 5,
    CLASS_IND = 6,
    ACB = 7
  }

QA : How do I get artworks by game id?

I can get the artworks of a game by calling the game endpoint with field artwork, -

public async Artworks GetArtworksByGameId(string id)
        {
            var r = await IGDBClient.QueryAsync<Game>(IGDBClient.Endpoints.Games,
                $"fields id, artworks.*; where id = {id};");
            return r.First().Artworks.Values;
        }

which would give me the artwork object or the values, whichever we want,

but I'm thinking there has to be a way to get artworks by game id -
this wrapper seems to do something like that.

Is there a way to achieve this here?

Missing models

Hey,

I've noticed that there are missing some models:

  • GameLocalization
  • LanguageSupport
  • LanguageSupportType
  • Language
  • Region
  • ReleaseDateStatus

Is it intended or can be added?

Migrate to refit

Unfortunately, RestEase is not compatible with Xamarin.iOS cause:

It won't work on platforms which don't support runtime code generation, including .NET Native and iOS.

Would be great if it would be ported to Refit instead of RestEase.

API update: Collections

Changes to Collection:
As part of our migration from a 1:many to a many:many relationship between games and series (collections), we have migrated the data that used to be in the old collection field into the new collections field in the games endpoint. From collections, this can be found through the existing games filed. For the next 6 months (until August 2024) we will serve the data in both locations, then we will deprecate the collection field in the games endpoint and the games field in the collections endpoint

Actions required:

  • If you consume series (collection) data at some point in the next six months:
    • Move from the collection field to the collections field in the games endpoint.
  • Remove Collection from model
  • Add Collections

Support for private endpoints

Would be great to explicitly support authentication required for private endpoints - or at the least mention in the README.md that they are not supported (yet)

System.ArgumentOutOfRangeException for parameter "seconds"

The following code

var games = await mainWindow.igdbClient.QueryAsync<IGDB.Models.Game>(IGDB.IGDBClient.Endpoints.Games, query: "search \"" + SearchBox.Text + "\"; fields *,platforms.*,platforms.versions.platform_logo.image_id,release_dates.*,release_dates.platform.*,involved_companies.*,involved_companies.company.*,genres.*,keywords.*,franchise.*,screenshots.image_id,artworks.image_id,cover.image_id; limit 18;")

with "No More Heroes 2" replacing SearchBox.Text gives me this exception:
image
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.