Skip to content

Commit

Permalink
Add graphql API
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed May 31, 2020
1 parent f63212c commit fc39691
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ modules are available.
- [`actions`] GitHub Actions.
- [`current`] Information about the current user.
- [`gitignore`] Gitignore templates.
- [`graphql`] GraphQL.
- [`issues`] Issues and related items, e.g. comments, labels, etc.
- [`licenses`] License Metadata.
- [`markdown`] Rendering Markdown with GitHub.
Expand All @@ -38,6 +39,7 @@ modules are available.
[`actions`]: https://docs.rs/octocrab/0.4.0/octocrab/actions/struct.ActionsHandler.html
[`current`]: https://docs.rs/octocrab/0.4.0/octocrab/current/struct.CurrentAuthHandler.html
[`gitignore`]: https://docs.rs/octocrab/0.4.0/octocrab/gitignore/struct.GitignoreHandler.html
[`graphql`]: https://docs.rs/octocrab/0.4.0/octocrab/struct.Octocrab.html#graphql-api
[`markdown`]: https://docs.rs/octocrab/0.4.0/octocrab/gitignore/struct.MarkdownHandler.html
[`issues`]: https://docs.rs/octocrab/0.4.0/octocrab/issues/struct.IssueHandler.html
[`licenses`]: https://docs.rs/octocrab/0.4.0/octocrab/licenses/struct.LicenseHandler.html
Expand Down
62 changes: 43 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! - [`actions`] GitHub Actions
//! - [`current`] Information about the current user.
//! - [`gitignore`] Gitignore templates
//! - [`graphql`] GraphQL.
//! - [`issues`] Issues and related items, e.g. comments, labels, etc.
//! - [`licenses`] License Metadata.
//! - [`markdown`] Rendering Markdown with GitHub
Expand All @@ -24,6 +25,7 @@
//! [`actions`]: ./actions/struct.ActionsHandler.html
//! [`current`]: ./current/struct.CurrentAuthHandler.html
//! [`gitignore`]: ./gitignore/struct.GitignoreHandler.html
//! [`graphql`]: ./struct.Octocrab.html#graphql-api
//! [`issues`]: ./issues/struct.IssueHandler.html
//! [`licenses`]: ./licenses/struct.LicenseHandler.html
//! [`markdown`]: ./markdown/struct.MarkdownHandler.html
Expand Down Expand Up @@ -350,20 +352,32 @@ impl Default for Octocrab {
}
}

/// # Constructors
impl Octocrab {
/// Returns a new `OctocrabBuilder`.
pub fn builder() -> OctocrabBuilder {
OctocrabBuilder::default()
}
}

/// GitHub API Methods
/// # GitHub API Methods
impl Octocrab {
/// Creates a `ActionsHandler`.
pub fn actions(&self) -> actions::ActionsHandler {
actions::ActionsHandler::new(self)
}

/// Creates a `CurrentAuthHandler` that allows you to access
/// information about the current authenticated user.
pub fn current(&self) -> api::current::CurrentAuthHandler {
api::current::CurrentAuthHandler::new(self)
}

/// Creates a `GitIgnoreHandler`.
pub fn gitignore(&self) -> gitignore::GitignoreHandler {
gitignore::GitignoreHandler::new(self)
}

/// Creates a `IssueHandler` for the repo specified at `owner/repo`,
/// that allows you to access GitHub's issues API.
pub fn issues(
Expand All @@ -383,24 +397,12 @@ impl Octocrab {
pub fn markdown(&self) -> markdown::MarkdownHandler {
markdown::MarkdownHandler::new(self)
}

/// Creates a `GitIgnoreHandler`.
pub fn gitignore(&self) -> gitignore::GitignoreHandler {
gitignore::GitignoreHandler::new(self)
}

/// Creates an `OrgHandler` for the specified organization,
/// that allows you to access GitHub's organization API.
pub fn orgs(&self, owner: impl Into<String>) -> api::orgs::OrgHandler {
api::orgs::OrgHandler::new(self, owner.into())
}

/// Creates a `TeamHandler` for the specified organization that allows
/// you to access GitHub's teams API.
pub fn teams(&self, owner: impl Into<String>) -> api::teams::TeamHandler {
api::teams::TeamHandler::new(self, owner.into())
}

/// Creates a `PullRequestHandler` for the repo specified at `owner/repo`,
/// that allows you to access GitHub's pull request API.
pub fn pulls(
Expand All @@ -421,19 +423,41 @@ impl Octocrab {
api::repos::RepoHandler::new(self, owner.into(), repo.into())
}

/// Creates a `CurrentAuthHandler` that allows you to access
/// information about the current authenticated user.
pub fn current(&self) -> api::current::CurrentAuthHandler {
api::current::CurrentAuthHandler::new(self)
}

/// Creates a `SearchHandler` that allows you to construct general queries
/// to GitHub's API.
pub fn search(&self) -> search::SearchHandler {
search::SearchHandler::new(self)
}

/// Creates a `TeamHandler` for the specified organization that allows
/// you to access GitHub's teams API.
pub fn teams(&self, owner: impl Into<String>) -> api::teams::TeamHandler {
api::teams::TeamHandler::new(self, owner.into())
}

}

/// # GraphQL API.
impl Octocrab {
/// Sends a graphql query to GitHub, and deserialises the response
/// from JSON.
/// ```no_run
///# async fn run() -> octocrab::Result<()> {
/// let response: serde_json::Value = octocrab::instance()
/// .graphql("query { viewer { login }}")
/// .await?;
///# Ok(())
///# }
/// ```
pub async fn graphql<R: crate::FromResponse>(&self, body: impl AsRef<str>) -> crate::Result<R> {
self.post("/graphql", Some(&serde_json::json!({
"query": body.as_ref(),
}))).await
}
}



/// # HTTP Methods
/// A collection of different of HTTP methods to use with Octocrab's
/// configuration (Authenication, etc.). All of the HTTP methods (`get`, `post`,
Expand Down

0 comments on commit fc39691

Please sign in to comment.