Skip to content

Commit

Permalink
fix: Resolved the API key validation error caused by the unavailabili…
Browse files Browse the repository at this point in the history
…ty of the OpenAI subscription API (/v1/dashboard/billing/subscription).
  • Loading branch information
Yubyf committed Jul 28, 2023
1 parent 68e33b9 commit 7e82ba2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ object OpenAIPrefKeys {
const val PREF_OPENAI_QUOTE_TYPE_DEFAULT = PREF_OPENAI_QUOTE_TYPE_AI_GENERATED
const val PREF_OPENAI_API_HOST_DEFAULT = "https://api.openai.com"
const val PREF_OPENAI_CHAT_API_PATH = "/v1/chat/completions"
const val PREF_OPENAI_CHAT_SUB_PATH = "dashboard/billing/subscription"
const val PREF_OPENAI_CHAT_USAGE_PATH = "dashboard/billing/usage"
const val PREF_OPENAI_MODELS_PATH = "/v1/models"

const val PREF_OPENAI_API_KEY_SUPPORT_LINK = "https://platform.openai.com/account/api-keys"
const val PREF_OPENAI_API_KEY_PREFIX = "sk-"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_A
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_API_HOST_DEFAULT
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_API_KEY
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_CHAT_API_PATH
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_CHAT_SUB_PATH
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_LANGUAGE
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_LANGUAGE_DEFAULT
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_MODEL
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_MODELS_PATH
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_MODEL_DEFAULT
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_QUOTE_SOURCE_DEFAULT
import com.crossbowffs.quotelock.app.configs.openai.OpenAIPrefKeys.PREF_OPENAI_QUOTE_SYSTEM_PROMPTS
Expand All @@ -22,8 +22,8 @@ import com.crossbowffs.quotelock.data.api.OpenAIConfigs
import com.crossbowffs.quotelock.data.modules.openai.chat.OpenAIChatInput
import com.crossbowffs.quotelock.data.modules.openai.chat.OpenAIChatMessage
import com.crossbowffs.quotelock.data.modules.openai.chat.OpenAIChatResponse
import com.crossbowffs.quotelock.data.modules.openai.chat.OpenAIModelsResponse
import com.crossbowffs.quotelock.data.modules.openai.chat.OpenAIQuote
import com.crossbowffs.quotelock.data.modules.openai.chat.OpenAISubscriptionResponse
import com.crossbowffs.quotelock.data.modules.openai.geo.OpenAITraceResponse
import com.crossbowffs.quotelock.data.modules.openai.geo.SUPPORTED_COUNTRY_CODES
import com.crossbowffs.quotelock.data.modules.openai.geo.parseTraceResponse
Expand Down Expand Up @@ -196,7 +196,7 @@ class OpenAIRepository @Inject internal constructor(

suspend fun fetchAccountInfo() {
val apiKey = requireApiKey()
getSubscription(apiKey) ?: throw OpenAIException.ConnectException
getModels(apiKey) ?: throw OpenAIException.ConnectException
}

suspend fun requestQuote(): OpenAIQuote? {
Expand Down Expand Up @@ -233,11 +233,11 @@ class OpenAIRepository @Inject internal constructor(
}

@Throws(IOException::class)
private suspend fun getSubscription(apiKey: String): OpenAISubscriptionResponse? =
private suspend fun getModels(apiKey: String): OpenAIModelsResponse? =
requestOpenAI { host ->
httpClient.fetchJson<OpenAISubscriptionResponse>(
httpClient.fetchJson<OpenAIModelsResponse>(
url = host,
path = PREF_OPENAI_CHAT_SUB_PATH,
path = PREF_OPENAI_MODELS_PATH,
method = HttpMethod.Get,
headers = mapOf(
HttpHeaders.Authorization to "Bearer $apiKey"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.crossbowffs.quotelock.data.modules.openai.chat

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class OpenAIModelsResponse(
@SerialName("object")
val objectX: String,
@SerialName("data")
val dataX: List<OpenAIModelData>,
)

@Serializable
data class OpenAIModelData(
@SerialName("id")
val id: String,
@SerialName("object")
val objectX: String,
@SerialName("created")
val created: Long,
@SerialName("owned_by")
val ownedBy: String,
@SerialName("root")
val root: String,
)

This file was deleted.

This file was deleted.

0 comments on commit 7e82ba2

Please sign in to comment.