Giter VIP home page Giter VIP logo

pokeapinet's Introduction

PokeApiNet

A .Net wrapper for the Pokemon API at https://pokeapi.co.

Targets .Net Standard 2.0+.

NuGet Build Status

Use

using PokeApiNet;

...

// instantiate client
PokeApiClient pokeClient = new PokeApiClient();

// get a resource by name
Pokemon hoOh = await pokeClient.GetResourceAsync<Pokemon>("ho-oh");

// ... or by id
Item clawFossil = await pokeClient.GetResourceAsync<Item>(100);

To see all the resources that are available, see the PokeAPI docs site.

Internally, PokeApiClient uses an instance of the HttpClient class. As such, instances of PokeApiClient are meant to be instantiated once and re-used throughout the life of an application.

Navigation URLs

PokeAPI uses navigation urls for many of the resource's properties to keep requests lightweight, but require subsequent requests in order to resolve this data. Example:

Pokemon pikachu = await pokeClient.GetResourceAsync<Pokemon>("pikachu");

pikachu.Species only has a Name and Url property. In order to load this data, an additonal request is needed; this is more of a problem when the property is a list of navigation URLs, such as the pikachu.Moves.Move collection.

GetResourceAsync includes overloads to assist with resolving these navigation properties. Example:

// to resolve a single navigation url property
PokemonSpecies species = await pokeClient.GetResourceAsync(pikachu.Species);

// to resolve a list of them
List<Move> allMoves = await pokeClient.GetResourceAsync(pikachu.Moves.Select(move => move.Move));

Paging

PokeAPI supports the paging of resources, allowing users to get a list of available resources for that API. Depending on the shape of the resource data, two methods for paging are included, along with overloads to allow for the specification of the page count limit and the page offset. Example:

// get a page of data (defaults to a limit of 20)
NamedApiResourceList<Berry> firstBerryPage = await client.GetNamedResourcePageAsync<Berry>();

// to specify a certain page, use the provided overloads
NamedApiResourceList<Berry> lotsMoreBerriesPage = await client.GetNamedResourcePageAsync<Berry>(60, 2);

Because the Berry resource has a Name property, the GetNamedResourcePageAsync() method is used. For resources that do not have a Name property, use the GetApiResourcePageAsync() method. Example:

ApiResourceList<ContestEffect> contestEffectPage = await client.GetApiResourcePageAsync<ContestEffect>();

Regardless of which method is used, the returning object includes a Results collection that can be used to pull the full resource data. Example:

Berry cheri = await client.GetResourceAsync<Berry>(firstBerryPage.Results[0]);

Refer to the PokeAPI documention to see which resources include a Name property.

IAsyncEnumerable Support

Two methods expose support for IAsyncEnumerable to make paging through all pages super simple: GetAllNamedResourcesAsync<T>() and GetAllApiResourcesAsync<T>(). Example:

await foreach (var berryRef in pokeClient.GetAllNamedResourcesAsync<Berry>())
{
    // do something with each berry reference
}

Caching

Every resource and page response is automatically cached in memory, making all subsequent requests for the same resource or page pull cached data. Example:

// this will fetch the data from the API
Pokemon mew = await pokeClient.GetResourceAsync<Pokemon>(151);

// another call to the same resource will fetch from the cache
Pokemon mewCached = await pokeClient.GetResourceAsync<Pokemon>("mew");

To clear the cache of data:

// clear all caches for both resources and pages
pokeClient.ClearCache();

Additional overloads are provided to allow for clearing the individual caches for resources and pages, as well as by type of cache.

Build

dotnet build

Test

The test bank includes unit tests and integration tests. Integration tests will actually ping the PokeApi server for data while the unit tests run off of mocked data.

dotnet test --filter "Category = Unit"
dotnet test --filter "Category = Integration"

pokeapinet's People

Contributors

jtwotimes avatar jotatoledo avatar anu6is avatar josago97 avatar phrasmotica avatar jruffs7 avatar ldematte avatar pandascoder avatar konvay avatar schinwinkwinsky avatar towodile avatar dependabot[bot] avatar lwebb-dev avatar treesgobark avatar

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.