Skip to content
This repository was archived by the owner on Apr 5, 2022. It is now read-only.

Add organization operations and extend user operations #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions spring-social-github/src/main/java/org/springframework/social/github/api/GitHub.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* @author Craig Walls
* @author Willie Wheeler ([email protected])
* @author Michał Łoza ([email protected])
*/
public interface GitHub extends ApiBinding {

Expand All @@ -53,6 +54,13 @@ public interface GitHub extends ApiBinding {
*/
UserOperations userOperations();

/**
* Returns the portion of the GitHub API containing the organization operations.
*
* @return organization operations
*/
OrganizationOperations organizationOperations();

/**
* Returns the underlying {@link RestOperations} object allowing for consumption of GitHub endpoints that may not be otherwise covered by the API binding.
* The RestOperations object returned is configured to include an OAuth "Authorization" header on all requests.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.github.api;

import java.io.Serializable;

/**
* A GitHub organization
*
* @author Michał Łoza ([email protected])
*/
@SuppressWarnings("serial")
public class GitHubOrganization implements Serializable {

private final long id;

private final String url;

private final String login;

private final String avatarUrl;

private final String description;

public GitHubOrganization(long id, String url, String login, String avatarUrl, String description) {
this.id = id;
this.url = url;
this.login = login;
this.avatarUrl = avatarUrl;
this.description = description;
}

public long getId() { return id; }

public String getUrl() { return url; }

public String getLogin() { return login; }

public String getAvatarUrl() { return avatarUrl; }

public String getDescription() { return description; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.springframework.social.github.api;

import java.util.List;

/**
* Interface defining the operations for working with GitHub organizations.
*
* @author Michał Łoza ([email protected])
*/
public interface OrganizationOperations {

GitHubOrganization getOrganization(String organizationName);

List<GitHubRepo> getOrganizationRepositories(String organizationName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Interface defining the operations for working with GitHub users.
*
* @author Willie Wheeler ([email protected])
* @author Michał Łoza ([email protected])
*/
public interface UserOperations {

Expand All @@ -44,7 +45,35 @@ public interface UserOperations {
* @return the URL to the user's GitHub profile.
*/
String getProfileUrl();


/**
* Retrieve current user repository list
*
* @return list of current user repositories
*/
List<GitHubRepo> getRepositories();

/**
* Retrieve given user repository list
*
* @return list of given user repositories
*/
List<GitHubRepo> getRepositories(String user);

/**
* Retrieve current user organizations list (also private organizations)
*
* @return list of current user organizations
*/
List<GitHubOrganization> getOrganizations();

/**
* Retrieve given user organizations list (only public organizations)
*
* @return list of given user organizations
*/
List<GitHubOrganization> getOrganizations(String user);

/**
* Public operation to return a given user's followers.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.social.github.api.GistOperations;
import org.springframework.social.github.api.GitHub;
import org.springframework.social.github.api.RepoOperations;
import org.springframework.social.github.api.UserOperations;
import org.springframework.social.github.api.*;
import org.springframework.social.github.api.impl.json.GitHubModule;
import org.springframework.social.oauth2.AbstractOAuth2ApiBinding;
import org.springframework.social.oauth2.OAuth2Version;
Expand All @@ -33,12 +30,14 @@
* @author Craig Walls
* @author Willie Wheeler ([email protected])
* @author Andy Wilkinson
* @author Michał Łoza ([email protected])
*/
public class GitHubTemplate extends AbstractOAuth2ApiBinding implements GitHub {
private GistOperations gistOperations;
private RepoOperations repoOperations;
private UserOperations userOperations;

private OrganizationOperations organizationOperations;

/**
* No-arg constructor to support cases in which you want to call the GitHub
* API without requiring authorization. This is useful for public operations,
Expand Down Expand Up @@ -84,6 +83,10 @@ public RestOperations restOperations() {
return getRestTemplate();
}

public OrganizationOperations organizationOperations() {
return organizationOperations;
}

@Override
protected MappingJackson2HttpMessageConverter getJsonMessageConverter() {
MappingJackson2HttpMessageConverter converter = super.getJsonMessageConverter();
Expand All @@ -99,6 +102,7 @@ private void initSubApis() {
this.gistOperations = new GistTemplate(getRestTemplate(), isAuthorized());
this.repoOperations = new RepoTemplate(getRestTemplate(), isAuthorized());
this.userOperations = new UserTemplate(getRestTemplate(), isAuthorized());
this.organizationOperations = new OrganizationTemplate(getRestTemplate(), isAuthorized());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.github.api.impl;

import org.springframework.social.github.api.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import static java.util.Arrays.asList;

/**
* <p>
* Organization template implementation.
* </p>
*
* @author Michał Łoza ([email protected])
*/
public class OrganizationTemplate extends AbstractGitHubOperations implements OrganizationOperations {

private final RestTemplate restTemplate;

/**
* @param restTemplate A RestTemplate
* @param isAuthorizedForUser Boolean indicating whether the RestTemplate is authorized for a user
*/
public OrganizationTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser) {
super(isAuthorizedForUser);
this.restTemplate = restTemplate;
}

public GitHubOrganization getOrganization(String organizationName) {
return restTemplate.getForObject(buildOrganizationUri(""), GitHubOrganization.class, organizationName);
}

public List<GitHubRepo> getOrganizationRepositories(String organizationName) {
return asList(restTemplate.getForObject(buildOrganizationUri("/repos"), GitHubRepo[].class, organizationName));
}

private String buildOrganizationUri(String path) {
return buildUri("orgs/{organization}" + path);
}
}
24 changes: 19 additions & 5 deletions ...-social-github/src/main/java/org/springframework/social/github/api/impl/UserTemplate.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@
*/
package org.springframework.social.github.api.impl;

import static java.util.Arrays.*;
import static java.util.Arrays.asList;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.social.github.api.GitHubUser;
import org.springframework.social.github.api.GitHubUserProfile;
import org.springframework.social.github.api.UserOperations;
import org.springframework.social.github.api.*;
import org.springframework.web.client.RestTemplate;

/**
Expand All @@ -37,6 +34,7 @@
*
* @author Willie Wheeler ([email protected])
* @author Andy Wilkinson
* @author Michał Łoza ([email protected])
*/
public class UserTemplate extends AbstractGitHubOperations implements UserOperations {

Expand All @@ -51,6 +49,22 @@ public UserTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser) {
this.restTemplate = restTemplate;
}

public List<GitHubRepo> getRepositories() {
return asList(restTemplate.getForObject(buildUri("/user/repos"), GitHubRepo[].class));
}

public List<GitHubRepo> getRepositories(String user) {
return asList(restTemplate.getForObject(buildUserUri("/repos"), GitHubRepo[].class, user));
}

public List<GitHubOrganization> getOrganizations() {
return asList(restTemplate.getForObject(buildUri("/user/orgs"), GitHubOrganization[].class));
}

public List<GitHubOrganization> getOrganizations(String user) {
return asList(restTemplate.getForObject(buildUserUri("/orgs"), GitHubOrganization[].class, user));
}

public List<GitHubUser> getFollowers(String user) {
return asList(restTemplate.getForObject(buildUserUri("/followers"), GitHubUser[].class, user));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Jackson module for setting up mixin annotations on GitHub model types.
*
* @author Andy Wilkinson
* @author Michał Łoza
*/
public class GitHubModule extends SimpleModule {

Expand All @@ -42,5 +43,6 @@ public void setupModule(SetupContext context) {
context.setMixInAnnotations(GitHubRepo.class, GitHubRepoMixin.class);
context.setMixInAnnotations(GitHubUser.class, GitHubUserMixin.class);
context.setMixInAnnotations(GitHubUserProfile.class, GitHubUserProfileMixin.class);
context.setMixInAnnotations(GitHubOrganization.class, GitHubOrganizationMixin.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.github.api.impl.json;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.social.github.api.GitHubOrganization;

/**
* Annotated mixin to add annotations to {@link GitHubOrganization}
*
* @author Michał Łoza
*/
abstract class GitHubOrganizationMixin extends GitHubObjectMixin {

long id;

String url;

String login;

String avatarUrl;

String description;

GitHubOrganizationMixin(
@JsonProperty("id") long id,
@JsonProperty("url") String url,
@JsonProperty("login") String login,
@JsonProperty("avatar_url") String avatarUrl,
@JsonProperty("description") String description) {}
}
Loading