Skip to content

Commit

Permalink
Merge pull request #117 from grida-diary/main
Browse files Browse the repository at this point in the history
docs
  • Loading branch information
wwan13 authored Aug 18, 2024
2 parents ef15f42 + 6b2b6fb commit bcaa317
Show file tree
Hide file tree
Showing 92 changed files with 732 additions and 536 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 8 additions & 0 deletions grida-clients/kakao-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dependencies {
// feign client
implementation("org.springframework.cloud:spring-cloud-starter-openfeign:3.1.8")
implementation("io.github.openfeign:feign-jackson:12.1")

// jackson
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.grida.auth

import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestParam

@FeignClient(
name = "KakaoAuth",
url = "https://kauth.kakao.com/oauth",
)
interface KakaoAuthApi {

@PostMapping("/token", headers = ["Content-type=application/x-www-form-urlencoded;charset=utf-8"])
fun provideToken(
@RequestParam("grant_type") grantType: String,
@RequestParam("client_id") clientId: String,
@RequestParam("redirect_uri") redirectUri: String,
@RequestParam("code") code: String
): KakaoAuthResponse

@PostMapping("/token", headers = ["Content-type=application/x-www-form-urlencoded;charset=utf-8"])
fun refreshToken(
@RequestParam("grant_type") grantType: String,
@RequestParam("client_id") clientId: String,
@RequestParam("refresh_token") refreshToken: String
): KakaoAuthResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.grida.auth

import feign.FeignException
import org.grida.config.KakaoProperties
import org.springframework.stereotype.Component

@Component
class KakaoAuthClient(
private val kakaoAuthApi: KakaoAuthApi,
private val kakaoProperties: KakaoProperties
) {

fun provideAuthToken(code: String): KakaoAuthToken {
try {
val response = kakaoAuthApi.provideToken(
grantType = "authorization_code",
clientId = kakaoProperties.appKey,
redirectUri = kakaoProperties.redirectUri,
code = code
)
return response.toKakaoAuthToken()
} catch (e: FeignException) {
throw IllegalArgumentException(e.stackTraceToString())
}
}

fun refreshToken(refreshToken: String): KakaoAuthToken {
try {
val response = kakaoAuthApi.refreshToken(
grantType = "refresh_token",
clientId = kakaoProperties.appKey,
refreshToken = refreshToken
)
return response.toKakaoAuthToken()
} catch (e: FeignException) {
throw IllegalArgumentException(e.stackTraceToString())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.grida.auth

import com.fasterxml.jackson.annotation.JsonProperty

data class KakaoAuthResponse(
@JsonProperty("token_type")
val tokenType: String,
@JsonProperty("access_token")
val accessToken: String,
@JsonProperty("expires_in")
val expiresId: Int,
@JsonProperty("refresh_token")
val refreshToken: String?,
@JsonProperty("refresh_token_expires_in")
val refreshTokenExpiredIn: Int?
) {

fun toKakaoAuthToken(): KakaoAuthToken {
return KakaoAuthToken(accessToken, refreshToken ?: "")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.grida.auth

data class KakaoAuthToken(
val accessToken: String,
val refreshToken: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.grida.config

import org.springframework.boot.context.properties.ConfigurationPropertiesScan
import org.springframework.cloud.openfeign.EnableFeignClients
import org.springframework.context.annotation.Configuration

@Configuration
@ConfigurationPropertiesScan
@EnableFeignClients(
basePackages = [
"org.grida.auth",
"org.grida.user"
]
)
class KakaoClientConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.grida.config

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding

@ConstructorBinding
@ConfigurationProperties("kakao")
data class KakaoProperties(
val appKey: String,
val redirectUri: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.grida.user

import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestParam

@FeignClient(
name = "KakaoUser",
url = "https://kapi.kakao.com/v2/user",
)
interface KakaoUserApi {

@GetMapping("/me", headers = ["Content-type=application/x-www-form-urlencoded;charset=utf-8"])
fun readUserProfile(
@RequestHeader("Authorization") bearerToken: String,
@RequestParam("property_keys") propertyKeys: String,
@RequestParam("secure_resource") secureResource: Boolean
): KakaoUserProfileResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.grida.user

import org.springframework.stereotype.Component

@Component
class KakaoUserClient(
private val kakaoUserApi: KakaoUserApi
) {

fun readUserProfile(accessToken: String): KakaoUserProfile {
val response = kakaoUserApi.readUserProfile(
bearerToken = "Bearer $accessToken",
propertyKeys = QUERY_PROPERTY_KEYS,
secureResource = true
)

return response.toKakaoUserProfile()
}

companion object {
const val QUERY_PROPERTY_KEYS = "[\"kakao_account.profile\"]"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.grida.user

data class KakaoUserProfile(
val id: String,
val name: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.grida.user

import com.fasterxml.jackson.annotation.JsonProperty

data class KakaoUserProfileResponse(
val id: String,
@JsonProperty("kakao_account")
val properties: KakaoAccount
) {
fun toKakaoUserProfile(): KakaoUserProfile {
return KakaoUserProfile(
id = id,
name = properties.profile.nickname,
)
}
}

data class KakaoAccount(
val profile: KakaoAccountProfile
)

data class KakaoAccountProfile(
val nickname: String,
)
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.grida.chat

import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestHeader

@FeignClient(
name = "OpenAiChatCompletion",
url = "https://api.openai.com/v1/chat/completions",
)
interface OpenAiChatApi {

@PostMapping(headers = ["Content-Type=application/json"])
fun chat(
@RequestHeader("Authorization") secretKey: String,
@RequestBody request: OpenAiChatRequest
): OpenAiChatResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.grida.chat

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.stereotype.Component

@Component
class OpenAiChatClient(
private val openAiChatApi: OpenAiChatApi,
private val openAiSecretKey: String,
private val objectMapper: ObjectMapper
) {

fun <T> chat(
prompt: String,
valueType: Class<T>
): T {
val bearerToken = "Bearer $openAiSecretKey"
val request = OpenAiChatRequest(
model = "gpt-4o",
role = "system",
prompt = prompt,
responseFormat = "json_object"
)

val response = openAiChatApi.chat(bearerToken, request)
val result = response.result

return objectMapper.readValue(result, valueType)
}
}
Loading

0 comments on commit bcaa317

Please sign in to comment.