-
-
Notifications
You must be signed in to change notification settings - Fork 827
Replace retrofit api dependency with implementation #5950
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
base: main
Are you sure you want to change the base?
Replace retrofit api dependency with implementation #5950
Conversation
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.
It seems you haven't yet signed a CLA. Please do so here.
Once you do that we will be able to review and accept this pull request.
Thanks!
| import java.lang.reflect.ParameterizedType | ||
| import java.lang.reflect.Type | ||
|
|
||
| class HttpExceptionCallAdapterFactory : CallAdapter.Factory() { |
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.
Added this adapter to minimize the amount of code changes. IMO we should replace it with an Result style adapter to remove exception-based error handling. Im happy to do it in this /separate PR.
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.
Changing every API to return a Result<> is a big task. I invite you to first create an issue with proper explanations. It needs to be in another PR (maybe multiples one), then adding a lint rule to force the usage of Result.
Currently exception handling in the application is not the best and would need huge changes in general (stopping using catch(e:Exception) that is way too broad for instance)
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.
ktlint found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.
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.
Pull Request Overview
This PR replaces the api dependency configuration with implementation for Retrofit in the common module, preventing the leakage of Retrofit types into dependent modules' public APIs. This change encapsulates the HTTP client implementation details within the common module.
Key changes:
- Created a custom
HttpExceptionclass to replace Retrofit'sHttpExceptionthroughout the codebase - Introduced
UrlBuilderutility class to encapsulate OkHttp URL building logic - Updated all modules to use the new custom exception type
Reviewed Changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| common/build.gradle.kts | Changed Retrofit dependency from api to implementation |
| common/src/main/kotlin/.../HttpException.kt | Added custom HttpException data class |
| common/src/main/kotlin/.../HttpExceptionCallAdapterFactory.kt | Added call adapter to convert Retrofit's HttpException to custom type |
| common/src/main/kotlin/.../UrlBuilder.kt | Added utility class for URL building |
| common/src/main/kotlin/.../HomeAssistantApis.kt | Registered HttpExceptionCallAdapterFactory with Retrofit |
| onboarding/.../ConnectionViewModel.kt | Replaced OkHttp URL building with UrlBuilder |
| onboarding/.../NameYourDeviceViewModel.kt | Updated to use custom HttpException |
| app/.../LaunchActivity.kt | Updated to use custom HttpException |
| app/.../HaControlsProviderService.kt | Updated to use custom HttpException and removed manual error creation |
| wear/.../EntityStateDataSourceService.kt | Removed unused Retrofit HttpException import |
| */gradle.lockfile | Updated dependency configurations reflecting the change from api to implementation |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
TimoPtr
left a comment
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.
Today our application is not using retrofit2.Call API from retrofit but the coroutine capability of Retrofit, so the factory that you've created won't do anything unfortunately.
Furthermore you are using the onFailure from the Call API. The documentation is clear
Invoked when a network exception occurred talking to the server or when an unexpected exception occurred creating the request or processing the response.
Error like 404 are still handled by onResponse according to the documentation
Invoked for a received HTTP response.
Note: An HTTP response may still indicate an application-level failure such as a 404 or 500. Call Response.isSuccessful() to determine if the response indicates success.
It means that if we merge your PR today the app won't display the right message in LaunchActivity since it is going to be the retrofit HttpException thrown not yours.
| @@ -0,0 +1,53 @@ | |||
| package io.homeassistant.companion.android.common.data | |||
|
|
|||
| import io.homeassistant.companion.android.common.exception.HttpException | |||
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.
| import io.homeassistant.companion.android.common.exception.HttpException | |
| import io.homeassistant.companion.android.common.data.exception.HttpException |
And you should also move MalformedHttpUrlException
| import retrofit2.Response | ||
| import retrofit2.Retrofit | ||
|
|
||
| class HttpExceptionCallAdapterFactory : CallAdapter.Factory() { |
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.
From https://developers.home-assistant.io/docs/android/best_practices#documentation Future-proof: Ask yourself, "Will I understand what I did in 6 months?"
I think we would benefits from having a documentation on the class definition explaining what is the goal of this factory.
| import java.lang.reflect.ParameterizedType | ||
| import java.lang.reflect.Type | ||
|
|
||
| class HttpExceptionCallAdapterFactory : CallAdapter.Factory() { |
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.
Changing every API to return a Result<> is a big task. I invite you to first create an issue with proper explanations. It needs to be in another PR (maybe multiples one), then adding a lint rule to force the usage of Result.
Currently exception handling in the application is not the best and would need huge changes in general (stopping using catch(e:Exception) that is way too broad for instance)
| import retrofit2.Response | ||
| import retrofit2.Retrofit | ||
|
|
||
| class HttpExceptionCallAdapterFactory : CallAdapter.Factory() { |
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.
| class HttpExceptionCallAdapterFactory : CallAdapter.Factory() { | |
| internal class HttpExceptionCallAdapterFactory : CallAdapter.Factory() { |
This should not be exposed outside of the common module.
| val convertedException = if (t is retrofit2.HttpException) { | ||
| HttpException( | ||
| code = t.code(), | ||
| message = t.response()?.errorBody()?.string() ?: t.message(), |
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.
I would simply use the message from the exception they already handle everything properly.
| message = t.response()?.errorBody()?.string() ?: t.message(), | |
| message = t.message, |
| import okhttp3.HttpUrl.Companion.toHttpUrl | ||
|
|
||
| @Singleton | ||
| class UrlBuilder @Inject constructor() { |
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.
I don't think this is relevant to the current PR.
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Summary
Replaced
apiRetrofit dependency fromcommonmodule withimplementation.Closes #5421
Checklist
Screenshots
Link to pull request in documentation repositories
User Documentation: home-assistant/companion.home-assistant#
Developer Documentation: home-assistant/developers.home-assistant#
Any other notes