Skip to content

Commit

Permalink
Merge pull request #29 from carols10cents/more-orgs
Browse files Browse the repository at this point in the history
Implement support for listing organizations
  • Loading branch information
softprops committed Mar 22, 2016
2 parents 484c9de + 86e5d10 commit a2af948
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/orgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ fn main() {
for repo in github.org_repos("rust-lang").list(&options).unwrap() {
println!("{}", repo.name)
}

println!("");

println!("My organizations:");
for org in github.orgs().list().unwrap() {
println!("{}", org.login)
}

println!("");

println!("softprops' organizations:");
for org in github.user_orgs("softprops").list().unwrap() {
println!("{}", org.login)
}
}
_ => println!("example missing GITHUB_TOKEN"),
}
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod releases;
pub mod repositories;
pub mod statuses;
pub mod pulls;
pub mod organizations;

pub use rep::*;
pub use errors::Error;
Expand All @@ -31,6 +32,7 @@ use hyper::method::Method;
use hyper::header::{Authorization, ContentLength, UserAgent};
use hyper::status::StatusCode;
use repositories::{Repository, Repositories, UserRepositories, OrganizationRepositories};
use organizations::{Organizations, UserOrganizations};
use std::fmt;
use std::io::Read;
use url::Url;
Expand Down Expand Up @@ -166,6 +168,20 @@ impl<'a> Github<'a> {
Repositories::new(self)
}

/// Return a reference to the collection of organizations that the user
/// associated with the current authentication credentials is in
pub fn orgs(&self) -> Organizations {
Organizations::new(self)
}

/// Return a reference to the collection of organizations a user
/// is publicly associated with
pub fn user_orgs<U>(&self, user: U) -> UserOrganizations
where U: Into<String>
{
UserOrganizations::new(self, user)
}

/// Return a reference to an interface that provides access to a user's gists
pub fn user_gists<O>(&self, owner: O) -> UserGists
where O: Into<String>
Expand Down
45 changes: 45 additions & 0 deletions src/organizations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use self::super::{Github, Result};
use rep::Org;

pub struct Organizations<'a> {
github: &'a Github<'a>,
}

impl<'a> Organizations<'a> {
pub fn new(github: &'a Github<'a>) -> Organizations<'a> {
Organizations { github: github }
}

fn path(&self, more: &str) -> String {
format!("/user/orgs{}", more)
}

/// list the authenticated user's organizations
/// https://developer.github.com/v3/orgs/#list-your-organizations
pub fn list(&self) -> Result<Vec<Org>> {
self.github.get::<Vec<Org>>(&self.path(""))
}
}

pub struct UserOrganizations<'a> {
github: &'a Github<'a>,
user: String,
}

impl<'a> UserOrganizations<'a> {
pub fn new<U>(github: &'a Github<'a>, user: U) -> UserOrganizations<'a>
where U: Into<String>
{
UserOrganizations { github: github, user: user.into() }
}

fn path(&self, more: &str) -> String {
format!("/users/{}/orgs{}", self.user, more)
}

/// list the organizations this user is publicly associated with
/// https://developer.github.com/v3/orgs/#list-user-organizations
pub fn list(&self) -> Result<Vec<Org>> {
self.github.get::<Vec<Org>>(&self.path(""))
}
}
15 changes: 15 additions & 0 deletions src/rep.rs.in
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,21 @@ pub struct User {
pub site_admin: bool,
}

#[derive(Debug, Deserialize)]
pub struct Org {
pub login: String,
pub id: u64,
pub url: String,
pub repos_url: String,
pub events_url: String,
pub hooks_url: String,
pub issues_url: String,
pub members_url: String,
pub public_members_url: String,
pub avatar_url: String,
pub description: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct Commit {
pub label: String,
Expand Down

0 comments on commit a2af948

Please sign in to comment.