Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHubからリポジトリの情報を取得できるようにした #18

Merged
merged 1 commit into from
Sep 28, 2018
Merged
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
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 {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GithubRepositoryを取得するのはGithubRepositoryRepository?やばいな。と一瞬考えたがこれが一番わかり易いと思った。いい名前案絶賛募集中です

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