#github-api #builder #octocrate #path #response #string #body

octocrate-api-builder

octocrate 的 Github API 构建工具

2 个版本

0.1.1 2023年4月19日
0.1.0 2023年4月11日

#57 in #github-api

Download history 50/week @ 2024-03-15 46/week @ 2024-03-22 74/week @ 2024-03-29 35/week @ 2024-04-05 23/week @ 2024-04-12 24/week @ 2024-04-19 33/week @ 2024-04-26 21/week @ 2024-05-03 24/week @ 2024-05-10 30/week @ 2024-05-17 28/week @ 2024-05-24 16/week @ 2024-05-31 14/week @ 2024-06-07 23/week @ 2024-06-14 26/week @ 2024-06-21 8/week @ 2024-06-28

73 每月下载量

MIT 许可证

22KB
514

带测试的 Github API 构建工具

用法

use octocrate_api_builder::github_api;
use crate::domains::issues::GithubIssue;

github_api! {
  GithubIssueAPI {
    list_repository_issues {
      path "/repos/{}/{}/issues"
      params {
        owner String
        repo String
      }
      response Vec<GithubIssue>
    }
  }
}

编译如下

use std::ops::Deref;
use std::sync::Arc;

use crate::domains::issues::{GithubIssue};
use octocrate_infra::{ExpirableToken, GithubAPIClient, GithubError};

#[derive(Clone, Debug)]
pub struct GithubIssueAPI<T: ExpirableToken + Clone> {
    client: Arc<GithubAPIClient<T>>,
}

impl<T: ExpirableToken + Clone> GithubIssueAPI<T> {
    pub fn new(client: Arc<GithubAPIClient<T>>) -> Self {
        Self { client }
    }

    pub async fn list_repository_issues(
        &self,
        owner: impl Into<String>,
        repo: impl Into<String>,
    ) -> Result<Vec<GithubIssue>, GithubError> {
        let request_url = format!(
            "/repos/{}/{}/issues",
            owner.into(),
            repo.into()
        );

        self.client
            .deref()
            .get(request_url)
            .respond_json::<Vec<GithubIssue>>()
            .await
    }
}

如果您想添加测试

github_api! {
  GithubIssueAPI {
    list_repository_issues {
      /// ...
      /// Add test block
      test {
        params {
          envs.repo_owner
          envs.repo_name
        }
        assert assert!(res.len() > 0)
      }
    }
  }
}

编译如下

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::test_utils;
    use octocrate_infra::GithubResult;

    #[tokio::test]
    async fn list_repository_issues() -> GithubResult<()> {
        let envs = test_utils::load_test_envs()?;
        let api_client = test_utils::create_api_client()?;

        let api = GithubIssueAPI::new(Arc::new(api_client));
        let res = api
            .list_repository_issues(envs.repo_owner, envs.repo_name)
            .await?;

        assert!(res.len() > 0);

        Ok(())
    }
}

您可以传递查询或主体进行测试

github_api! {
  GithubIssueAPI {
    list_repository_issues {
      /// ...
      /// Add test block
      test {
        params {
          envs.repo_owner
          envs.repo_name
        }
        query {
          state "closed"
        }
        assert assert!(res.len() > 0)
      }
    }
  }
}
github_api! {
  GithubIssueAPI {
    create_issue_comment {
      // ...
      test {
        // ...
        body {
          body "Hello World"
        }
        assert assert!(res.body == "Hello World")
      }
    }
  }
}

依赖项

~1.5MB
~35K SLoC