Skip to content

Vigintil/pokeapi-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pokeapi-c

A PokéAPI wrapper in C with disk caching, proxy support and both synchronous and asynchronous APIs.

Dependencies

  • pthreads
  • libcurl
  • json-c

Install on Debian/Ubuntu: sudo apt install libcurl4-openssl-dev libjson-c-dev

Build

git clone https://github.com/Vigintil/pokeapi-c.git
cd pokeapi-c
make
sudo make install

Examples

Synchronous

#include <stdio.h>
#include <pokeapi/pokeapi.h>

int main(void) {
    pokeapi_client_t pokeapi = pokeapi_client_init();
    pokeapi_set_proxy(pokeapi, "socks4://127.0.0.1:9050", NULL);

    pokeapi_berry_t *berry = pokeapi_get_berry_by_name(pokeapi, "cheri");
    if (berry) {
        printf("Berry: %llu, growth: %d h\n", berry->id, berry->growth_time);
        pokeapi_free_berry(berry);
    } else {
        printf("Error code: %i. Error message: %s\n", pokeapi_error(pokeapi), pokeapi_error_str(pokeapi));
    }

    pokeapi_client_destroy(pokeapi);
    return 0;
}

Asynchronous

#include <stdio.h>
#include <pokeapi/pokeapi.h>

int done = 0;

void berry_callback(pokeapi_async_client_t client, void *response, void *userdata) {
    if (response) {
        pokeapi_berry_t *berry = (pokeapi_berry_t *)response;
        printf("Berry: %llu, growth: %d h\n", berry->id, berry->growth_time);
        pokeapi_free_berry(berry);
    } else {
        printf("Error code: %i. Error message: %s\n",
               pokeapi_async_error(client), pokeapi_async_error_str(client));
    }
    *(int *)userdata = 1;
}

int main(void) {
    pokeapi_async_client_t client = pokeapi_async_client_init();

    pokeapi_async_get_berry_by_name(client, "cheri", berry_callback, &done);
    pokeapi_async_wait(client);

    pokeapi_async_client_destroy(client);
    return 0;
}

Caching

The library caches responses in ~/.cache/pokeapi/. Freshness is determined by HTTP headers. Expired entries are revalidated with If-None-Match header.

About

PokeAPI wrapper in C

Topics

Resources

License

Stars

Watchers

Forks

Contributors