Skip to content

Commit

Permalink
chore: Format
Browse files Browse the repository at this point in the history
  • Loading branch information
danthorpe committed Oct 20, 2023
1 parent f16c058 commit ecd490f
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 51 deletions.
3 changes: 2 additions & 1 deletion Sources/Helpers/AsyncSequence+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ extension AsyncSequence {
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AsyncSequence {
public func first(beforeTimeout duration: Duration, using clock: any Clock<Duration>) async throws
-> Element {
-> Element
{
try await first(beforeTimeout: duration, using: clock, where: { _ in true })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Helpers

extension NetworkingComponent {
public func authenticated<Delegate: AuthenticationDelegate>(with delegate: Delegate)
-> some NetworkingComponent {
-> some NetworkingComponent
{
checkedStatusCode().modified(Authentication(delegate: delegate))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ extension AuthenticationMethod {
}

public struct BearerCredentials: Hashable, Sendable, Codable, HTTPRequestDataOption,
AuthenticatingCredentials {
AuthenticatingCredentials
{
public static let method: AuthenticationMethod = .bearer
public static let defaultOption: Self? = nil

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public struct HeaderBasedAuthentication<Delegate: AuthenticationDelegate> {
}

private func performCredentialFetch(for request: HTTPRequestData) async throws -> Credentials {
logger?.info(
"🔐 Fetching credentials for \(Credentials.method.rawValue, privacy: .public) authorization method"
)
logger?
.info(
"🔐 Fetching credentials for \(Credentials.method.rawValue, privacy: .public) authorization method"
)
do {
let credentials = try await delegate.fetch(for: request)
set(state: .authorized(credentials))
Expand All @@ -49,7 +50,8 @@ public struct HeaderBasedAuthentication<Delegate: AuthenticationDelegate> {
}

func refresh(unauthorized credentials: Credentials, from response: HTTPResponseData)
async throws -> Credentials {
async throws -> Credentials
{
if case let .fetching(task) = state {
return try await task.value
}
Expand All @@ -65,9 +67,10 @@ public struct HeaderBasedAuthentication<Delegate: AuthenticationDelegate> {
unauthorized credentials: Credentials,
from response: HTTPResponseData
) async throws -> Credentials {
logger?.info(
"🔑 Refreshing credentials for \(Credentials.method.rawValue, privacy: .public) authorization method"
)
logger?
.info(
"🔑 Refreshing credentials for \(Credentials.method.rawValue, privacy: .public) authorization method"
)
do {
let refreshed = try await delegate.refresh(unauthorized: credentials, from: response)
set(state: .authorized(refreshed))
Expand Down
2 changes: 1 addition & 1 deletion Sources/Networking/Components/Metrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public struct NetworkingInstrumentClient {

extension [ElapsedTimeMeasurement] {
var total: Duration {
return map(\.duration).reduce(.zero, +)
map(\.duration).reduce(.zero, +)
}
}

Expand Down
7 changes: 3 additions & 4 deletions Sources/Networking/Components/Retry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ actor RetryData {
func send(upstream: NetworkingComponent, request: HTTPRequestData) -> ResponseStream<
HTTPResponseData
> {
return ResponseStream { continuation in
ResponseStream { continuation in
Task {
var progress = BytesReceived()
do {
Expand Down Expand Up @@ -216,8 +216,7 @@ public protocol RetryingStrategy {
}

public struct BackoffRetryStrategy: RetryingStrategy {
private var block:
(HTTPRequestData, [Result<HTTPResponseData, Error>], Date, Calendar) -> Duration?
private var block: (HTTPRequestData, [Result<HTTPResponseData, Error>], Date, Calendar) -> Duration?
public init(
block: @escaping (HTTPRequestData, [Result<HTTPResponseData, Error>], Date, Calendar) ->
Duration?
Expand All @@ -241,7 +240,7 @@ public struct BackoffRetryStrategy: RetryingStrategy {
let rate: Double = 2.0
return .init { _, attempts, _, _ in
guard attempts.count < maxAttemptCount else { return nil }
let delay: Double = (interval * pow(rate, Double(attempts.count))) + .random(in: 0...0.001)
let delay: Double = (interval * pow(rate, Double(attempts.count))) + .random(in: 0 ... 0.001)
return min(.seconds(delay), maxDelay)
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Networking/Core/ActiveRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public actor ActiveRequests {
await self.removeStream(for: request)
})
}
}.shared()
}
.shared()

active[Key(id: request.id)] = Value(request: request, stream: shared)
return shared
Expand Down
8 changes: 4 additions & 4 deletions Sources/Networking/Core/HTTPRequestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ extension HTTPRequestData: Equatable {
&& lhs.body == rhs.body
&& lhs.request == rhs.request
&& lhs.options.allSatisfy { key, lhs in
return lhs.isEqualTo(rhs.options[key]?.value)
lhs.isEqualTo(rhs.options[key]?.value)
}
&& rhs.options.allSatisfy { key, rhs in
return rhs.isEqualTo(lhs.options[key]?.value)
rhs.isEqualTo(lhs.options[key]?.value)
}
}
}
Expand All @@ -161,10 +161,10 @@ public func ~= (lhs: HTTPRequestData, rhs: HTTPRequestData) -> Bool {
lhs.body == rhs.body
&& (lhs.request == rhs.request)
&& lhs.options.allSatisfy { key, lhs in
return lhs.isEqualTo(rhs.options[key]?.value)
lhs.isEqualTo(rhs.options[key]?.value)
}
&& rhs.options.allSatisfy { key, rhs in
return rhs.isEqualTo(lhs.options[key]?.value)
rhs.isEqualTo(lhs.options[key]?.value)
}
}

Expand Down
11 changes: 6 additions & 5 deletions Sources/Networking/Core/HTTPResponseData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public struct HTTPResponseData: Sendable {

extension HTTPResponseData {
public subscript<Metadata: HTTPResponseMetadata>(metadata metadataType: Metadata.Type)
-> Metadata.Value {
-> Metadata.Value
{
get {
let id = ObjectIdentifier(metadataType)
guard let container = metadata[id], let value = container.value as? Metadata.Value else {
Expand Down Expand Up @@ -86,10 +87,10 @@ extension HTTPResponseData: Equatable {
&& lhs.data == rhs.data
&& lhs._response == rhs._response
&& lhs.metadata.allSatisfy { key, lhs in
return lhs.isEqualTo(rhs.metadata[key]?.value)
lhs.isEqualTo(rhs.metadata[key]?.value)
}
&& rhs.metadata.allSatisfy { key, rhs in
return rhs.isEqualTo(lhs.metadata[key]?.value)
rhs.isEqualTo(lhs.metadata[key]?.value)
}
}
}
Expand All @@ -111,9 +112,9 @@ extension HTTPResponseData: CustomDebugStringConvertible {
if let contentType = self.headerFields[.contentType] {
debugDescription += "\(contentType.description)"
#if hasFeature(BareSlashRegexLiterals)
let regex = /(json)/
let regex = /(json)/
#else
let regex = #/(json)/#
let regex = #/(json)/#
#endif
if contentType.contains(regex) {
let dataDescription = String(decoding: data, as: UTF8.self)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Networking/Core/NetworkingModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ extension NetworkingComponent {
}

private struct Modified<Upstream: NetworkingComponent, Modifier: NetworkingModifier>:
NetworkingComponent {
NetworkingComponent
{
let upstream: Upstream
let modifier: Modifier
init(upstream: Upstream, modifier: Modifier) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/TestSupport/StubbedResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public struct StubbedResponseStream: Equatable, Sendable {
continuation.finish()
case .throwing:
let bytesReceived = bytes.expected / 4
for _ in 0..<2 {
for _ in 0 ..< 2 {
await clock.advance(by: .seconds(2))
bytes.receiveBytes(count: bytesReceived)
continuation.yield(.progress(bytes))
Expand All @@ -55,7 +55,7 @@ public struct StubbedResponseStream: Equatable, Sendable {

case let .uniform(steps: steps, interval: interval):
let bytesReceived = bytes.expected / Int64(steps)
for _ in 0..<steps {
for _ in 0 ..< steps {
await clock.advance(by: interval)
bytes.receiveBytes(count: bytesReceived)
continuation.yield(.progress(bytes))
Expand Down
3 changes: 2 additions & 1 deletion Sources/TestSupport/TestAuthenticationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Protected
import XCTestDynamicOverlay

public final class TestAuthenticationDelegate<Credentials: AuthenticatingCredentials>: @unchecked
Sendable {
Sendable
{
public typealias Fetch = @Sendable (HTTPRequestData) async throws -> Credentials
public typealias Refresh = @Sendable (Credentials, HTTPResponseData) async throws -> Credentials

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ final class AuthenticationTests: XCTestCase {
try await withMainSerialExecutor {
try await withThrowingTaskGroup(of: HTTPResponseData.self) { group in

for _ in 0..<4 {
for _ in 0 ..< 4 {
group.addTask {
return try await network.data(copy)
try await network.data(copy)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class DuplicatesRemovedTests: XCTestCase {
.duplicatesRemoved()

try await withThrowingTaskGroup(of: HTTPResponseData.self) { group in
for _ in 0..<4 {
for _ in 0 ..< 4 {
group.addTask {
try await network.data(request1)
}
Expand Down
39 changes: 21 additions & 18 deletions Tests/NetworkingTests/Components/RetryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,29 @@ final class RetryTests: XCTestCase {
let request = HTTPRequestData(id: .init("1"), authority: "example.com")
XCTAssertTrue(request.supportsRetryingRequests)
let strategy = request.retryingStrategy
var delay = await strategy?.retryDelay(
request: request,
after: [.failure("Some Error")],
date: Date(),
calendar: .current
)
var delay = await strategy?
.retryDelay(
request: request,
after: [.failure("Some Error")],
date: Date(),
calendar: .current
)
XCTAssertEqual(delay, .seconds(3))
delay = await strategy?.retryDelay(
request: request,
after: [.failure("Some Error"), .failure("Some Error")],
date: Date(),
calendar: .current
)
delay = await strategy?
.retryDelay(
request: request,
after: [.failure("Some Error"), .failure("Some Error")],
date: Date(),
calendar: .current
)
XCTAssertEqual(delay, .seconds(3))
delay = await strategy?.retryDelay(
request: request,
after: [.failure("Some Error"), .failure("Some Error"), .failure("Some Error")],
date: Date(),
calendar: .current
)
delay = await strategy?
.retryDelay(
request: request,
after: [.failure("Some Error"), .failure("Some Error"), .failure("Some Error")],
date: Date(),
calendar: .current
)
XCTAssertNil(delay)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetworkingTests/Components/ThrottledTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class ThrottledTests: XCTestCase {
.throttled(max: 5)

try await withThrowingTaskGroup(of: HTTPResponseData.self) { group in
for _ in 0..<100 {
for _ in 0 ..< 100 {
group.addTask {
try await network.data(HTTPRequestData(authority: "example.com"))
}
Expand Down

0 comments on commit ecd490f

Please sign in to comment.