Skip to content

Commit

Permalink
[BAEL-1401] Fuel HTTP Library with Kotlin (eugenp#5142)
Browse files Browse the repository at this point in the history
* builder pattern in kotlin

* builder pattern in kotlin new-line

* deleted Sandbox, added unit test

* add other tests

* named and default parameters builder

* Make FoodOrderNamed a data class

* idiomatic Kotlin version

* Fuel HTTP library

* fuel pom property

* BAEL-1401 Removed extra pom.xml properties
  • Loading branch information
freddyaott authored and pedja4 committed Sep 6, 2018
1 parent 717fb11 commit a2f77a2
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 1 deletion.
23 changes: 22 additions & 1 deletion core-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@
<artifactId>h2</artifactId>
<version>${h2database.version}</version>
</dependency>
<dependency>
<groupId>com.github.kittinunf.fuel</groupId>
<artifactId>fuel</artifactId>
<version>${fuel.version}</version>
</dependency>
<dependency>
<groupId>com.github.kittinunf.fuel</groupId>
<artifactId>fuel-gson</artifactId>
<version>${fuel.version}</version>
</dependency>
<dependency>
<groupId>com.github.kittinunf.fuel</groupId>
<artifactId>fuel-rxjava</artifactId>
<version>${fuel.version}</version>
</dependency>
<dependency>
<groupId>com.github.kittinunf.fuel</groupId>
<artifactId>fuel-coroutines</artifactId>
<version>${fuel.version}</version>
</dependency>
</dependencies>

<properties>
Expand All @@ -100,6 +120,7 @@
<assertj.version>3.10.0</assertj.version>
<h2database.version>1.4.197</h2database.version>
<exposed.version>0.10.4</exposed.version>
<fuel.version>1.15.0</fuel.version>
</properties>

</project>
</project>
11 changes: 11 additions & 0 deletions core-kotlin/src/main/kotlin/com/baeldung/fuel/Interceptors.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.fuel

import com.github.kittinunf.fuel.core.Request

fun tokenInterceptor() = {
next: (Request) -> Request ->
{ req: Request ->
req.header(mapOf("Authorization" to "Bearer AbCdEf123456"))
next(req)
}
}
15 changes: 15 additions & 0 deletions core-kotlin/src/main/kotlin/com/baeldung/fuel/Post.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.fuel

import com.github.kittinunf.fuel.core.ResponseDeserializable
import com.google.gson.Gson

data class Post(var userId:Int,
var id:Int,
var title:String,
var body:String){


class Deserializer : ResponseDeserializable<Array<Post>> {
override fun deserialize(content: String): Array<Post> = Gson().fromJson(content, Array<Post>::class.java)
}
}
42 changes: 42 additions & 0 deletions core-kotlin/src/main/kotlin/com/baeldung/fuel/PostRoutingAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.fuel

import com.github.kittinunf.fuel.core.Method
import com.github.kittinunf.fuel.util.FuelRouting

sealed class PostRoutingAPI : FuelRouting {

override val basePath = "https://jsonplaceholder.typicode.com"

class posts(val id: String, override val body: String?): PostRoutingAPI()

class comments(val postId: String, override val body: String?): PostRoutingAPI()

override val method: Method
get() {
return when(this) {
is PostRoutingAPI.posts -> Method.GET
is PostRoutingAPI.comments -> Method.GET
}
}

override val path: String
get() {
return when(this) {
is PostRoutingAPI.posts -> "/posts"
is PostRoutingAPI.comments -> "/comments"
}
}

override val params: List<Pair<String, Any?>>?
get() {
return when(this) {
is PostRoutingAPI.posts -> listOf("id" to this.id)
is PostRoutingAPI.comments -> listOf("postId" to this.postId)
}
}

override val headers: Map<String, String>?
get() {
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ internal class BuilderPatternUnitTest {
Assertions.assertNull(foodOrder.fish)
}


@Test
fun whenBuildingFoodOrderApplySettingValues_thenFieldsNotNull() {

Expand Down
Loading

0 comments on commit a2f77a2

Please sign in to comment.