Skip to content

Commit aac3da4

Browse files
committed
Merge main
2 parents 8612f02 + 80fdde9 commit aac3da4

20 files changed

+69
-40
lines changed
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.

.gradle/6.3/fileHashes/fileHashes.bin

-1.81 KB
Binary file not shown.
0 Bytes
Binary file not shown.
1 Byte
Binary file not shown.
17 Bytes
Binary file not shown.

.gradle/6.5.1/gc.properties

Whitespace-only changes.
Binary file not shown.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#Mon Oct 19 09:13:53 CEST 2020
1+
#Thu Oct 22 22:37:52 CEST 2020
22
gradle.version=6.3
-504 Bytes
Binary file not shown.

.gradle/checksums/checksums.lock

0 Bytes
Binary file not shown.

.gradle/checksums/md5-checksums.bin

10 KB
Binary file not shown.

.gradle/checksums/sha1-checksums.bin

70.9 KB
Binary file not shown.

build.gradle

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id 'java'
33
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
4+
id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.10'
45
}
56

67
apply plugin: 'kotlin-kapt' // required
@@ -16,13 +17,15 @@ repositories {
1617

1718
dependencies {
1819
implementation "org.jetbrains.kotlin:kotlin-stdlib"
19-
implementation 'com.fasterxml.jackson.core:jackson-core: 2.9.8'
20-
implementation 'com.fasterxml.jackson.core:jackson-databind: 2.9.8'
21-
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8'
2220

2321
implementation 'info.picocli:picocli:4.5.2'
24-
implementation 'com.github.kittinunf.fuel:fuel:2.3.0'
25-
implementation 'com.github.kittinunf.fuel:fuel-jackson:2.3.0'
22+
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0'
23+
implementation 'io.ktor:ktor-client-core:1.4.1'
24+
implementation 'io.ktor:ktor-client-core-jvm:1.4.1'
25+
implementation 'io.ktor:ktor-client-apache:1.4.1'
26+
implementation 'io.ktor:ktor-client-logging-jvm:1.4.1'
27+
implementation 'io.ktor:ktor-client-serialization-jvm:1.4.1'
28+
2629

2730
testCompile group: 'junit', name: 'junit', version: '4.12'
2831

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package nl.lengrand.swacli
2+
3+
import io.ktor.client.*
4+
import io.ktor.client.engine.apache.*
5+
import io.ktor.client.features.json.*
6+
import io.ktor.client.features.json.serializer.*
7+
import kotlinx.serialization.json.Json
8+
9+
object Configuration {
10+
11+
private val jsonSerializer = Json { ignoreUnknownKeys = true}
12+
13+
fun getHttpClient() = HttpClient(Apache) {
14+
install(JsonFeature) {
15+
serializer = KotlinxSerializer(jsonSerializer)
16+
}
17+
}
18+
}
+20-23
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
package nl.lengrand.swacli
22

3-
import com.fasterxml.jackson.databind.DeserializationFeature
4-
import com.fasterxml.jackson.databind.ObjectMapper
5-
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
6-
import com.github.kittinunf.fuel.Fuel
7-
import com.github.kittinunf.fuel.jackson.responseObject
8-
9-
val mapper: ObjectMapper = ObjectMapper().registerKotlinModule().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
3+
import io.ktor.client.request.*
4+
import io.ktor.http.*
5+
import kotlinx.serialization.Serializable
106

117
const val BASE_URL = "https://swapi.dev/api"
128

13-
class SwApi {
14-
companion object{
15-
fun getPeople(query : String?) : Response<People> {
16-
return Fuel.get("$BASE_URL/people/${queryString(query)}")
17-
.header("accept", "application/json")
18-
.responseObject<Response<People>>(mapper).third.get()
19-
}
9+
object SwApi {
10+
11+
private val httpClient = Configuration.getHttpClient()
2012

21-
fun getPlanets(query : String?) : Response<Planet> {
22-
return Fuel.get("$BASE_URL/planets/${queryString(query)}")
23-
.header("accept", "application/json")
24-
.responseObject<Response<Planet>>(mapper).third.get()
13+
suspend fun getPeople(query : String?) : Response<People> {
14+
return httpClient.get("$BASE_URL/people/${queryString(query)}") {
15+
header("Content-Type", ContentType.Application.Json.toString())
2516
}
17+
}
2618

27-
private fun queryString(query: String?) = if(query == null) "" else "?search=${query}"
19+
suspend fun getPlanets(query : String?) : Response<Planet> {
20+
return httpClient.get("$BASE_URL/planets/${queryString(query)}") {
21+
header("Content-Type", ContentType.Application.Json.toString())
22+
}
2823
}
24+
25+
private fun queryString(query: String?) = if(query == null) "" else "?search=${query}"
2926
}
3027

31-
sealed class Data
32-
data class Response<Data>(val count: Int, val next : String?, val previous : String?, val results : List<Data>)
33-
data class Planet(val climate: String, val name: String, val gravity: String, val orbital_period: String, val diameter: String) : Data()
34-
data class People(val name: String, val height: String, val mass: String, val hair_color: String, val homeworld: String, val birth_year: String) : Data()
28+
@Serializable sealed class Data
29+
@Serializable data class Response<Data>(val count: Int, val next : String?, val previous : String?, val results : List<Data>)
30+
@Serializable data class Planet(val climate: String, val name: String, val gravity: String, val orbital_period: String, val diameter: String) : Data()
31+
@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()

src/main/kotlin/nl/lengrand/swacli/SwaCLIOptions.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package nl.lengrand.swacli
22

3+
import kotlinx.coroutines.runBlocking
34
import picocli.CommandLine
45
import picocli.CommandLine.*
56
import java.util.concurrent.Callable
@@ -28,10 +29,12 @@ class SwaCLIOptions : Callable<Int> {
2829
}
2930

3031
override fun call(): Int {
31-
if(exclusive.characters)
32-
PrettyPrinter.print(SwApi.getPeople(searchQuery))
33-
if(exclusive.planets)
34-
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
32+
runBlocking {
33+
if (exclusive.characters)
34+
PrettyPrinter.print(SwApi.getPeople(searchQuery))
35+
if (exclusive.planets)
36+
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
37+
}
3538

3639
return 0
3740
}

src/main/kotlin/nl/lengrand/swacli/SwaCLIProgrammatic.kt

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package nl.lengrand.swacli
22

3+
import kotlinx.coroutines.runBlocking
34
import picocli.CommandLine
45
import picocli.CommandLine.IExecutionStrategy
56
import picocli.CommandLine.Model.CommandSpec
67
import picocli.CommandLine.Model.PositionalParamSpec
78
import picocli.CommandLine.ParseResult
89
import kotlin.system.exitProcess
910

10-
object SwaCLIProgrammatic {
11+
object SwaCLIProgrammatic {
1112

1213
private fun run(parseResult: ParseResult): Int {
1314
val helpExitCode = CommandLine.executeHelpRequest(parseResult)
@@ -17,10 +18,12 @@ object SwaCLIProgrammatic {
1718
val subResult = parseResult.subcommand()
1819
val searchQuery = if (subResult.hasMatchedPositional(0)) subResult.matchedPositional(0).stringValues()[0] else null
1920

20-
if (subResult.commandSpec().name() == "planets")
21-
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
22-
if (subResult.commandSpec().name() == "people")
23-
PrettyPrinter.print(SwApi.getPeople(searchQuery))
21+
runBlocking {
22+
if (subResult.commandSpec().name() == "planets")
23+
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
24+
if (subResult.commandSpec().name() == "people")
25+
PrettyPrinter.print(SwApi.getPeople(searchQuery))
26+
}
2427
}
2528
return 0
2629
}

src/main/kotlin/nl/lengrand/swacli/SwaCLISubCommands.kt

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package nl.lengrand.swacli
22

3+
import kotlinx.coroutines.runBlocking
34
import picocli.CommandLine
45
import picocli.CommandLine.*
56
import picocli.CommandLine.Model.*
@@ -37,7 +38,9 @@ class PlanetsCommand : Callable<Int> {
3738
private var searchQuery : String? = null
3839

3940
override fun call(): Int {
40-
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
41+
runBlocking {
42+
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
43+
}
4144
return 0
4245
}
4346
}
@@ -49,7 +52,9 @@ class PeopleCommand : Callable<Int> {
4952
private var searchQuery : String? = null
5053

5154
override fun call(): Int {
52-
PrettyPrinter.print(SwApi.getPeople(searchQuery))
55+
runBlocking {
56+
PrettyPrinter.print(SwApi.getPeople(searchQuery))
57+
}
5358
return 0
5459
}
5560
}

0 commit comments

Comments
 (0)