/annict-rs

Annict API client library for Rust

Primary LanguageRustMIT LicenseMIT

annict-rs

Build Status Build status crates.io

日本語: README-ja.md

Annict API client library for Rust.

Annict is a web service to make and manage watching anime life.

Usage

Add this to your Cargo.toml:

[dependencies]
annis = "0.0.6"

and this to your crate root:

extern crate annis;

Here is a example code that the process from obtaining access_token until making a request for /v1/works.

・Asynchronous example

use annis::nonblocking::Client;
use annis::{ OAuth, Value, Error };
use tokio;

#[tokio::main]
async fn main() -> Result<(), Error> {

	let auth = OAuth::client_id("client_id");
	let url = auth.authorize_url().redirect_uri("https://example.com").scope("read+write").build();

	// Get a certification code.

	let access_token = auth
	.access_token()
	.client_secret("client_secret_key")
	.code("認証コード")
	.build();

	let client = Client::set_token(access_token);
	let works = annis::works().params(vec![("filter_title", "lain")]);

	let json = client.call(works).await?.json::<Value>().await?;

	assert_eq!(json["works"][0]["title"], "serial experiments lain".to_string());

    Ok(())
}

・Synchronous example

use annis::{OAuth, Client , Value, Error };

fn main() -> Result<(), Error> {

	let auth = OAuth::client_id("client_id");
	let url = auth.authorize_url().redirect_uri("https://example.com").scope("read+write").build();

	// Get a certification code.

	let access_token = auth
	.access_token()
	.client_secret("client_secret_key")
	.code("認証コード")
	.build();

	let client = Client::set_token(access_token);
	let works = annis::works().params(vec![("filter_title", "lain")]);

	let json = client.call(works)?.json::<Value>()?;

	assert_eq!(json["works"][0]["title"], "serial experiments lain".to_string());

    Ok(())
}

Auth Requests were made two ways that methods or creating struct.

use annis::{OAuth, AuthorizeUri, AccessToken};
// Please use nonblocking module in case of asynchronous request.
// use annis::nonblocking::{OAuth, AuthorizeUrl, AccessToken};

	let auth = OAuth::client_id("client_id");

// Get Authorize URL
	let instant = auth.authorize_url().build();

	let manual = AuthorizeUrl{
			client_id: "client_id".to_string(),
			redirect_uri: "urn:ietf:wg:oauth:2.0:oob".to_string(),
			scope: "read".to_string()
		}.build();

	assert_eq!(instant, manual);


// Get AccessToken
	let instant = auth
	.access_token()
	.client_secret("client_secret_key")
	.code("certification code")
	.build();

    let manual = AccessToken{
    		client_id: "client_id".to_string(),
    		client_secret: "client_secret_key".to_string(),
    		code: "certification code".to_string(),
    		redirect_uri: "urn:ietf:wg:oauth:2.0:oob".into()
    	}.build();

	assert_eq!(instant, manual);

Parameter arguments accept &str, String and Enum.

extern crate annis;
use annis::{OAuth, Client, Works::*};

	let use_enum = annis::works().params(vec![(filter_title, "lain")]);
	let use_string = annis::works().params(vec![("filter_title", "lain")]);

	assert_eq!(use_enum.params, use_string.params);

License

MIT license
http://opensource.org/licenses/mit-license.php