If you would like to help the project, pull requests and suggestions are always welcome :)
This crate is using cargo-features so your project compiles only code related to the endpoint you'll be using.
For example, if you're querying only games
and characters
endpoint, Cargo.toml
should look like this:
toml
[dependencies]
rusty_igdb = { "2.0.0", default-features = false, features = ["game", "character"]}
Unless you want the entire codebase from the crate containing all endpoits methods and structs
add this to your Cargo.toml
:
toml
[dependencies]
rusty_igdb = "2.0.0"
Also, this package uses rust nightly build. Be sure to have installed.
You can read how to retrieve those credentials here. With Twitch Access Token and Twitch Client ID in hands, we can bring IGDB API wrapper into scope like this: ```rust // Along with the wrapper, bring the endpoint Structs to scope so your code knows the return type of the Vector use rusty_igdb::{ APIWrapper, models::{ Game, GameResult, Character, CharacterResult } }; use std::env;
fn main() { // Using stored environment variables to unwrap our credentials let accesstoken = env::var("TWITCHACCESSTOKEN").unwrap(); let clientid = env::var("TWITCHCLIENTID").unwrap();
// Authenticating with our API wrapper let apiwrapper = APIWrapper::new(&accesstoken, &client_id).unwrap();
// Using the API wrapper methods to query for Zelda games // Here we are expecting a vector of Game struct, so we used the imported Struct. let zeldagames: GameResult = apiwrapper .build("games") // endpoint .search("zelda") .limit("2") .fields("name") .request() .unwrap();
// Using the API wrapper methods to query for Characters named Mario
// Here we are expecting a vector of Character struct.
let charactersnamedmario: CharacterResult = api_wrapper
.build("characters") // endpoint
.search("mario")
.fields("name")
.request()
.unwrap();
}
``
This example used **environment variables** to store the Twitch retrieved credentials, and then accessing with the rust standard feature
std::env`. In your personal project, you can manage these credentials as you please.
You can avoid typing fields()
query method if you want a full fields response
This would return all fields related to characters that contains Solid Snake
rust
// no .fields() query method returns every field from endpoint
let solid_snake_chars_vec: CharacterResult = api_wrapper
.build("characters")
.search("Solid Snake")
.request()
.unwrap();
The crate offers a JSON public method, so you can customize the response content into your project.
Value
represents the serde_json::Value
struct in the below example.
See the serde_json crate for more information about.
All IGDB endpoints should be available using the request_json()
method.
```rust
let testcharacters: Vec
/*
response should look like this:
[{
"gender": 0,
"id": 4445,
"name": "Beast"
},
{
"gender": 0,
"id": 8988,
"name": "Mr. Wong"
}],
*/
```
The result should look and accessed like this:
json
[{
"gender": 0,
"id": 4445,
"name": "Beast"
},
{
"gender": 0,
"id": 8988,
"name": "Mr. Wong"
}]
rust
let first_character = &test_characters[0]["gender"];
Cargo is primarily distributed under the terms of Apache License (Version 2.0).
See LICENSE-APACHE