Skip to content

Commit

Permalink
Merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
jlengrand committed Oct 22, 2020
2 parents 8612f02 + 80fdde9 commit aac3da4
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 40 deletions.
Binary file modified .gradle/6.3/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/6.3/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/6.3/fileContent/fileContent.lock
Binary file not shown.
Binary file modified .gradle/6.3/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/6.3/fileHashes/fileHashes.lock
Binary file not shown.
Binary file added .gradle/6.5.1/fileChanges/last-build.bin
Binary file not shown.
Binary file added .gradle/6.5.1/fileHashes/fileHashes.lock
Binary file not shown.
Empty file added .gradle/6.5.1/gc.properties
Empty file.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
2 changes: 1 addition & 1 deletion .gradle/buildOutputCleanup/cache.properties
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 modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file modified .gradle/checksums/checksums.lock
Binary file not shown.
Binary file modified .gradle/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified .gradle/checksums/sha1-checksums.bin
Binary file not shown.
13 changes: 8 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.10'
}

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

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation 'com.fasterxml.jackson.core:jackson-core: 2.9.8'
implementation 'com.fasterxml.jackson.core:jackson-databind: 2.9.8'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8'

implementation 'info.picocli:picocli:4.5.2'
implementation 'com.github.kittinunf.fuel:fuel:2.3.0'
implementation 'com.github.kittinunf.fuel:fuel-jackson:2.3.0'
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0'
implementation 'io.ktor:ktor-client-core:1.4.1'
implementation 'io.ktor:ktor-client-core-jvm:1.4.1'
implementation 'io.ktor:ktor-client-apache:1.4.1'
implementation 'io.ktor:ktor-client-logging-jvm:1.4.1'
implementation 'io.ktor:ktor-client-serialization-jvm:1.4.1'


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

Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/nl/lengrand/swacli/Configuration.kt
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)
}
}
}
43 changes: 20 additions & 23 deletions src/main/kotlin/nl/lengrand/swacli/SwApi.kt
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()
11 changes: 7 additions & 4 deletions src/main/kotlin/nl/lengrand/swacli/SwaCLIOptions.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nl.lengrand.swacli

import kotlinx.coroutines.runBlocking
import picocli.CommandLine
import picocli.CommandLine.*
import java.util.concurrent.Callable
Expand Down Expand Up @@ -28,10 +29,12 @@ class SwaCLIOptions : Callable<Int> {
}

override fun call(): Int {
if(exclusive.characters)
PrettyPrinter.print(SwApi.getPeople(searchQuery))
if(exclusive.planets)
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
runBlocking {
if (exclusive.characters)
PrettyPrinter.print(SwApi.getPeople(searchQuery))
if (exclusive.planets)
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
}

return 0
}
Expand Down
13 changes: 8 additions & 5 deletions src/main/kotlin/nl/lengrand/swacli/SwaCLIProgrammatic.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package nl.lengrand.swacli

import kotlinx.coroutines.runBlocking
import picocli.CommandLine
import picocli.CommandLine.IExecutionStrategy
import picocli.CommandLine.Model.CommandSpec
import picocli.CommandLine.Model.PositionalParamSpec
import picocli.CommandLine.ParseResult
import kotlin.system.exitProcess

object SwaCLIProgrammatic {
object SwaCLIProgrammatic {

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

if (subResult.commandSpec().name() == "planets")
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
if (subResult.commandSpec().name() == "people")
PrettyPrinter.print(SwApi.getPeople(searchQuery))
runBlocking {
if (subResult.commandSpec().name() == "planets")
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
if (subResult.commandSpec().name() == "people")
PrettyPrinter.print(SwApi.getPeople(searchQuery))
}
}
return 0
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/nl/lengrand/swacli/SwaCLISubCommands.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nl.lengrand.swacli

import kotlinx.coroutines.runBlocking
import picocli.CommandLine
import picocli.CommandLine.*
import picocli.CommandLine.Model.*
Expand Down Expand Up @@ -37,7 +38,9 @@ class PlanetsCommand : Callable<Int> {
private var searchQuery : String? = null

override fun call(): Int {
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
runBlocking {
PrettyPrinter.print(SwApi.getPlanets(searchQuery))
}
return 0
}
}
Expand All @@ -49,7 +52,9 @@ class PeopleCommand : Callable<Int> {
private var searchQuery : String? = null

override fun call(): Int {
PrettyPrinter.print(SwApi.getPeople(searchQuery))
runBlocking {
PrettyPrinter.print(SwApi.getPeople(searchQuery))
}
return 0
}
}

0 comments on commit aac3da4

Please sign in to comment.