forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
1 parent
717fb11
commit a2f77a2
Showing
6 changed files
with
377 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
core-kotlin/src/main/kotlin/com/baeldung/fuel/Interceptors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
core-kotlin/src/main/kotlin/com/baeldung/fuel/PostRoutingAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.