Skip to content

Commit a2f77a2

Browse files
freddyaottpedja4
authored andcommitted
[BAEL-1401] Fuel HTTP Library with Kotlin (eugenp#5142)
* 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
1 parent 717fb11 commit a2f77a2

6 files changed

Lines changed: 377 additions & 1 deletion

File tree

‎core-kotlin/pom.xml‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@
8787
<artifactId>h2</artifactId>
8888
<version>${h2database.version}</version>
8989
</dependency>
90+
<dependency>
91+
<groupId>com.github.kittinunf.fuel</groupId>
92+
<artifactId>fuel</artifactId>
93+
<version>${fuel.version}</version>
94+
</dependency>
95+
<dependency>
96+
<groupId>com.github.kittinunf.fuel</groupId>
97+
<artifactId>fuel-gson</artifactId>
98+
<version>${fuel.version}</version>
99+
</dependency>
100+
<dependency>
101+
<groupId>com.github.kittinunf.fuel</groupId>
102+
<artifactId>fuel-rxjava</artifactId>
103+
<version>${fuel.version}</version>
104+
</dependency>
105+
<dependency>
106+
<groupId>com.github.kittinunf.fuel</groupId>
107+
<artifactId>fuel-coroutines</artifactId>
108+
<version>${fuel.version}</version>
109+
</dependency>
90110
</dependencies>
91111

92112
<properties>
@@ -100,6 +120,7 @@
100120
<assertj.version>3.10.0</assertj.version>
101121
<h2database.version>1.4.197</h2database.version>
102122
<exposed.version>0.10.4</exposed.version>
123+
<fuel.version>1.15.0</fuel.version>
103124
</properties>
104125

105-
</project>
126+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.fuel
2+
3+
import com.github.kittinunf.fuel.core.Request
4+
5+
fun tokenInterceptor() = {
6+
next: (Request) -> Request ->
7+
{ req: Request ->
8+
req.header(mapOf("Authorization" to "Bearer AbCdEf123456"))
9+
next(req)
10+
}
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.fuel
2+
3+
import com.github.kittinunf.fuel.core.ResponseDeserializable
4+
import com.google.gson.Gson
5+
6+
data class Post(var userId:Int,
7+
var id:Int,
8+
var title:String,
9+
var body:String){
10+
11+
12+
class Deserializer : ResponseDeserializable<Array<Post>> {
13+
override fun deserialize(content: String): Array<Post> = Gson().fromJson(content, Array<Post>::class.java)
14+
}
15+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.fuel
2+
3+
import com.github.kittinunf.fuel.core.Method
4+
import com.github.kittinunf.fuel.util.FuelRouting
5+
6+
sealed class PostRoutingAPI : FuelRouting {
7+
8+
override val basePath = "https://jsonplaceholder.typicode.com"
9+
10+
class posts(val id: String, override val body: String?): PostRoutingAPI()
11+
12+
class comments(val postId: String, override val body: String?): PostRoutingAPI()
13+
14+
override val method: Method
15+
get() {
16+
return when(this) {
17+
is PostRoutingAPI.posts -> Method.GET
18+
is PostRoutingAPI.comments -> Method.GET
19+
}
20+
}
21+
22+
override val path: String
23+
get() {
24+
return when(this) {
25+
is PostRoutingAPI.posts -> "/posts"
26+
is PostRoutingAPI.comments -> "/comments"
27+
}
28+
}
29+
30+
override val params: List<Pair<String, Any?>>?
31+
get() {
32+
return when(this) {
33+
is PostRoutingAPI.posts -> listOf("id" to this.id)
34+
is PostRoutingAPI.comments -> listOf("postId" to this.postId)
35+
}
36+
}
37+
38+
override val headers: Map<String, String>?
39+
get() {
40+
return null
41+
}
42+
}

‎core-kotlin/src/test/kotlin/com/baeldung/builder/BuilderPatternUnitTest.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ internal class BuilderPatternUnitTest {
9393
Assertions.assertNull(foodOrder.fish)
9494
}
9595

96+
9697
@Test
9798
fun whenBuildingFoodOrderApplySettingValues_thenFieldsNotNull() {
9899

0 commit comments

Comments
 (0)