Skip to content

Commit

Permalink
feat: commercial license: providable api key
Browse files Browse the repository at this point in the history
  • Loading branch information
DadiBit committed Jun 9, 2023
1 parent 963f160 commit abfe7d0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/src/main/kotlin/com/openmeteo/api/AirQuality.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ object AirQuality : Endpoint(
override val pastDays: Int? = null,
override val timezone: Timezone? = null,
val domains: String? = null,
) : Q.Coordinate, Q.Hourly, Q.TimeFormat, Q.DateRange, Q.PastDays, Q.Timezone
override val apikey: String?,
) : Q.Coordinate, Q.Hourly, Q.TimeFormat, Q.DateRange, Q.PastDays, Q.Timezone,
Q.CommercialLicense

@Serializable
open class Response(
Expand Down
4 changes: 3 additions & 1 deletion lib/src/main/kotlin/com/openmeteo/api/Forecast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ object Forecast : Endpoint(
override val elevation: Float? = null,
override val models: String? = null,
override val cellSelection: CellSelection? = null,
override val apikey: String?,
) : Q.Coordinate, Q.Elevation, Q.Daily, Q.Hourly, Q.TimeFormat, Q.DateRange,
Q.PastDays, Q.ForecastDays, Q.CurrentWeather, Q.Timezone, Q.Models,
Q.TemperatureUnit, Q.WindSpeedUnit, Q.PrecipitationUnit, Q.CellSelection
Q.TemperatureUnit, Q.WindSpeedUnit, Q.PrecipitationUnit, Q.CellSelection,
Q.CommercialLicense

@Serializable
open class Response(
Expand Down
4 changes: 3 additions & 1 deletion lib/src/main/kotlin/com/openmeteo/api/Historical.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ object Historical : Endpoint(
override val elevation: Float? = null,
override val models: String? = null,
override val cellSelection: CellSelection? = null,
override val apikey: String?,
) : Q.Coordinate, Q.Elevation, Q.DateRange, Q.Daily, Q.Hourly, Q.TimeFormat, Q.Timezone,
Q.TemperatureUnit, Q.WindSpeedUnit, Q.PrecipitationUnit, Q.Models, Q.CellSelection
Q.TemperatureUnit, Q.WindSpeedUnit, Q.PrecipitationUnit, Q.Models, Q.CellSelection,
Q.CommercialLicense

@Serializable
open class Response(
Expand Down
3 changes: 2 additions & 1 deletion lib/src/main/kotlin/com/openmeteo/api/Marine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ object Marine : Endpoint(
override val cellSelection: CellSelection? = null,
@SerialName("length_unit")
override val lengthUnit: LengthUnit? = null,
override val apikey: String? = null,
) : Q.Coordinate, Q.Daily, Q.Hourly, Q.TimeFormat, Q.Timezone, Q.PastDays,
Q.DateRange, Q.CellSelection, Q.LengthUnit
Q.DateRange, Q.CellSelection, Q.LengthUnit, Q.CommercialLicense

@Serializable
open class Response(
Expand Down
11 changes: 8 additions & 3 deletions lib/src/main/kotlin/com/openmeteo/api/common/http/Endpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ open class Endpoint(
* (from the specified query format or, if undefined, from json)
*/
inline fun <reified R : Response, reified Q : Query> query(query: Q) =
runCatching { Query.toURL(query, context) }
Query.toURL(query, context)
.runCatching {
// set the "customer-" prefix to the host(name) if an api key was provided
val prefix = if (query is Query.CommercialLicense && query.apikey != null)
"customer-" else ""
URL(protocol, prefix + host, port, file /* includes query */)
}
.mapCatching { get(it) }
.mapCatching {
if (query is Query.ContentFormat && query.format == ContentFormat.ProtoBuf)
protoBuf<R>(it)
else json<R>(it)
protoBuf<R>(it) else json<R>(it)
}
}
13 changes: 13 additions & 0 deletions lib/src/main/kotlin/com/openmeteo/api/common/query/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,17 @@ interface Query {
val cellSelection: com.openmeteo.api.common.CellSelection?
}

/**
* Query that may be retrieved with a commercial license (api key)
*/
interface CommercialLicense : Query {
/**
* The api key (commercial usage)
* When commerical usage use `customer-` prefix on all domains:
* - `https://customer-archive-api.open-meteo.com/v1/archive`
* - `https://customer-marine-api.open-meteo.com/v1/marine`
*/
val apikey: String?
}

}

0 comments on commit abfe7d0

Please sign in to comment.