Skip to content

Instantly share code, notes, and snippets.

@AhmedMourad0
Last active July 16, 2023 22:02
Show Gist options
  • Save AhmedMourad0/5b2a88af3eaaf819a0dd28bcf6effc4b to your computer and use it in GitHub Desktop.
Save AhmedMourad0/5b2a88af3eaaf819a0dd28bcf6effc4b to your computer and use it in GitHub Desktop.
ktor
import com.dequeue.app.common.log
import com.dequeue.app.common.logging.D
import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.plugins.resources.*
import io.ktor.serialization.kotlinx.json.*
fun ktorHttpClient(baseUrl: String): HttpClient = ktorClient {
defaultRequest {
url(baseUrl)
}
install(ContentNegotiation) {
json()
}
install(Resources)
}
fun ktorClient(
maxRetries: Int = 5,
requestTimeoutMillis: Long = 10_000L,
block: HttpClientConfig<*>.() -> Unit = { }
): HttpClient = HttpClient {
expectSuccess = true
install(HttpTimeout) {
this.requestTimeoutMillis = requestTimeoutMillis
}
install(HttpRequestRetry) {
retryOnServerErrors(maxRetries = maxRetries)
exponentialDelay()
}
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
log(D, message)
}
}
level = LogLevel.ALL
}
block.invoke(this)
}
prayersKtorClient.get(PrayersRequest(
latitude = coordinates.latitude,
longitude = coordinates.longitude,
year = date.year,
month = date.monthNumber
)).body<PrayersResponse>().data
import com.dequeue.app.common.ktor.ktorHttpClient
import io.ktor.resources.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
val prayersKtorClient = ktorHttpClient("http://api.aladhan.com/v1/")
@Resource("/calendar")
class PrayersRequest(
val latitude: Double,
val longitude: Double,
val year: Int,
val month: Int,
val iso8601: Boolean = true
)
@Serializable
data class PrayersResponse(
@SerialName("data") val data: List<PrayersDataItem>
)
@Serializable
data class PrayersDataItem(
@SerialName("timings") val timings: PrayersTimings
)
@Serializable
data class PrayersTimings(
@SerialName("Fajr") val fajr: String,
@SerialName("Dhuhr") val dhuhr: String,
@SerialName("Asr") val asr: String,
@SerialName("Maghrib") val maghrib: String,
@SerialName("Isha") val isha: String,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment