#request-body #github #github-api

octocrate-webhooks

基于Rust的全面GitHub REST API库

2个稳定版本

1.0.1 2024年8月3日
1.0.0 2024年5月20日

HTTP客户端 中排名90

Download history 180/week @ 2024-05-17 18/week @ 2024-05-24 123/week @ 2024-08-02

每月下载123

MIT许可证

1.5MB
30K SLoC

Octocrate

octocrate是基于Rust的全面GitHub REST API库。

octocrate MIT

英文 | 简体中文

特性

  • 完全符合GitHub REST API文档中的官方文档GitHub REST API文档
  • 为Body / Query参数提供完整的类型限制
  • 利用功能标志为单个API依赖项
  • 支持GitHub应用对安装API的请求
  • 支持安装访问令牌和个人访问令牌
  • 定义所有Webhooks事件类型

依赖项

[dependencies]
octocrate = "*"

使用功能来选择性地导入所需的API

octocrate = { version = "*", features = ["repos", "git", "pulls", "issues", "users", "search"] }

或者使用full功能来导入所有API和Webhooks(注意:这将增加编译时间)

octocrate = { version = "*", features = ["full"] }

类型依赖

您也可以仅导入类型而无需使用相应的API

octocrate-types = "*"

使用功能来选择性地导入所需类型

octocrate-types = { version = "*", features = ["repos", "git", "pulls", "issues", "users", "search"] }

导入Webhooks类型

octocrate-webhooks = { version = "*", features = ["pull_request", "push"] }

您可以在octocrate-types文档octocrate-webhooks文档中找到所有受支持的功能和类型。

示例

创建一个默认的GitHub API配置,并使用它获取存储库的拉取请求

use octocrate::{APIConfig, Error, GitHubAPI};

#[tokio::main]
async fn main() {
  // Create a default GitHub API configuration
  let config = APIConfig::default().shared();

  let api = GitHubAPI::new(&config);

  let pull_request = api
    .pulls
    .get("panghu-huang", "octocrate", 1)
    .send()
    .await
    .unwrap();

  // ..
}

直接导入相应的API

// Import the repos API instead of GitHubAPI
use octocrate::{repos::GitHubReposAPI, APIConfig, GitHubAPI, PersonalAccessToken};

#[tokio::main]
async fn main() {
  // Create a personal access token
  let personal_access_token = PersonalAccessToken::new("YOUR_PERSONAL_ACCESS_TOKEN");

  // Use the personal access token to create a API configuration
  let config = APIConfig::with_token(personal_access_token).shared();

  let repos_api = GitHubReposAPI::new(&config);

  let commit = repos_api
    .get_commit(
      "panghu-huang",
      "octocrate",
      "18ff8ed1a3437649e7d87bec9a7d4fe5562f6ad3",
    )
    .send()
    .await
    .unwrap();
}

使用GitHub应用

use octocrate::{APIConfig, AppAuthorization, GitHubAPI};

#[tokio::main]
async fn main() {
  let app_id = "YOUR_APP_ID";
  let private_key = "YOUR_PRIVATE_KEY";

  // Create a GitHub App authorization
  let app_authorization = AppAuthorization::new(app_id, private_key);

  // Use the GitHub App authorization to create an API configuration
  let config = APIConfig::with_token(app_authorization).shared();

  let api = GitHubAPI::new(&config);

  // Get the Installation for a repository
  let installation = api
    .apps
    .get_repo_installation("panghu-huang", "octocrate")
    .send()
    .await
    .unwrap();

  // Get the Installation Access Token for this Installation
  let installation_token = api
    .apps
    .create_installation_access_token(installation.id)
    .send()
    .await
    .unwrap();

  // Use the Installation Access Token to create a new API configuration
  let config = APIConfig::with_token(installation_token).shared();

  let api = GitHubAPI::new(&config);

  let repository = api
    .repos
    .get("panghu-huang", "octocrate")
    .send()
    .await
    .unwrap();

  // ..
}

请求体参数

use octocrate::{
  issues, APIConfig, AuthorAssociation, GitHubAPI, PersonalAccessToken,
};

#[tokio::main]
async fn main() {
  let personal_access_token = PersonalAccessToken::new("YOUR_PERSONAL_ACCESS_TOKEN");

  let config = APIConfig::with_token(personal_access_token).shared();

  let api = GitHubAPI::new(&config);

  // Create a comment request
  // https://github.com/panghu-huang/octocrate/pull/1#issuecomment-2041280635
  let comment = issues::create_comment::Request {
    body: "Hello, world! (Created by octocrate)".to_string(),
  };

  // Create a comment on the issue
  let issue_comment = api
    .issues
    .create_comment("panghu-huang", "octocrate", 1)
    .body(&comment)
    .send()
    .await
    .unwrap();
}

请求查询参数

use octocrate::{
  pulls, APIConfig, Error, GitHubAPI, PersonalAccessToken,
};

#[tokio::main]
async fn main() {
  let personal_access_token = PersonalAccessToken::new("YOUR_PERSONAL_ACCESS_TOKEN");

  let config = APIConfig::with_token(personal_access_token).shared();

  let api = GitHubAPI::new(&config);

  // Use builder pattern to construct query parameters
  let query = pulls::list::Query::builder()
    .state(pulls::list::QueryState::Open)
    .per_page(10)
    .build()

  let pull_requests = api
    .pulls
    .list("facebook", "react")
    .query(&query)
    .send()
    .await
    .unwrap();

  // ..
}

您可以在octocrate/examples目录中找到更多示例代码。

贡献

欢迎贡献!请随意打开问题或提交拉取请求以改进项目。

许可证

本项目采用MIT许可证。

依赖项

~0.7–1.6MB
~35K SLoC