New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding integration for Retrofit #1037
Merged
leandroBorgesFerreira
merged 20 commits into
arrow-kt:master
from
leandroBorgesFerreira:master
Oct 24, 2018
Merged
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0673b00
adding retrofit adapter for arrow
leandroBorgesFerreira b8330c4
Fixing tests
leandroBorgesFerreira 2b49077
simple pr corrections
leandroBorgesFerreira 06a7781
small corrections in pr 2
leandroBorgesFerreira ba265f8
thinking about Response<R> or just R
leandroBorgesFerreira 5240354
removing IO adapter and pr adjustments
leandroBorgesFerreira c011713
Merge branch 'master' into master
pakoito c1ca395
Merge branch 'master' into master
leandroBorgesFerreira ecc8e37
Merge branch 'master' into master
leandroBorgesFerreira 1a7c2cd
refactor to extension functions
leandroBorgesFerreira e490bad
Merge branch 'master' into master
leandroBorgesFerreira 7dd20ef
pr fix
leandroBorgesFerreira 84aa047
Merge remote-tracking branch 'remotes/upstream/master'
leandroBorgesFerreira a592d0d
Merge branch 'master' into master
raulraja a6b73e8
fixing modules and imports
leandroBorgesFerreira a58acca
Merge branch 'master' into master
leandroBorgesFerreira 9cc9539
pr fixes
leandroBorgesFerreira 4f9f188
Merge branch 'master' of https://github.com/leandroBorgesFerreira/arrow
leandroBorgesFerreira 16d5444
Merge branch 'master' into master
leandroBorgesFerreira 9ac5402
Merge branch 'master' into master
leandroBorgesFerreira File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
19 changes: 19 additions & 0 deletions
19
modules/integrations/arrow-integrations-retrofit-adapter/build.gradle
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,19 @@ | ||
def retrofitVersion = "2.4.0" | ||
|
||
dependencies { | ||
compile project(':arrow-effects') | ||
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion" | ||
compile project(':arrow-annotations') | ||
compile "com.squareup.retrofit2:retrofit:$retrofitVersion" | ||
kapt project(':arrow-annotations-processor') | ||
kaptTest project(':arrow-annotations-processor') | ||
compileOnly project(':arrow-annotations-processor') | ||
testCompileOnly project(':arrow-annotations-processor') | ||
testCompile project(':arrow-test') | ||
testCompile "com.squareup.retrofit2:converter-gson:$retrofitVersion" | ||
testCompile 'com.squareup.okhttp3:mockwebserver:3.11.0' | ||
testCompile project(':arrow-effects-rx2') | ||
} | ||
|
||
apply from: rootProject.file('gradle/gradle-mvn-push.gradle') | ||
apply plugin: 'kotlin-kapt' |
4 changes: 4 additions & 0 deletions
4
modules/integrations/arrow-integrations-retrofit-adapter/gradle.properties
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,4 @@ | ||
# Maven publishing configuration | ||
POM_NAME=Arrow-Integrations-Retrofit-Adapter | ||
POM_ARTIFACT_ID=arrow-integration-retrofit-adapter | ||
POM_PACKAGING=jar |
36 changes: 36 additions & 0 deletions
36
...t-adapter/src/main/kotlin/arrow/integrations/retrofit/adapter/Async2CallAdapterFactory.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,36 @@ | ||
package arrow.integrations.retrofit.adapter | ||
|
||
import retrofit2.CallAdapter | ||
import retrofit2.Retrofit | ||
import java.lang.reflect.ParameterizedType | ||
import java.lang.reflect.Type | ||
|
||
class Async2CallAdapterFactory : CallAdapter.Factory() { | ||
|
||
companion object { | ||
fun create(): Async2CallAdapterFactory = Async2CallAdapterFactory() | ||
} | ||
|
||
override fun get(returnType: Type, annotations: Array<Annotation>, retrofit: Retrofit): CallAdapter<*, *>? { | ||
val rawType = CallAdapter.Factory.getRawType(returnType) | ||
|
||
if (returnType !is ParameterizedType) { | ||
val name = parseTypeName(returnType) | ||
throw IllegalArgumentException("Return type must be parameterized as " + | ||
"$name<Foo> or $name<out Foo>") | ||
} | ||
|
||
val effectType = CallAdapter.Factory.getParameterUpperBound(0, returnType) | ||
|
||
return if (rawType == CallK::class.java) { | ||
CallKind2CallAdapter<Type>(effectType) | ||
} else { | ||
null | ||
} | ||
} | ||
} | ||
|
||
private fun parseTypeName(type: Type) = | ||
type.typeName | ||
.split(".") | ||
.last() |
30 changes: 30 additions & 0 deletions
30
...ntegrations-retrofit-adapter/src/main/kotlin/arrow/integrations/retrofit/adapter/CallK.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,30 @@ | ||
package arrow.integrations.retrofit.adapter | ||
|
||
import arrow.Kind | ||
import arrow.effects.typeclasses.Async | ||
import arrow.effects.typeclasses.MonadDefer | ||
import arrow.typeclasses.MonadError | ||
import retrofit2.Call | ||
import retrofit2.Response | ||
|
||
data class CallK<R>(val call: Call<R>) { | ||
fun <F> async(AC: Async<F>): Kind<F, Response<R>> = call.runInAsyncContext(AC) | ||
|
||
fun <F> defer(defer: MonadDefer<F>): Kind<F, Response<R>> = call.runSyncDeferred(defer) | ||
|
||
fun <F> catch(monadError: MonadError<F, Throwable>): Kind<F, Response<R>> = call.runSyncCatch(monadError) | ||
} | ||
|
||
fun <F, A> Call<A>.runInAsyncContext(AC: Async<F>): Kind<F, Response<A>> = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move these to |
||
AC.async { callback -> | ||
enqueue(ResponseCallback(callback)) | ||
} | ||
|
||
fun <F, A> Call<A>.runSyncDeferred(defer: MonadDefer<F>): Kind<F, Response<A>> = defer { execute() } | ||
|
||
fun <F, A> Call<A>.runSyncCatch(monadError: MonadError<F, Throwable>): Kind<F, Response<A>> = | ||
monadError.run { | ||
catch { | ||
execute() | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...rofit-adapter/src/main/kotlin/arrow/integrations/retrofit/adapter/CallKind2CallAdapter.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 arrow.integrations.retrofit.adapter | ||
|
||
import retrofit2.Call | ||
import retrofit2.CallAdapter | ||
import java.lang.reflect.Type | ||
|
||
class CallKind2CallAdapter<R>(private val type: Type) : CallAdapter<R, CallK<R>> { | ||
override fun adapt(call: Call<R>): CallK<R> = CallK(call) | ||
|
||
override fun responseType(): Type = type | ||
} |
18 changes: 18 additions & 0 deletions
18
...-integrations-retrofit-adapter/src/main/kotlin/arrow/integrations/retrofit/adapter/Ext.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,18 @@ | ||
package arrow.integrations.retrofit.adapter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth renaming to |
||
|
||
import arrow.Kind | ||
import arrow.typeclasses.ApplicativeError | ||
import retrofit2.HttpException | ||
import retrofit2.Response | ||
|
||
fun <F, R> Response<R>.unwrapBody(apError: ApplicativeError<F, Throwable>): Kind<F, R> = | ||
if (this.isSuccessful) { | ||
val body = this.body() | ||
if (body != null) { | ||
apError.just(body) | ||
} else { | ||
apError.raiseError(IllegalStateException("The request returned a null body")) | ||
} | ||
} else { | ||
apError.raiseError(HttpException(this)) | ||
} |
18 changes: 18 additions & 0 deletions
18
...-retrofit-adapter/src/main/kotlin/arrow/integrations/retrofit/adapter/ResponseCallback.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,18 @@ | ||
package arrow.integrations.retrofit.adapter | ||
|
||
import arrow.core.Either | ||
import arrow.core.left | ||
import arrow.core.right | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
class ResponseCallback<R>(private val proc: (Either<Throwable, Response<R>>) -> Unit) : Callback<R> { | ||
override fun onResponse(call: Call<R>, response: Response<R>) { | ||
proc(response.right()) | ||
} | ||
|
||
override fun onFailure(call: Call<R>, throwable: Throwable) { | ||
proc(throwable.left()) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...apter/src/test/kotlin/arrow/integrations/retrofit/adapter/Async2CallAdapterFactoryTest.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 arrow.integrations.retrofit.adapter | ||
|
||
import arrow.effects.IO | ||
import arrow.integrations.retrofit.adapter.retrofit.retrofit | ||
import arrow.test.UnitSpec | ||
import com.google.common.reflect.TypeToken | ||
import io.kotlintest.KTestJUnitRunner | ||
import io.kotlintest.matchers.shouldBe | ||
import io.kotlintest.matchers.shouldThrow | ||
import okhttp3.HttpUrl | ||
import org.junit.runner.RunWith | ||
|
||
private val NO_ANNOTATIONS = emptyArray<Annotation>() | ||
|
||
private val retrofit = retrofit(HttpUrl.parse("http://localhost:1")!!) | ||
private val factory = Async2CallAdapterFactory.create() | ||
|
||
@RunWith(KTestJUnitRunner::class) | ||
class Async2CallAdapterFactoryTest : UnitSpec() { | ||
init { | ||
"Non CallK Class should return null" { | ||
factory.get(object : TypeToken<List<String>>() {}.type, NO_ANNOTATIONS, retrofit) shouldBe null | ||
} | ||
|
||
"Non parametrized type should throw exception" { | ||
val exceptionList = shouldThrow<IllegalArgumentException> { | ||
factory.get(List::class.java, NO_ANNOTATIONS, retrofit) | ||
} | ||
exceptionList.message shouldBe "Return type must be parameterized as List<Foo> or List<out Foo>" | ||
|
||
val exceptionIO = shouldThrow<IllegalArgumentException> { | ||
factory.get(IO::class.java, NO_ANNOTATIONS, retrofit) | ||
} | ||
exceptionIO.message shouldBe "Return type must be parameterized as IO<Foo> or IO<out Foo>" | ||
} | ||
|
||
"Should work for CallK types" { | ||
factory.get(object : TypeToken<CallK<String>>() {}.type, NO_ANNOTATIONS, retrofit)!! | ||
.responseType() shouldBe String::class.java | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...-retrofit-adapter/src/test/kotlin/arrow/integrations/retrofit/adapter/ProcCallBackTest.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,74 @@ | ||
package arrow.integrations.retrofit.adapter | ||
|
||
import arrow.core.Either | ||
import arrow.core.applicativeError | ||
import arrow.core.fix | ||
import arrow.effects.IO | ||
import arrow.effects.ObservableK | ||
import arrow.effects.applicativeError | ||
import arrow.effects.async | ||
import arrow.effects.fix | ||
import arrow.effects.monadDefer | ||
import arrow.integrations.retrofit.adapter.mock.ResponseMock | ||
import arrow.integrations.retrofit.adapter.retrofit.ApiClientTest | ||
import arrow.integrations.retrofit.adapter.retrofit.retrofit | ||
import arrow.test.UnitSpec | ||
import io.kotlintest.KTestJUnitRunner | ||
import io.kotlintest.matchers.fail | ||
import okhttp3.HttpUrl | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.junit.Assert.assertEquals | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(KTestJUnitRunner::class) | ||
class ProcCallBackTest : UnitSpec() { | ||
private val server = MockWebServer().apply { | ||
enqueue(MockResponse().setBody("{\"response\": \"hello, world!\"}").setResponseCode(200)) | ||
start() | ||
} | ||
|
||
private val baseUrl: HttpUrl = server.url("/") | ||
|
||
init { | ||
"should be able to parse answer with ForIO" { | ||
createApiClientTest(baseUrl) | ||
.testCallK() | ||
.async(IO.async()) | ||
.fix() | ||
.unsafeRunSync() | ||
.unwrapBody(Either.applicativeError()) | ||
.fix() | ||
.fold({ throwable -> | ||
fail("The requested terminated with an exception. Message: ${throwable.message}") | ||
}, { responseMock -> | ||
assertEquals(ResponseMock("hello, world!"), responseMock) | ||
}) | ||
} | ||
|
||
"should be able to parse answer with ForObservableK" { | ||
with(ObservableK) { | ||
createApiClientTest(baseUrl) | ||
.testCallK() | ||
.defer(monadDefer()) | ||
.fix() | ||
.flatMap { response -> | ||
response.unwrapBody(applicativeError()).fix() | ||
} | ||
.observable | ||
.test() | ||
.assertValue(ResponseMock("hello, world!")) | ||
} | ||
} | ||
|
||
"should be able to run a POST with UNIT as response" { | ||
createApiClientTest(baseUrl) | ||
.testIOResponsePost() | ||
.async(IO.async()) | ||
.fix() | ||
.unsafeRunSync() | ||
} | ||
} | ||
} | ||
|
||
private fun createApiClientTest(baseUrl: HttpUrl) = retrofit(baseUrl).create(ApiClientTest::class.java) |
41 changes: 41 additions & 0 deletions
41
...rofit-adapter/src/test/kotlin/arrow/integrations/retrofit/adapter/ResponseCallbackTest.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,41 @@ | ||
package arrow.integrations.retrofit.adapter | ||
|
||
import arrow.effects.IO | ||
import arrow.effects.async | ||
import arrow.effects.fix | ||
import arrow.integrations.retrofit.adapter.retrofit.ApiClientTest | ||
import arrow.integrations.retrofit.adapter.retrofit.retrofit | ||
import arrow.test.UnitSpec | ||
import io.kotlintest.KTestJUnitRunner | ||
import io.kotlintest.matchers.fail | ||
import okhttp3.HttpUrl | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(KTestJUnitRunner::class) | ||
class ResponseCallbackTest : UnitSpec() { | ||
private val server = MockWebServer().apply { | ||
enqueue(MockResponse().setBody("{response: \"hello, world!\"}").setResponseCode(200)) | ||
start() | ||
} | ||
|
||
private val baseUrl: HttpUrl = server.url("/") | ||
|
||
init { | ||
"bad deserialization should return Either.Left" { | ||
createApiClientTest(baseUrl) | ||
.testCallK() | ||
.async(IO.async()) | ||
.fix() | ||
.attempt() | ||
.unsafeRunSync() | ||
.fold({ | ||
}, { | ||
fail("The request should have not terminated correctly") | ||
}) | ||
} | ||
} | ||
} | ||
|
||
private fun createApiClientTest(baseUrl: HttpUrl) = retrofit(baseUrl).create(ApiClientTest::class.java) |
3 changes: 3 additions & 0 deletions
3
...retrofit-adapter/src/test/kotlin/arrow/integrations/retrofit/adapter/mock/ResponseMock.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,3 @@ | ||
package arrow.integrations.retrofit.adapter.mock | ||
|
||
data class ResponseMock(val response: String) |
19 changes: 19 additions & 0 deletions
19
...fit-adapter/src/test/kotlin/arrow/integrations/retrofit/adapter/retrofit/ApiClientTest.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,19 @@ | ||
package arrow.integrations.retrofit.adapter.retrofit | ||
|
||
import arrow.integrations.retrofit.adapter.CallK | ||
import arrow.integrations.retrofit.adapter.mock.ResponseMock | ||
import retrofit2.http.GET | ||
import retrofit2.http.POST | ||
|
||
interface ApiClientTest { | ||
|
||
@GET("test") | ||
fun testCallK(): CallK<ResponseMock> | ||
|
||
@GET("testCallKResponse") | ||
fun testCallKResponse(): CallK<ResponseMock> | ||
|
||
@POST("testIOResponsePOST") | ||
fun testIOResponsePost(): CallK<Unit> | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...retrofit-adapter/src/test/kotlin/arrow/integrations/retrofit/adapter/retrofit/Retrofit.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,20 @@ | ||
package arrow.integrations.retrofit.adapter.retrofit | ||
|
||
import arrow.integrations.retrofit.adapter.Async2CallAdapterFactory | ||
import okhttp3.HttpUrl | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
private fun provideOkHttpClient(): OkHttpClient = | ||
OkHttpClient.Builder().build() | ||
|
||
private fun configRetrofit(retrofitBuilder: Retrofit.Builder) = | ||
retrofitBuilder | ||
.addCallAdapterFactory(Async2CallAdapterFactory.create()) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.client(provideOkHttpClient()) | ||
|
||
private fun getRetrofitBuilderDefaults(baseUrl: HttpUrl) = Retrofit.Builder().baseUrl(baseUrl) | ||
|
||
fun retrofit(baseUrl: HttpUrl): Retrofit = configRetrofit(getRetrofitBuilderDefaults(baseUrl)).build() |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we don't do
IO
, what about justCallKindAdapterFactory
?