-
Notifications
You must be signed in to change notification settings - Fork 6
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
Resource-specific properties set by resource service subclass #67
base: main
Are you sure you want to change the base?
Conversation
…specific properties of the request
Codecov Report
@@ Coverage Diff @@
## master #67 +/- ##
==========================================
+ Coverage 86.12% 86.28% +0.15%
==========================================
Files 22 22
Lines 346 350 +4
Branches 30 30
==========================================
+ Hits 298 302 +4
Misses 48 48 |
@@ -54,14 +74,20 @@ class SubclassedNetworkJSONResourceServiceTests: XCTestCase { //swiftlint:disabl | |||
let resourceHTTPHeaderFields = [commonKey: "resource"] | |||
mockResource = MockHTTPHeaderFieldsNetworkDataResource(url: mockURL, httpHeaderFields: resourceHTTPHeaderFields) | |||
testService.fetch(resource: mockResource) { _ in } | |||
XCTAssertEqual(mockSession.capturedRequest!.allHTTPHeaderFields!, resourceHTTPHeaderFields) | |||
let expectedHeaderFields = [commonKey: "resource", resourceNameKey: mockResourceName] | |||
XCTAssertEqual(mockSession.capturedRequest!.allHTTPHeaderFields!, expectedHeaderFields) |
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.
If you wanted to avoid the force unwraps here you could use optional chaining with a default value?
mockSession.capturedRequest?.allHTTPHeaderFields ?? [:]
should work? It'll still fail but won't crash the tests
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.
good point @KaneCheshire. i just modified what was already there. didn't even notice the force unwraps.
- returns: A URLRequest or nil if the construction of the request failed | ||
*/ | ||
func urlRequest() -> URLRequest? | ||
|
||
func urlRequest(with: [URLQueryItem]) -> URLRequest? |
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.
can you also please include additionalQueryParameters
in the function signature as the parameter name just to go w/ the comment above.
🛠 Description & Reasoning
Adding a
resource
argument to the "additional" functions of the resource service - the functions that are intended to be overridden by subclasses.Providing the subclass with the resource gives that subclass an opportunity to set parameters of the URL request based on the resource. This is different to the resource providing those parameters itself, e.g. the resource could implement the
httpHeaderFields
andqueryItems
properties.Consider a subclass that adds the requirement that its resources must conform to a protocol
GraphQLResouce
. TheGraphQLResouce
protocol requires a propertyqueryName: String
to be implemented by its conformers.The
NetworkDataResourceService
can now add in thequeryName
to either the HTTP header fields or the URL query items. It would be unreliable to expect all resources to implement thequeryItems
andhttpHeaderFields
to add thequeryName
key/value.This PR modifies the
NetworkDataResourceService.additionalHeaderFields
function by adding an argument to the function. This function is overridden (not invoked explicitly), which is why we cannot provide a default argument value. For this reason, this is an API-breaking change.✨ Other Changes
The addition of the
NetworkDataResourceService.additionalQueryParameters
function. This serves the same purpose as theadditionalHeaderFields
function, but for URL query parameters.