Skip to content

Commit

Permalink
Overhaul and deprotocolize Network 🛰 (#218)
Browse files Browse the repository at this point in the history
As it was, our `NetworkStack` and it's main conforming type
`URLSessionNetworkStack` were not straightforward to use, mostly
because of the many different `Resource` protocols and respective
associated types. All these `Resource` protocols, while being broken
into simple, focused and composable units, created lots of complexity
due to the generic constraints they impose.

While initially designed for allowing diverse `NetworkStack`'s to be
modeled, we have observed that most of the times users are simply
interested in using a `URLSessionNetworkStack`.

The `NetworkStore` suffered from similar issues, by being tied to our
huge `Resource` protocol family, while its main idea (and sole
implementation) was a store that orchestrated both a network and
persistence stacks.

Finally, `PersistenceStack` received a minor clean up and improvement
to make it more flexible and powerful.

With all these things in mind, our `Resource` "family" was rethought
to be composed of multiple witnesses, instead of multiple protocols.
Witnesses provide the same abstraction of modeling behavior, without
some of the limitations and complexities of protocols. By being generic
concrete types, they also allow creating default implementations via
static properties and functions, making code reuse very simple.

The `NetworkStack` protocol was kept but greatly simplified and the
`NetworkStore` protocol was discontinuated in favor of a
`StackOrchestratorStore`, also simplified while making its purpose
clearer.

## Changes

- Deprecate all `Resource` protocols.

- Create new `BaseRequestMaking` witness to replace
`BaseRequestResource`.

- Create new `ModelDecoding` witness to replace `DecodableResource`.

- Create new `ErrorDecoding` witness to replace
`ExternalErrorDecoderResource`.

- Deprecate `RequestAuthenticator` in favor of
`URLRequestAuthenticator`, providing a simpler API.

- Deprecate `RequestInterceptor` in favour of a more powerful
`URLSessionResourceInterceptor` defined at a Resource level, allowing
much more flexibility and control. Add default conformances to
`URLRequestAuthenticator`, `Network.URLSessionRetryPolicy`.

- Improve `Retry` infrastructure:

  + Create new `Retry.State` to model the retry state of an arbitrary
operation, update `Policy.shouldRetry()` to use it.

  + Create `Retry.Action.CompareClosure` to compare actions.

  + Create `Retry.Action.mostPrioritary` helper static function, with
the default logic to pick the most prioritary action between two.

  + Rename `Retry.Policy.retries` to `maxRetries`.

  + Rename `Retry.Policy.Backoff.Truncation.retries` to `maxRetries`.

  + Rename `Retry.Policy.Backoff.Truncation.delay` to `maxDelay`.

- Improve `HTTPResourceEndpoint`:

  + Convert `body` property into a throwable function, to allow
handling enconding errors.

  + Convert `request` computed property into a throwable function, to
propagate body encoding errors.

- Rename `Network.URLSessionNetworkStack.Error` to
`Network.URLSessionError` and improve it:

  + Remove `URLResponse` associated value from `.url` and `.retry`

  + Use new `Retry.State`

- Remove `Network.URLSessionNetworkStack.Configuration`

- Update `NetworkStack` to require just a `Resource` and `FetchError`
associated types, and add protocol extension to perform decoding after a
network fetch.

- Improve `PersistenceStack` to have a new `Key: Hashable`
`associatedtype` instead of the hardcoded `Persistence.Key`, and
renamed existing `Remote` to `Payload`.

- Deprecate `NetworkStore` in favor of `StackOrchestratorStore`, which
requires a `NetworkStack` and `PersistenceStack associated types. Add a
protocol extension to perform decoding after a fetch.

- Create new `StackOrchestrator` namespace.

- Rename `NetworkPersistableStore` to `StackOrchestrator.Store`, plus
some internal adjustments.

- Rename `NetworkStorePerformanceMetricsTracker` to
`StackOrchestratorPerformanceMetricsTracker`.

- Update `podspec`.

- Add some new rule exceptions on `.swiftlint.yml`

- Create `FetchAndDecodeError`.

- Update Network documentation and playground
  • Loading branch information
p4checo authored Sep 30, 2020
1 parent 538f770 commit 812a8ab
Show file tree
Hide file tree
Showing 82 changed files with 4,545 additions and 4,025 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ disabled_rules:
- cyclomatic_complexity
- identifier_name
- colon
- opening_brace
opt_in_rules:
- empty_count
- closure_spacing
Expand Down
Loading

0 comments on commit 812a8ab

Please sign in to comment.