Skip to content

awaitlink/rvk

Repository files navigation

rvk

A set of crates to be able to easily access VK (VKontakte) API in Rust.

The combined changelog for all crates is available here.

Crates

  • rvk (docs) — simple crate for accessing VK API (using async/await);
  • rvk_methods (docs) — provides VK API methods to avoid the need to specify them as strings, depends on rvk to call the methods;
  • rvk_objects (docs) — represents various objects that are returned as JSON by the VK API.

Note that for rvk_methods and rvk_objects, the supported versions of the VK API may be different. Consult the API_VERSION constant in these crates to learn which versions they support.

Usage

Add the necessary dependencies to your project. For example, to use all 3:

Cargo.toml

[dependencies]
rvk = "0.23"
rvk_methods = "0.1"
rvk_objects = "0.1"

Now you can take a look at the documentation (linked above for each crate) to learn more about the available functions.

Example using all 3 crates

To use this example, you will also need the tokio crate for the tokio::main attribute proc macro.

Cargo.toml

[dependencies]
tokio = { version = "1.0", features = ["full"] }

main.rs

use rvk::Params;
use rvk_methods::users;
use rvk_objects::user::User;

#[tokio::main]
async fn main() {
    // Create an API client that uses the API version supported by `rvk_methods`.
    let api = rvk_methods::supported_api_client("your_access_token");

    // A HashMap to store parameters.
    let mut params = Params::new();
    params.insert("user_ids".into(), "1".into());

    // Use a type from `rvk_objects` as the result type.
    let res = users::get::<Vec<User>>(&api, params).await;

    match res {
        Ok(users) => {
            let user: &User = &users[0];

            println!(
                "User #{} is {} {}.",
                user.id, user.first_name, user.last_name
            );
        }
        Err(e) => println!("{}", e),
    };
}