-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
69 additions
and
40 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#Mon Oct 19 09:13:53 CEST 2020 | ||
#Thu Oct 22 22:37:52 CEST 2020 | ||
gradle.version=6.3 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package nl.lengrand.swacli | ||
|
||
import io.ktor.client.* | ||
import io.ktor.client.engine.apache.* | ||
import io.ktor.client.features.json.* | ||
import io.ktor.client.features.json.serializer.* | ||
import kotlinx.serialization.json.Json | ||
|
||
object Configuration { | ||
|
||
private val jsonSerializer = Json { ignoreUnknownKeys = true} | ||
|
||
fun getHttpClient() = HttpClient(Apache) { | ||
install(JsonFeature) { | ||
serializer = KotlinxSerializer(jsonSerializer) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,31 @@ | ||
package nl.lengrand.swacli | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.registerKotlinModule | ||
import com.github.kittinunf.fuel.Fuel | ||
import com.github.kittinunf.fuel.jackson.responseObject | ||
|
||
val mapper: ObjectMapper = ObjectMapper().registerKotlinModule().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import kotlinx.serialization.Serializable | ||
|
||
const val BASE_URL = "https://swapi.dev/api" | ||
|
||
class SwApi { | ||
companion object{ | ||
fun getPeople(query : String?) : Response<People> { | ||
return Fuel.get("$BASE_URL/people/${queryString(query)}") | ||
.header("accept", "application/json") | ||
.responseObject<Response<People>>(mapper).third.get() | ||
} | ||
object SwApi { | ||
|
||
private val httpClient = Configuration.getHttpClient() | ||
|
||
fun getPlanets(query : String?) : Response<Planet> { | ||
return Fuel.get("$BASE_URL/planets/${queryString(query)}") | ||
.header("accept", "application/json") | ||
.responseObject<Response<Planet>>(mapper).third.get() | ||
suspend fun getPeople(query : String?) : Response<People> { | ||
return httpClient.get("$BASE_URL/people/${queryString(query)}") { | ||
header("Content-Type", ContentType.Application.Json.toString()) | ||
} | ||
} | ||
|
||
private fun queryString(query: String?) = if(query == null) "" else "?search=${query}" | ||
suspend fun getPlanets(query : String?) : Response<Planet> { | ||
return httpClient.get("$BASE_URL/planets/${queryString(query)}") { | ||
header("Content-Type", ContentType.Application.Json.toString()) | ||
} | ||
} | ||
|
||
private fun queryString(query: String?) = if(query == null) "" else "?search=${query}" | ||
} | ||
|
||
sealed class Data | ||
data class Response<Data>(val count: Int, val next : String?, val previous : String?, val results : List<Data>) | ||
data class Planet(val climate: String, val name: String, val gravity: String, val orbital_period: String, val diameter: String) : Data() | ||
data class People(val name: String, val height: String, val mass: String, val hair_color: String, val homeworld: String, val birth_year: String) : Data() | ||
@Serializable sealed class Data | ||
@Serializable data class Response<Data>(val count: Int, val next : String?, val previous : String?, val results : List<Data>) | ||
@Serializable data class Planet(val climate: String, val name: String, val gravity: String, val orbital_period: String, val diameter: String) : Data() | ||
@Serializable data class People(val name: String, val height: String, val mass: String, val hair_color: String, val homeworld: String, val birth_year: String) : Data() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters