Skip to content

Commit

Permalink
Merge pull request #133 from dwijnand/rate_limit
Browse files Browse the repository at this point in the history
Introduce GET /rate_limit & test the example in CI
  • Loading branch information
softprops committed Jun 6, 2018
2 parents 8d47012 + 87a81d0 commit 42a82e6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -16,6 +16,7 @@ script:
# https://github.com/rust-lang/rust/issues/47309#issuecomment-359166547
#- RUSTFLAGS="$RUSTFLAGS -C link-dead-code" cargo test
- travis_wait cargo test
- cargo run --example rate_limit


before_cache:
Expand Down
21 changes: 21 additions & 0 deletions examples/rate_limit.rs
@@ -0,0 +1,21 @@
extern crate env_logger;
extern crate futures;
extern crate hubcaps;
extern crate tokio_core;

use tokio_core::reactor::Core;

use hubcaps::{Github, Result};

fn main() -> Result<()> {
drop(env_logger::init());
let mut core = Core::new()?;
let github = Github::new(
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")),
None,
&core.handle(),
);
let status = core.run(github.rate_limit().get())?;
println!("{:#?}", status);
Ok(())
}
6 changes: 6 additions & 0 deletions src/lib.rs
Expand Up @@ -115,6 +115,7 @@ pub mod labels;
pub mod organizations;
pub mod pull_commits;
pub mod pulls;
pub mod rate_limit;
pub mod releases;
pub mod repositories;
pub mod review_comments;
Expand All @@ -129,6 +130,7 @@ pub use errors::{Error, ErrorKind, Result};
use activity::Activity;
use gists::{Gists, UserGists};
use organizations::{Organization, Organizations, UserOrganizations};
use rate_limit::RateLimit;
use repositories::{OrganizationRepositories, Repositories, Repository, UserRepositories};
use search::Search;
use users::Users;
Expand Down Expand Up @@ -282,6 +284,10 @@ where
}
}

pub fn rate_limit(&self) -> RateLimit<C> {
RateLimit::new(self.clone())
}

/// Return a reference to user activity
pub fn activity(&self) -> Activity<C> {
Activity::new(self.clone())
Expand Down
42 changes: 42 additions & 0 deletions src/rate_limit.rs
@@ -0,0 +1,42 @@
//! Rate Limit interface

use hyper::client::Connect;

use {Future, Github};

pub struct RateLimit<C: Clone + Connect> {
github: Github<C>,
}

impl<C: Clone + Connect> RateLimit<C> {
#[doc(hidden)]
pub fn new(github: Github<C>) -> Self {
Self { github }
}

/// https://developer.github.com/v3/rate_limit/#get-your-current-rate-limit-status
pub fn get(&self) -> Future<RateLimitStatus> {
self.github.get("/rate_limit")
}
}

// representations

#[derive(Debug, Deserialize)]
pub struct RateLimitStatus {
pub resources: RateLimitResourcesStatus
}

#[derive(Debug, Deserialize)]
pub struct RateLimitResourcesStatus {
pub core: RateLimitResourceStatus,
pub search: RateLimitResourceStatus,
pub graphql: RateLimitResourceStatus,
}

#[derive(Debug, Deserialize)]
pub struct RateLimitResourceStatus {
pub limit: u32,
pub remaining: u32,
pub reset: u32 // ideally something like std::time::Duration
}

0 comments on commit 42a82e6

Please sign in to comment.