-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Request Adapter State #3504
Request Adapter State #3504
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.
👍 This looks good functionally, I just have a testing suggestion. I'd also like to ask you update the various bits of RequestAdapter documentation in the Usage and AdvancedUsage documents (and anywhere else you can think of) to reflect the new protocol signature.
Tests/RequestInterceptorTests.swift
Outdated
| completion(.success(request)) | ||
| } | ||
|
|
||
| let state = RequestAdapterState(requestID: UUID(), session: session) |
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 suggest capturing the UUID separately and then comparing the values returned from the adaptation to those provided in the RequestAdapterState, just to ensure they're round tripping appropriately.
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.
Will do!
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.
Fixed in 2dbda96. A little tricky to implement, but a subclass Adapter fixed things right up.
|
That's good feedback @jshier. I'll update the docs as well attempting to explain the second API and why it exists. |
|
Updated the docs in a602c12. |
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.
👍 Looks good with the documentation. I might add a bit about how the new method will become the requirement in the future, but that's up to you. Feel free to merge when you want.
…ptation (#3504) * Added RequestAdapterState struct to store additional state during adaptation * Updated adapter tests to verify RequestAdapterState passthrough * Updated RequestAdapter documentation in AdvancedUsage * Added comment about RequestAdapterState method becoming new requirement
This PR adds a new
RequestAdapterStatestruct, a newRequestAdapterprotocol API, and a default protocol extension for the new API for backwards compatibility.Goals ⚽
To allow client libraries to match the
URLRequestin aRequestAdapterwith theRequestobject they created.Instead, I'd like to propose the addition of the new adapt API:
This new API will give you access to the original
Request.idwhich you can then use in your client library to do anything you need. In my example above, I'll now be able to usestate.requestIDas therequestIDin that example toauthenticatetheURLRequestwith theAuthorizationheader.Implementation Details 🚧
The new
RequestAdapterStatestruct is a new approach to protocol design in Alamofire when we (Alamofire) need to expose parts of the internal state to the client. Instead of always trying to customize the parameters in the protocol functions, we should instead defineStateobjects which we can add / remove state from over time which can actually be deprecated properly between MAJOR version changes. Right now, there's no possible way to extend the currentadaptAPI without creating a completely new function. This approach should give us more flexibility over time when we find additional cases where we need to expose a bit more state to provide some extra functionality.I decided to implement the new API as a protocol extension for backwards compatibility. I also considered adding a new
RequestAdapterV2protocol which extendedRequestAdapter. This approach ended up not giving us any actual benefit or easier deprecation strategy than just adding the function on toRequestAdapterdirectly. I also considered adding a separateRequestAdapterV2protocol, but that's just not practical given how different public APIs we support withRequestInterceptor. In the end, it ended up making the most sense to add another method to the protocol and add a default protocol extension for backwards compatibility.Testing Details 🔍
I added a few tests in the cases where I wanted to verify that the classes (
AdapterandInterceptor) are actually overriding the protocol extension correctly. They all are and everything is working as expected.