-
Notifications
You must be signed in to change notification settings - Fork 432
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
Increasing Test Coverages #866
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 8 additions & 12 deletions
20
fuel-jackson-jvm/src/main/kotlin/fuel/jackson/ResponseExtension.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 |
---|---|---|
@@ -1,19 +1,15 @@ | ||
package fuel.jackson | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import com.fasterxml.jackson.module.kotlin.registerKotlinModule | ||
import com.github.kittinunf.result.Result | ||
import com.github.kittinunf.result.doTry | ||
import com.github.kittinunf.result.runCatching | ||
import fuel.HttpResponse | ||
|
||
public val defaultMapper: ObjectMapper = ObjectMapper().registerKotlinModule() | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
|
||
public inline fun <reified T : Any> HttpResponse.toJackson(mapper: ObjectMapper = defaultMapper): Result<T, Throwable> = | ||
doTry(work = { | ||
Result.success(mapper.readValue(body.string())) | ||
}, errorHandler = { | ||
Result.failure(it) | ||
}) | ||
public inline fun <reified T : Any> HttpResponse.toJackson( | ||
mapper: ObjectMapper = jacksonObjectMapper() | ||
): Result<T?, Throwable> = | ||
runCatching { | ||
mapper.readValue(body.string()) | ||
} |
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 |
---|---|---|
|
@@ -14,6 +14,12 @@ import org.junit.Assert.fail | |
import org.junit.Test | ||
|
||
class FuelJacksonTest { | ||
|
||
private val createCustomMapper: ObjectMapper = ObjectMapper().registerKotlinModule() | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).apply { | ||
propertyNamingStrategy = PropertyNamingStrategies.SNAKE_CASE | ||
} | ||
|
||
data class HttpBinUserAgentModel( | ||
val userAgent: String = "", | ||
val http_status: String = "" | ||
|
@@ -29,7 +35,7 @@ class FuelJacksonTest { | |
val response = Fuel.get(mockWebServer.url("user-agent").toString()) | ||
val jackson = response.toJackson<HttpBinUserAgentModel>() | ||
jackson.fold({ | ||
assertEquals("Fuel", it.userAgent) | ||
assertEquals("Fuel", it?.userAgent) | ||
}, { | ||
fail(it.localizedMessage) | ||
}) | ||
|
@@ -45,19 +51,14 @@ class FuelJacksonTest { | |
} | ||
|
||
val response = Fuel.get(mockWebServer.url("user-agent").toString()) | ||
val jackson = response.toJackson<HttpBinUserAgentModel>(createCustomMapper()) | ||
val jackson = response.toJackson<HttpBinUserAgentModel>(createCustomMapper) | ||
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. [q] Just wondering whether we could provide a nice DSL for client to configure the mapper? fun toJackson<T>(configure: ObjectMapper.() -> Unit) {
val mapper = ObjectMapper()
mapper.apply(configure)
} 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. you could have to write a PR about it. |
||
jackson.fold({ | ||
assertEquals("", it.userAgent) | ||
assertEquals("OK", it.http_status) | ||
assertEquals("", it?.userAgent) | ||
assertEquals("OK", it?.http_status) | ||
}, { | ||
fail(it.localizedMessage) | ||
}) | ||
|
||
mockWebServer.shutdown() | ||
} | ||
|
||
private fun createCustomMapper(): ObjectMapper = ObjectMapper().registerKotlinModule() | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).apply { | ||
propertyNamingStrategy = PropertyNamingStrategies.SNAKE_CASE | ||
} | ||
} |
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
17 changes: 8 additions & 9 deletions
17
fuel-moshi-jvm/src/main/kotlin/fuel/moshi/ResponseExtension.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 |
---|---|---|
@@ -1,24 +1,23 @@ | ||
package fuel.moshi | ||
|
||
import com.github.kittinunf.result.Result | ||
import com.github.kittinunf.result.runCatching | ||
import com.squareup.moshi.JsonAdapter | ||
import com.squareup.moshi.Moshi | ||
import fuel.HttpResponse | ||
import java.io.IOException | ||
import java.lang.reflect.Type | ||
|
||
public val defaultMoshi: Moshi.Builder = Moshi.Builder() | ||
|
||
public inline fun <reified T : Any> HttpResponse.toMoshi(): Result<T?, IOException> = toMoshi(T::class.java) | ||
public inline fun <reified T : Any> HttpResponse.toMoshi(): Result<T?, Throwable> = toMoshi(T::class.java) | ||
|
||
public fun <T : Any> HttpResponse.toMoshi(clazz: Class<T>): Result<T?, IOException> = | ||
public fun <T : Any> HttpResponse.toMoshi(clazz: Class<T>): Result<T?, Throwable> = | ||
toMoshi(defaultMoshi.build().adapter(clazz)) | ||
|
||
public fun <T : Any> HttpResponse.toMoshi(type: Type): Result<T?, IOException> = | ||
public fun <T : Any> HttpResponse.toMoshi(type: Type): Result<T?, Throwable> = | ||
toMoshi(defaultMoshi.build().adapter(type)) | ||
|
||
public fun <T : Any> HttpResponse.toMoshi(jsonAdapter: JsonAdapter<T>): Result<T?, IOException> = try { | ||
Result.success(jsonAdapter.fromJson(body.source())) | ||
} catch (ioe: IOException) { | ||
Result.failure(ioe) | ||
} | ||
public fun <T : Any> HttpResponse.toMoshi(jsonAdapter: JsonAdapter<T>): Result<T?, Throwable> = | ||
runCatching { | ||
jsonAdapter.fromJson(body.source()) | ||
} |
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
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.
Cool 👍