-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from orekyuu/fetch-github-repository
GitHubからリポジトリの情報を取得できるようにした
- Loading branch information
Showing
5 changed files
with
561 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package net.tuzigiri.domain.github; | ||
|
||
import net.tuzigiri.domain.identity.AccessToken; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
|
||
@Component | ||
public class GithubApi { | ||
|
||
private final RestTemplate restOperations; | ||
|
||
public GithubApi(RestTemplate restOperations) { | ||
this.restOperations = restOperations; | ||
} | ||
|
||
/** | ||
* アクセストークンに紐づくユーザーのリポジトリ一覧を返します | ||
* | ||
* @param accessToken アクセストークン | ||
* @return アクセストークンに紐づくユーザーのリポジトリ一覧 | ||
*/ | ||
public List<GithubRepository> fetchRepositories(AccessToken accessToken) { | ||
try { | ||
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); | ||
headers.add("Authorization", String.format("token %s", accessToken.getTokenValue())); | ||
RequestEntity requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("https://api.github.com/user/repos")); | ||
ResponseEntity<List<GithubRepository>> entity = restOperations.exchange(requestEntity, new ParameterizedTypeReference<List<GithubRepository>>() { | ||
}); | ||
return entity.getBody(); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
src/main/java/net/tuzigiri/domain/github/GithubRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package net.tuzigiri.domain.github; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* @see <a href="https://developer.github.com/v3/repos/#response">github document</a> | ||
*/ | ||
public class GithubRepository { | ||
private final long id; | ||
private final String name; | ||
private final String fullName; | ||
private final boolean isPrivate; | ||
private final GithubUser owner; | ||
private final String repositoryUrl; | ||
private final LocalDateTime createdAt; | ||
private final LocalDateTime updatedAt; | ||
private final LocalDateTime pushedAt; | ||
private final String gitUrl; | ||
private final String sshUrl; | ||
private final String cloneUrl; | ||
private final long size; | ||
private final long starsCount; | ||
private final long watchersCount; | ||
private final long forksCount; | ||
private final String language; | ||
|
||
@JsonCreator | ||
public GithubRepository( | ||
@JsonProperty("id") long id, | ||
@JsonProperty("name") String name, | ||
@JsonProperty("full_name") String fullName, | ||
@JsonProperty("private") boolean isPrivate, | ||
@JsonProperty("owner") GithubUser owner, | ||
@JsonProperty("html_url") String repositoryUrl, | ||
@JsonProperty("created_at") LocalDateTime createdAt, | ||
@JsonProperty("updated_at") LocalDateTime updatedAt, | ||
@JsonProperty("pushed_at") LocalDateTime pushedAt, | ||
@JsonProperty("git_url") String gitUrl, | ||
@JsonProperty("ssh_url") String sshUrl, | ||
@JsonProperty("clone_url") String cloneUrl, | ||
@JsonProperty("size") long size, | ||
@JsonProperty("stargazers_count") long starsCount, | ||
@JsonProperty("watchers_count") long watchersCount, | ||
@JsonProperty("forks_count") long forksCount, | ||
@JsonProperty("language") String language) { | ||
this.id = id; | ||
this.name = name; | ||
this.fullName = fullName; | ||
this.isPrivate = isPrivate; | ||
this.owner = owner; | ||
this.repositoryUrl = repositoryUrl; | ||
this.createdAt = createdAt; | ||
this.updatedAt = updatedAt; | ||
this.pushedAt = pushedAt; | ||
this.gitUrl = gitUrl; | ||
this.sshUrl = sshUrl; | ||
this.cloneUrl = cloneUrl; | ||
this.size = size; | ||
this.starsCount = starsCount; | ||
this.watchersCount = watchersCount; | ||
this.forksCount = forksCount; | ||
this.language = language; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getFullName() { | ||
return fullName; | ||
} | ||
|
||
public boolean isPrivate() { | ||
return isPrivate; | ||
} | ||
|
||
public GithubUser getOwner() { | ||
return owner; | ||
} | ||
|
||
public String getRepositoryUrl() { | ||
return repositoryUrl; | ||
} | ||
|
||
public LocalDateTime getCreatedAt() { | ||
return createdAt; | ||
} | ||
|
||
public LocalDateTime getUpdatedAt() { | ||
return updatedAt; | ||
} | ||
|
||
public LocalDateTime getPushedAt() { | ||
return pushedAt; | ||
} | ||
|
||
public String getGitUrl() { | ||
return gitUrl; | ||
} | ||
|
||
public String getSshUrl() { | ||
return sshUrl; | ||
} | ||
|
||
public String getCloneUrl() { | ||
return cloneUrl; | ||
} | ||
|
||
public long getSize() { | ||
return size; | ||
} | ||
|
||
public long getStarsCount() { | ||
return starsCount; | ||
} | ||
|
||
public long getWatchersCount() { | ||
return watchersCount; | ||
} | ||
|
||
public long getForksCount() { | ||
return forksCount; | ||
} | ||
|
||
public String getLanguage() { | ||
return language; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package net.tuzigiri.domain.github; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class GithubUser { | ||
private final String userId; | ||
private final long id; | ||
private final String avatarUrl; | ||
private final String profileUrl; | ||
|
||
@JsonCreator | ||
public GithubUser( | ||
@JsonProperty("login") String userId, | ||
@JsonProperty("id") long id, | ||
@JsonProperty("avatar_url") String avatarUrl, | ||
@JsonProperty("html_url") String profileUrl) { | ||
this.userId = userId; | ||
this.id = id; | ||
this.avatarUrl = avatarUrl; | ||
this.profileUrl = profileUrl; | ||
} | ||
|
||
public String getUserId() { | ||
return userId; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public String getAvatarUrl() { | ||
return avatarUrl; | ||
} | ||
|
||
public String getProfileUrl() { | ||
return profileUrl; | ||
} | ||
} |
Oops, something went wrong.