Created
January 20, 2020 08:35
-
-
Save ImanX/2bb6c86a796d468a9290c5c892bb7f17 to your computer and use it in GitHub Desktop.
A Simple build of Retrofit interface API by Operator function in Kotlin
This file contains hidden or 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
//Now you can use it this way | |
CoroutineScope(Dispatchers.IO).launch { | |
val user = User(); | |
UserInfoAPI().add(user) | |
UserInfoAPI().edit(user) | |
UserInfoAPI().delete(10) | |
} |
This file contains hidden or 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
interface UserInfoAPI { | |
companion object { | |
operator fun invoke(): UserInfoAPI { | |
return Retrofit.Builder().apply { | |
baseUrl("https://endpoint.com") | |
addConverterFactory(GsonConverterFactory.create()) | |
}.build().create(UserInfoAPI::class.java) | |
} | |
} | |
@POST("add/user.json") | |
suspend fun add(@Body user: User): Boolean | |
@PUT("edit/user.json") | |
suspend fun edit(@Body user: User): User | |
@DELETE("delete/user.json") | |
suspend fun delete(@Body userId: Int): Boolean | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment