Skip to content

Commit

Permalink
Added Currency converter API
Browse files Browse the repository at this point in the history
  • Loading branch information
myofficework000 committed Mar 23, 2024
1 parent 5d20b01 commit 363804b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.example.jetpack_compose_all_in_one.demos.currency_converter.data.dto

/**
* Represents a response containing currency data.
*
* @property date The date of the response.
* @property jpy The Japanese Yen value in the response.
*/
data class CurrencyResponse(
val date: String,
val jpy: Double
)
val date: String, // Date of the response
val jpy: Double // Japanese Yen value in the response
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ package com.example.jetpack_compose_all_in_one.demos.currency_converter.data.rep

import com.example.jetpack_compose_all_in_one.demos.currency_converter.data.dto.CurrencyResponse

/**
* Interface representing a currency repository.
* Implementations of this interface provide methods to fetch currency exchange rates.
*/
interface CurrencyRepository {
/**
* Asynchronously retrieves the exchange rate from Euro (EUR) to Japanese Yen (JPY).
*
* @return [Result] object containing the [CurrencyResponse] if the operation is successful,
* or an error if the operation fails.
*/
suspend fun getEURToJPY(): Result<CurrencyResponse>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,37 @@ import com.example.jetpack_compose_all_in_one.demos.currency_converter.data.api.
import com.example.jetpack_compose_all_in_one.demos.currency_converter.data.dto.CurrencyResponse
import javax.inject.Inject

class CurrencyRepositoryImpl @Inject constructor(private val apiService: CurrencyApiService) : CurrencyRepository {

/**
* Implementation of [CurrencyRepository] interface.
* This class is responsible for fetching currency exchange rates.
*
* @param apiService The API service used to make network calls for currency data.
*/
class CurrencyRepositoryImpl @Inject constructor(private val apiService: CurrencyApiService) :
CurrencyRepository {

/**
* Asynchronously retrieves the exchange rate from Euro (EUR) to Japanese Yen (JPY).
*
* @return [Result] object containing the [CurrencyResponse] if the operation is successful,
* or an error if the operation fails.
*/
override suspend fun getEURToJPY(): Result<CurrencyResponse> {
return try {
// Make the network call to fetch EUR to JPY exchange rate
val response = apiService.getEURToJPY()
// Check if the response is successful and contains data
if (response.isSuccessful && response.body() != null) {
// If successful, return the currency response wrapped in a success Result
Result.success(response.body()!!)
} else {
// If response is not successful or does not contain data, return a failure Result
Result.failure(RuntimeException("Failed to fetch data"))
}
} catch (e: Exception) {
// If an exception occurs during the operation, return a failure Result with the exception
Result.failure(e)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
* Dagger Hilt module for providing network-related dependencies.
* This module provides the implementation of the [CurrencyApiService].
*/
@InstallIn(SingletonComponent::class)
@Module
object NetworkModule {

/**
* Provides an instance of [CurrencyApiService] for making network calls related to currency data.
*
* @return An instance of [CurrencyApiService].
*/
@Provides
fun provideApiService() : CurrencyApiService {
fun provideApiService(): CurrencyApiService {
// Configure and build Retrofit instance
return Retrofit.Builder()
.baseUrl("https://cdn.jsdelivr.net")
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://cdn.jsdelivr.net") // Base URL for the API
.addConverterFactory(GsonConverterFactory.create()) // Gson converter factory for JSON serialization
.build()
.create(CurrencyApiService::class.java)

.create(CurrencyApiService::class.java) // Create an instance of CurrencyApiService interface
}
}
}

0 comments on commit 363804b

Please sign in to comment.