Skip to content

Commit

Permalink
♻️ : extract AbstractRegistryApi
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Jan 17, 2020
1 parent ffa7b81 commit 4980b02
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 136 deletions.
63 changes: 63 additions & 0 deletions src/main/java/io/codeka/gaia/registries/AbstractRegistryApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.codeka.gaia.registries

import io.codeka.gaia.teams.User
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.web.client.RestTemplate
import java.util.*

abstract class AbstractRegistryApi<K>(val restTemplate: RestTemplate,
private val registryType: RegistryType,
private val registryFileClass: Class<K>,
private val registryListFileClass: Class<Array<K>>): RegistryApi<K> {

private fun <T> callWithAuth(url: String, token: String, responseType: Class<T>): T {
val headers = HttpHeaders()
headers.add("Authorization", "Bearer $token")

val requestEntity = HttpEntity<Any>(headers)

val response = restTemplate.exchange(
url,
HttpMethod.GET,
requestEntity,
responseType)
if(response.statusCode == HttpStatus.OK) {
return response.body
}
else {
TODO("error code mgmt")
}
}

override fun getRepositories(user: User): List<K> {
// fetching repositories
val url = registryType.repositoriesUrl

val token = user.oAuth2User?.token!!

return callWithAuth(url, token, registryListFileClass).toList()
}

override fun getRepository(user: User, projectId: String): K {
// fetching repositories
val url = registryType.repositoryUrl.format(projectId)

val token = user.oAuth2User?.token!!

return callWithAuth(url, token, registryFileClass)
}

override fun getFileContent(user: User, projectId: String, filename: String): String {
val url = registryType.fileContentUrl.format(projectId, filename)

val token = user.oAuth2User?.token!!

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

// removing trailing \n
return String(Base64.getDecoder().decode(file.content.replace("\n", "")))
}
}
2 changes: 0 additions & 2 deletions src/main/java/io/codeka/gaia/registries/RegistryApi.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.codeka.gaia.registries

import io.codeka.gaia.registries.github.GithubRepository
import io.codeka.gaia.teams.User
import org.springframework.web.client.RestTemplate

interface RegistryApi<T> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,79 +1,18 @@
package io.codeka.gaia.registries.github

import com.fasterxml.jackson.annotation.JsonAlias
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.databind.annotation.JsonNaming
import io.codeka.gaia.registries.RegistryApi
import io.codeka.gaia.registries.RegistryFile
import io.codeka.gaia.registries.AbstractRegistryApi
import io.codeka.gaia.registries.RegistryType
import io.codeka.gaia.registries.gitlab.GitlabRepository
import io.codeka.gaia.teams.User
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate
import java.util.*

@Service
class GithubRegistryApi(val restTemplate: RestTemplate): RegistryApi<GithubRepository> {

fun <T> callWithAuth(url: String, token: String, responseType: Class<T>): T{
val headers = HttpHeaders()
if(token != null) {
headers.add("Authorization", "Bearer $token")
}

val requestEntity = HttpEntity<Any>(headers)

val response = restTemplate.exchange(
url,
HttpMethod.GET,
requestEntity,
responseType)
if(response.statusCode == HttpStatus.OK) {
return response.body
}
else {
TODO("error code mgmt")
}
}

override fun getRepositories(user: User): List<GithubRepository> {
// fetching repositories
val url = RegistryType.GITHUB.repositoriesUrl

val token = user.oAuth2User?.token!!

val repos = callWithAuth(url, token, Array<GithubRepository>::class.java)

return repos.toList()
}

override fun getRepository(user: User, projectId: String): GithubRepository {
// fetching repositories
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 = RegistryType.GITHUB.fileContentUrl.format(projectId, filename)

val token = user.oAuth2User?.token!!

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

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

}
class GithubRegistryApi(restTemplate: RestTemplate): AbstractRegistryApi<GithubRepository>(
restTemplate,
RegistryType.GITHUB,
GithubRepository::class.java,
Array<GithubRepository>::class.java)

data class GithubRepository(
@JsonAlias("full_name") val fullName: String,
@JsonAlias("html_url") val htmlUrl: String)
@JsonAlias("html_url") val htmlUrl: String)
Original file line number Diff line number Diff line change
@@ -1,77 +1,17 @@
package io.codeka.gaia.registries.gitlab

import com.fasterxml.jackson.annotation.JsonAlias
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.databind.annotation.JsonNaming
import io.codeka.gaia.registries.RegistryApi
import io.codeka.gaia.registries.RegistryFile
import io.codeka.gaia.registries.AbstractRegistryApi
import io.codeka.gaia.registries.RegistryType
import io.codeka.gaia.teams.User
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate
import java.util.*

@Service
class GitlabRegistryApi(val restTemplate: RestTemplate): RegistryApi<GitlabRepository> {

fun <T> callWithAuth(url: String, token: String, responseType: Class<T>): T{
val headers = HttpHeaders()
if(token != null) {
headers.add("Authorization", "Bearer $token")
}

val requestEntity = HttpEntity<Any>(headers)

val response = restTemplate.exchange(
url,
HttpMethod.GET,
requestEntity,
responseType)
if(response.statusCode == HttpStatus.OK) {
return response.body
}
else {
TODO("error code mgmt")
}
}

override fun getRepositories(user: User): List<GitlabRepository> {
// fetching repositories
val url = RegistryType.GITLAB.repositoriesUrl

val token = user.oAuth2User?.token!!

val repos = callWithAuth(url, token, Array<GitlabRepository>::class.java)

return repos.toList()
}

override fun getRepository(user: User, projectId: String): GitlabRepository {
// fetching repositories
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 = RegistryType.GITLAB.fileContentUrl.format(projectId, filename)

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

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

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

}
class GitlabRegistryApi(restTemplate: RestTemplate): AbstractRegistryApi<GitlabRepository>(
restTemplate,
RegistryType.GITLAB,
GitlabRepository::class.java,
Array<GitlabRepository>::class.java)

data class GitlabRepository(
@JsonAlias("id") val id: String,
Expand Down

0 comments on commit 4980b02

Please sign in to comment.