Skip to content
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

v5: Sealed interfaces, Raw Retrofit Responses, Kotest Migration, Kotlin 1.6.0 #50

Merged
merged 14 commits into from
Dec 16, 2021

Conversation

haroldadmin
Copy link
Owner

@haroldadmin haroldadmin commented Dec 4, 2021

This PR accomplishes some milestones aimed at the next major release of the library (#34)

Sealed Interfaces

The NetworkResponse class is now based on sealed interfaces. This allows for more concise when expressions when you don't care about the specific type of the error:

when (networkResponse) {
  is NetworkResponse.Success -> ...
  is NetworkResponse.Error -> ...
}

Raw Retrofit Responses

NetworkResponse.Success and NetworkResponse.ServerError now bundle the raw retrofit Response<S> object to allow for greater access to the response of the network request.

when (networkResponse) {
  is NetworkResponse.Success -> {
    val statusCode = networkResponse.response.code()
  }
}

We still supply code and headers properties as before to retain familiarity with the existing API design.

Handling Empty Response Bodies

In the current version of the library you have to use the Unit response type if you expected your server to respond without a body. This is fine when the API never returns a body, but causes problems if it sometimes returns a body and sometimes doesn't (200 vs 204 status code).

The bundled raw Retrofit responses provide a better way to handle this situation.

interface PostsService {
  @GET("/")
  suspend fun getPost(): NetworkResponse<Unit, ErrorResponse>
}

when (val postResponse  = service.getPost()) {
  is NetworkResponse.Success -> {
    if (postResponse.code != 204) {
      val rawBody = postResponse.response.rawBody()
      // Manually parse the raw body to access the response
    }
  }
  is NetworkResponse.Error -> { ... }
}

Tests Overhaul & Migration to Kotest

This PR gets refactors the library's test suite to get rid of redundant and obscure tests, and replaces them with a streamlined test suite focused on publicly exposed functionality.

We've also finally moved away from the deprecated kotlintest artifacts to the new kotest libraries.

Remove Deprecated Classes

We've removed deprecated ways to instantiate adapter factory. The existing classes had been marked as deprecated for a long period, and I hope everyone has moved away from them.

Kotlin 1.6.0

Updated the language level to 1.6.0

Sample App

Add a module showing sample usage of the library using the kotlinx.serialization library.

build.gradle.kts Outdated Show resolved Hide resolved
*
* when (response) {
* is NetworkResponse.Success -> // Action Succeeded do something with body
* If you expect your server response to not contain a body, set the success body type ([S]) to [Unit],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be a NetworkResponse.Complete and there can be a discussion on if we should include
public val response: Response<*> or not

we can also add a typealias of CompletableResponse

typealias CompletableResponse<E> = NetworkResponse<Unit, E>

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've included the raw Response as an escape hatch when you want more control over the response than what this library provides. I see no harm in adding it, but a loss of control for consumers in removing it. Unless there's a strong argument against not including it, it's better to leave it in there IMO.

Great idea on the CompletableResponse, I'll add it as a nice utility.

@gilgoldzweig
Copy link

gilgoldzweig commented Dec 8, 2021 via email

@haroldadmin
Copy link
Owner Author

Appreciate the thorough review, @gilgoldzweig and @argenkiwi, the feedback is really useful. I'll update the PR with the requested changes.

Other changes:
- Improve documentation on the public API surface
- Implement OkHttp timeout for faster test execution
- Add tests for successful responses with errors during body deserialization
- Add CompletableResponse type alias for NetworkResponse<Unit, E>
@haroldadmin
Copy link
Owner Author

I've updated the pull request with your feedback. Would appreciate another round of review, @gilgoldzweig @argenkiwi.

Copy link

@gilgoldzweig gilgoldzweig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fantastic, awesome job!!!

@haroldadmin haroldadmin linked an issue Dec 14, 2021 that may be closed by this pull request
Closed
Copy link
Contributor

@argenkiwi argenkiwi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the good work @haroldadmin and apologies for the late reply. Looking forward to the next release. :D

@haroldadmin haroldadmin merged commit 76123c3 into master Dec 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

V5.0
3 participants