Skip to content

Commit

Permalink
feat: Elevation api
Browse files Browse the repository at this point in the history
  • Loading branch information
DadiBit committed Jun 9, 2023
1 parent 8408acb commit 197b3ad
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/src/main/kotlin/com/openmeteo/api/Elevation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.openmeteo.api

import com.openmeteo.api.common.Coordinate
import com.openmeteo.api.common.http.Endpoint
import kotlinx.serialization.Serializable
import java.net.URL
import com.openmeteo.api.common.Response as R
import com.openmeteo.api.common.query.Query as Q

object Elevation : Endpoint(
URL("https://api.open-meteo.com/v1/elevation")
) {

operator fun invoke(query: Query, context: URL = this.context) =
query<Response, Query>(query, context)

@Serializable
open class Query private constructor(
val latitude: String,
val longitude: String,
override val apikey: String? = null,
) : Q.CommercialLicense {
constructor(latitude: Float, longitude: Float) : this(
latitude.toString(),
longitude.toString()
)
constructor(latitudes: List<Float>, longitudes: List<Float>) : this(
latitudes.joinToString(","),
longitudes.joinToString(","),
)
constructor(vararg coordinates: Coordinate) : this(
coordinates.map { it.latitude },
coordinates.map { it.longitude },
)
constructor(vararg coordinates: Pair<Float, Float>) : this(
coordinates.map { it.first },
coordinates.map { it.second },
)

}

@Serializable
open class Response(
val elevation: FloatArray,
) : R

}
34 changes: 34 additions & 0 deletions lib/src/test/kotlin/com/openmeteo/api/ElevationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.openmeteo.api

import kotlin.test.Test
import kotlin.test.assertContentEquals

class ElevationTest {

@Test
fun `Two coordinates (pairs of float-float)`() {
val query = Elevation.Query(
0f to 0f,
52.52f to 13.41f,
)
Elevation(query).getOrThrow().run {
assertContentEquals(floatArrayOf(
0f, 38f
), elevation)
}
}

@Test
fun `Two coordinates (lists of floats`() {
val query = Elevation.Query(
latitudes = listOf(0f, 52.52f),
longitudes = listOf(0f, 13.41f),
)
Elevation(query).getOrThrow().run {
assertContentEquals(floatArrayOf(
0f, 38f
), elevation)
}
}

}

0 comments on commit 197b3ad

Please sign in to comment.