Fix SubscriptionApi compilation errors

Fixed unresolved references by matching the standard API client pattern:
- Changed from apiCall helper to try-catch pattern
- Use ApiClient.httpClient instead of direct httpClient reference
- Use ApiClient.getBaseUrl() instead of ApiConfig.baseUrl
- Added proper HttpClient parameter with default value
- Added proper error handling with response status checks
- Fixed verifyIOSReceipt to include transactionId parameter
- Fixed Android verification request format (use map instead of data class)

Now matches the pattern used by other API classes (ResidenceApi, TaskApi, etc.)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Trey t
2025-11-24 13:55:46 -06:00
parent 90c3858c90
commit 0946842737

View File

@@ -1,50 +1,98 @@
package com.example.mycrib.network
import com.example.mycrib.models.*
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
class SubscriptionApi {
private val baseUrl = ApiConfig.baseUrl
class SubscriptionApi(private val client: HttpClient = ApiClient.httpClient) {
private val baseUrl = ApiClient.getBaseUrl()
suspend fun getSubscriptionStatus(token: String): ApiResult<SubscriptionStatus> {
return apiCall {
httpClient.get("$baseUrl/subscription/status/") {
return try {
val response = client.get("$baseUrl/subscription/status/") {
header("Authorization", "Token $token")
}.body()
}
if (response.status.isSuccess()) {
ApiResult.Success(response.body())
} else {
ApiResult.Error("Failed to fetch subscription status", response.status.value)
}
} catch (e: Exception) {
ApiResult.Error(e.message ?: "Unknown error occurred")
}
}
suspend fun getUpgradeTriggers(token: String): ApiResult<Map<String, UpgradeTriggerData>> {
return apiCall {
httpClient.get("$baseUrl/subscription/upgrade-triggers/") {
return try {
val response = client.get("$baseUrl/subscription/upgrade-triggers/") {
header("Authorization", "Token $token")
}.body()
}
if (response.status.isSuccess()) {
ApiResult.Success(response.body())
} else {
ApiResult.Error("Failed to fetch upgrade triggers", response.status.value)
}
} catch (e: Exception) {
ApiResult.Error(e.message ?: "Unknown error occurred")
}
}
suspend fun getFeatureBenefits(): ApiResult<List<FeatureBenefit>> {
return apiCall {
httpClient.get("$baseUrl/subscription/feature-benefits/").body()
return try {
val response = client.get("$baseUrl/subscription/feature-benefits/")
if (response.status.isSuccess()) {
ApiResult.Success(response.body())
} else {
ApiResult.Error("Failed to fetch feature benefits", response.status.value)
}
} catch (e: Exception) {
ApiResult.Error(e.message ?: "Unknown error occurred")
}
}
suspend fun getActivePromotions(token: String): ApiResult<List<Promotion>> {
return apiCall {
httpClient.get("$baseUrl/subscription/promotions/") {
return try {
val response = client.get("$baseUrl/subscription/promotions/") {
header("Authorization", "Token $token")
}.body()
}
if (response.status.isSuccess()) {
ApiResult.Success(response.body())
} else {
ApiResult.Error("Failed to fetch promotions", response.status.value)
}
} catch (e: Exception) {
ApiResult.Error(e.message ?: "Unknown error occurred")
}
}
suspend fun verifyIOSReceipt(token: String, receiptData: String): ApiResult<VerificationResponse> {
return apiCall {
httpClient.post("$baseUrl/subscription/verify-ios/") {
suspend fun verifyIOSReceipt(
token: String,
receiptData: String,
transactionId: String
): ApiResult<VerificationResponse> {
return try {
val response = client.post("$baseUrl/subscription/verify-ios/") {
header("Authorization", "Token $token")
contentType(ContentType.Application.Json)
setBody(ReceiptVerificationRequest(receiptData))
}.body()
setBody(mapOf(
"receipt_data" to receiptData,
"transaction_id" to transactionId
))
}
if (response.status.isSuccess()) {
ApiResult.Success(response.body())
} else {
ApiResult.Error("Failed to verify iOS receipt", response.status.value)
}
} catch (e: Exception) {
ApiResult.Error(e.message ?: "Unknown error occurred")
}
}
@@ -53,12 +101,23 @@ class SubscriptionApi {
purchaseToken: String,
productId: String
): ApiResult<VerificationResponse> {
return apiCall {
httpClient.post("$baseUrl/subscription/verify-android/") {
return try {
val response = client.post("$baseUrl/subscription/verify-android/") {
header("Authorization", "Token $token")
contentType(ContentType.Application.Json)
setBody(PurchaseVerificationRequest(purchaseToken, productId))
}.body()
setBody(mapOf(
"purchase_token" to purchaseToken,
"product_id" to productId
))
}
if (response.status.isSuccess()) {
ApiResult.Success(response.body())
} else {
ApiResult.Error("Failed to verify Android purchase", response.status.value)
}
} catch (e: Exception) {
ApiResult.Error(e.message ?: "Unknown error occurred")
}
}
}