Skip to content

Commit

Permalink
♻️ : extract API URLs in RegistryType
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Jan 17, 2020
1 parent 4b7736f commit ffa7b81
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
18 changes: 15 additions & 3 deletions src/main/java/io/codeka/gaia/registries/bo.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package io.codeka.gaia.registries

enum class RegistryType(val readmeUrl: String){
GITHUB("https://api.github.com/repos/{id}/contents/README.md?ref=master"),
GITLAB("https://gitlab.com/api/v4/projects/{id}/repository/files/README.md?ref=master")
enum class RegistryType(val repositoriesUrl: String,
val repositoryUrl: String,
val fileContentUrl: String,
val readmeUrl: String){

GITHUB("https://api.github.com/user/repos?visibility=public",
"https://api.github.com/repos/%s",
"https://api.github.com/repos/%s/contents/%s?ref=master",
"https://api.github.com/repos/{id}/contents/README.md?ref=master"),

GITLAB("https://gitlab.com/api/v4/projects?visibility=public&owned=true",
"https://gitlab.com/api/v4/projects/%s",
"https://gitlab.com/api/v4/projects/%s/repository/files/%s?ref=master",
"https://gitlab.com/api/v4/projects/{id}/repository/files/README.md?ref=master")

}

data class RegistryDetails(val registryType: RegistryType, val projectId: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GithubRegistryApi(val restTemplate: RestTemplate): RegistryApi<GithubRepos

override fun getRepositories(user: User): List<GithubRepository> {
// fetching repositories
val url = "https://api.github.com/user/repos?visibility=public"
val url = RegistryType.GITHUB.repositoriesUrl

val token = user.oAuth2User?.token!!

Expand All @@ -54,22 +54,21 @@ class GithubRegistryApi(val restTemplate: RestTemplate): RegistryApi<GithubRepos

override fun getRepository(user: User, projectId: String): GithubRepository {
// fetching repositories
val url = "https://api.github.com/repos/$projectId"
val url = RegistryType.GITHUB.repositoryUrl.format(projectId)

val token = user.oAuth2User?.token!!

return callWithAuth(url, token, GithubRepository::class.java)
}

override fun getFileContent(user: User, projectId: String, filename: String): String {
val url = "https://api.github.com/repos/$projectId/contents/$filename?ref=master";
val url = RegistryType.GITHUB.fileContentUrl.format(projectId, filename)

val token = user.oAuth2User?.token!!;
val token = user.oAuth2User?.token!!

val file = callWithAuth(url, token, RegistryFile::class.java)

// removing trailing \n
println(file.content.replace("\n", ""))
return String(Base64.getDecoder().decode(file.content.replace("\n", "")))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class GitlabRegistryApi(val restTemplate: RestTemplate): RegistryApi<GitlabRepos

override fun getRepositories(user: User): List<GitlabRepository> {
// fetching repositories
val url = "https://gitlab.com/api/v4/projects?visibility=public&owned=true"
val url = RegistryType.GITLAB.repositoriesUrl

val token = user.oAuth2User?.token!!

Expand All @@ -53,22 +53,21 @@ class GitlabRegistryApi(val restTemplate: RestTemplate): RegistryApi<GitlabRepos

override fun getRepository(user: User, projectId: String): GitlabRepository {
// fetching repositories
val url = "https://gitlab.com/api/v4/projects/$projectId"
val url = RegistryType.GITLAB.repositoryUrl.format(projectId)

val token = user.oAuth2User?.token!!

return callWithAuth(url, token, GitlabRepository::class.java)
}

override fun getFileContent(user: User, projectId: String, filename: String): String {
val url = "https://gitlab.com/api/v4/projects/$projectId/repository/files/$filename?ref=master";
val url = RegistryType.GITLAB.fileContentUrl.format(projectId, filename)

val token = user.oAuth2User?.token!!;

val file = callWithAuth(url, token, RegistryFile::class.java)

// removing trailing \n
println(file.content.replace("\n", ""))
return String(Base64.getDecoder().decode(file.content.replace("\n", "")))
}

Expand Down

0 comments on commit ffa7b81

Please sign in to comment.