Skip to content

Commit

Permalink
refactor: merged Http interface into Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DadiBit committed Jun 6, 2023
1 parent 2eb8fca commit c0c6052
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 82 deletions.
16 changes: 14 additions & 2 deletions lib/src/main/kotlin/com/openmeteo/api/common/http/Endpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import javax.net.ssl.HttpsURLConnection
open class Endpoint(
@Transient
val context: URL,
) : Http<InputStream> {
) {

companion object {
@OptIn(ExperimentalSerializationApi::class)
Expand Down Expand Up @@ -48,13 +48,19 @@ open class Endpoint(
inline fun <reified T> protoBuf(inputStream: InputStream) =
ProtoBuf.decodeFromByteArray<T>(inputStream.readAllBytes())

/**
* Open a new HTTPS connection
*/
private fun connect(url: URL) =
url.openConnection() as HttpsURLConnection

/**
* Handle responses based on their status code:
* - 200: return the inputStream
* - 400: parse JSON error as [BadRequest]
* - else: return a generic error with the url and the response code/message
*/
override fun response(connection: HttpsURLConnection): InputStream =
private fun response(connection: HttpsURLConnection): InputStream =
with(connection) {
when (responseCode) {
200 -> inputStream
Expand All @@ -63,6 +69,12 @@ open class Endpoint(
}
}

/**
* GET some data from the url
*/
fun get(url: URL) =
response(connect(url))

/**
* GET, with a [Query], the endpoint context url and parse the response data
* (from the specified query format or, if undefined, from json)
Expand Down
28 changes: 0 additions & 28 deletions lib/src/main/kotlin/com/openmeteo/api/common/http/Http.kt

This file was deleted.

52 changes: 0 additions & 52 deletions lib/src/test/kotlin/com/openmeteo/api/common/http/HttpTest.kt

This file was deleted.

0 comments on commit c0c6052

Please sign in to comment.