Skip to content

Commit

Permalink
change return type in serivces
Browse files Browse the repository at this point in the history
  • Loading branch information
tinpham5614 committed Dec 8, 2023
1 parent 7faa6fd commit b48d2e9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json

interface ProductService {
suspend fun getProduct(productRequest: ProductRequest): ProductRequest?
suspend fun getProduct(productRequest: ProductRequest): ProductResponse?

suspend fun getProducts(productRequest: ProductRequest): List<ProductRequest>?
suspend fun getProducts(): List<ProductResponse>?

suspend fun addProduct(productRequest: ProductRequest): ProductResponse?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,28 @@ class ProductServiceImpl (
private val client: HttpClient,
private val getToken: suspend () -> String
) : ProductService {
override suspend fun getProduct(productRequest: ProductRequest): ProductRequest? {
override suspend fun getProduct(productRequest: ProductRequest): ProductResponse? {
TODO("Not yet implemented")
}

@OptIn(InternalAPI::class)
override suspend fun getProducts(productRequest: ProductRequest): List<ProductRequest>? {
override suspend fun getProducts(): List<ProductResponse>? {
return try {
val token = getToken()
val response = client.get {
client.get {
url (HttpRoutes.PRODUCTS)
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
header(HttpHeaders.Authorization, "Bearer $token")
body = Json.encodeToString(ProductRequest.serializer(), productRequest)
}
response.body()
emptyList()
} catch (e: RedirectResponseException) {
println("Error: ${e.response.status.description}")
null
emptyList()
} catch (e: ClientRequestException) {
println("Error: ${e.response.status.description}")
null
emptyList()
} catch (e: ServerResponseException) {
println("Error: ${e.response.status.description}")
null
emptyList()
} catch (e: Exception) {
println("Error: ${e.message}")
null
emptyList()
}
}

Expand Down

0 comments on commit b48d2e9

Please sign in to comment.