2 releases

0.1.1 Apr 19, 2023
0.1.0 Apr 11, 2023

#55 in #github-api

Download history 19/week @ 2023-12-13 14/week @ 2023-12-20 11/week @ 2023-12-27 3/week @ 2024-01-03 17/week @ 2024-01-10 11/week @ 2024-01-17 2/week @ 2024-01-24 5/week @ 2024-01-31 21/week @ 2024-02-07 111/week @ 2024-02-14 66/week @ 2024-02-21 58/week @ 2024-02-28 44/week @ 2024-03-06 50/week @ 2024-03-13 49/week @ 2024-03-20 36/week @ 2024-03-27

185 downloads per month
Used in 10 crates (via octocrate)

MIT license

22KB
514 lines

Github API Builder with tests

Usage

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>
    }
  }
}

Will compile as below

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
    }
}

If you want add tests

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

Will compile as below

#[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(())
    }
}

You can pass query or body to test

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")
      }
    }
  }
}

Dependencies

~1.5MB
~33K SLoC