[BAEL-1401] Fuel HTTP Library with Kotlin (#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
This commit is contained in:
committed by
Predrag Maric
parent
717fb11d11
commit
a2f77a2783
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user