Skip to content

Commit

Permalink
Merge pull request #18 from orekyuu/fetch-github-repository
Browse files Browse the repository at this point in the history
GitHubからリポジトリの情報を取得できるようにした
  • Loading branch information
orekyuu committed Sep 28, 2018
2 parents 12ec963 + a48d48c commit 8abd94f
Show file tree
Hide file tree
Showing 5 changed files with 561 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/java/net/tuzigiri/TuzigiriApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class TuzigiriApplication {

public static void main(String[] args) {
SpringApplication.run(TuzigiriApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(TuzigiriApplication.class, args);
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
43 changes: 43 additions & 0 deletions src/main/java/net/tuzigiri/domain/github/GithubApi.java
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 src/main/java/net/tuzigiri/domain/github/GithubRepository.java
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;
}
}
39 changes: 39 additions & 0 deletions src/main/java/net/tuzigiri/domain/github/GithubUser.java
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;
}
}
Loading

0 comments on commit 8abd94f

Please sign in to comment.