Rust#

OpenAPI Specificationから生成 されたRust用クライアントライブラリーを使用するサンプルを示します。

Cargo.tomlに以下のライブラリーへの依存が定義されていることを前提としています。

[dependencies]
tokio = { version = "1.21", features = ["rt", "macros"] }
oauth2 = { version = "4.2" }
intdash = { path = "./intdash" }

APIトークンを使用してAPIにアクセスする#

以下のコードでは、APIトークンを使用して認証を行います。その後、認証したユーザー自身の情報を取得します。

実行時には以下を指定してください。

  • url: 接続先intdash APIのベースURL

  • api_token: ユーザーのAPIトークン(APIトークンはMy Pageで作成できます)

use intdash::apis::{
    authentication_service_me_api::get_me,
    configuration::{ApiKey, Configuration},
   };

#[tokio::main(flavor = "current_thread")]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let url = "http://localhost:8080/api";
    let api_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    let mut cfg = Configuration::new();
    cfg.base_path = url.to_string();
    cfg.api_key = Some(ApiKey {
        prefix: None,
        key: api_token.to_string(),
    });

    let me = get_me(&cfg).await?;

    println!("Hello {}", me.name);

    Ok(())
}

OAuth2トークンを使用してAPIにアクセスする#

以下のコードでは、OAuth2トークンを使って認証を行います。その後、認証したユーザー自身の情報を取得します。

実行時には以下を指定してください。

  • url: 接続先intdash APIのベースURL

  • client_id: エッジのUUID

  • client_secret: エッジの作成時に発行されるクライアントシークレット

use oauth2::{
    basic::BasicClient, reqwest::async_http_client, AuthType, AuthUrl, ClientId, ClientSecret,
    TokenResponse, TokenUrl,
};

use intdash::apis::{authentication_service_edges_api::get_me_as_edge, configuration::Configuration};

#[tokio::main(flavor = "current_thread")]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let url = "http://localhost:8080/api";
    let client_id = "00000000-0000-0000-0000-000000000000";
    let client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    let c = BasicClient::new(
        ClientId::new(client_id.to_string()),
        Some(ClientSecret::new(client_secret.to_string())),
        AuthUrl::new(format!("{}{}", url, "/auth/oauth2/authorization"))?,
        Some(TokenUrl::new(format!("{}{}", url, "/auth/oauth2/token"))?),
    )
    .set_auth_type(AuthType::RequestBody);

    let token_result = c
        .exchange_client_credentials()
        .request_async(async_http_client)
        .await?;

    let mut cfg = Configuration::new();
    cfg.base_path = url.to_string();
    cfg.bearer_access_token = Some(token_result.access_token().secret().to_string());

    let me = get_me_as_edge(&cfg).await?;

    println!("Hello {}", me.name);

    Ok(())
}

過去の計測のデータポイントを取得する#

以下のコードでは、対象の計測からデータポイントを10個取得します。

実行時には以下を指定してください。

  • url: 接続先intdash APIのベースURL

  • client_id: エッジのUUID

  • client_secret: エッジの作成時に発行されるクライアントシークレット

  • measurement_uuid: 計測のUUID

use oauth2::{
    basic::BasicClient, reqwest::async_http_client, AuthType, AuthUrl, ClientId, ClientSecret,
    TokenResponse, TokenUrl,
};

use intdash::apis::{configuration::Configuration, measurement_service_data_points_api::list_project_data_points};

#[tokio::main(flavor = "current_thread")]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let url = "http://localhost:8080/api";
    let client_id = "00000000-0000-0000-0000-000000000000";
    let client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    let measurement_uuid = "00000000-0000-0000-0000-000000000000";
    let idq = "*";

    let c = BasicClient::new(
        ClientId::new(client_id.to_string()),
        Some(ClientSecret::new(client_secret.to_string())),
        AuthUrl::new(format!("{}{}", url, "/auth/oauth2/authorization"))?,
        Some(TokenUrl::new(format!("{}{}", url, "/auth/oauth2/token"))?),
    )
    .set_auth_type(AuthType::RequestBody);

    let token_result = c
        .exchange_client_credentials()
        .request_async(async_http_client)
        .await?;

    let mut cfg = Configuration::new();
    cfg.base_path = url.to_string();
    cfg.bearer_access_token = Some(token_result.access_token().secret().to_string());

    let data_points = list_project_data_points(
        &cfg,
        "00000000-0000-0000-0000-000000000000",
        measurement_uuid,
        Some(vec![idq.to_string()]),
        None,
        None,
        None,
        None,
        None,
        None,
        Some(10),
        None,
        None,
    )
    .await?;

    println!("Data Points {:?}", data_points);

    Ok(())
}