Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Mar 25, 2024
1 parent fbc1d03 commit bfdd287
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 46 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
- Facilitation of testing and mocking.

## Usage
The core of the library is the `APIClient` struct, serving both as a request builder and executor. It is a generic struct, enabling use for any task associated with `URLRequest`.
The core of the library is the `APIClient` struct, serving both as a request builder and executor. It is a generic struct, enabling use for any task associated with URL request.

The branching and configuration injection/overriding capabilities of APIClient, extending to all its child instances, facilitate the effortless recycling of networking logic and tasks, thereby eliminating the need for copy-pasting.

Expand Down Expand Up @@ -78,7 +78,7 @@ try await johnClient.delete()

## What is `APIClient`

`APIClient` is a struct combining a closure for creating a URLRequest and a typed dictionary of configurations `APIClient.Configs`. There are two primary ways to extend a `APIClient`:
`APIClient` is a struct combining a closure for creating a URL request and a typed dictionary of configurations `APIClient.Configs`. There are two primary ways to extend a `APIClient`:
- `modifyRequest` modifiers.
- `configs` modifiers.

Expand All @@ -89,7 +89,7 @@ All built-in extensions utilize these modifiers.
## Built-in `APIClient` Extensions
The full list is available [in docs](https://dankinsoid.github.io/swift-api-client/documentation/swiftapiclient/apiclient).
### Request building
Numerous methods exist for modifying a `URLRequest` such as `query`, `body`, `header`, `headers`, `method`, `path`, `timeout`, `cachePolicy`, `body`, `bodyStream` and more.
Numerous methods exist for modifying a URL request such as `query`, `body`, `header`, `headers`, `method`, `path`, `body` and more.
```swift
let client = APIClient(url: baseURL)
.method(.post)
Expand Down Expand Up @@ -258,7 +258,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.0.0")
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.1.0")
],
targets: [
.target(
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftAPIClient/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct APIClient {
private var modifyConfigs: (inout Configs) -> Void = { _ in }

/// Initializes a new network client with a closure that creates a URLRequest.
/// - Parameter createRequest: A closure that takes `Configs` and returns a `URLRequest`.
/// - Parameter createRequest: A closure that takes `Configs` and returns a `HTTPRequest`.
public init(
createRequest: @escaping (Configs) throws -> HTTPRequest
) {
Expand Down Expand Up @@ -98,7 +98,7 @@ public struct APIClient {
}

/// Executes an operation with the current URLRequest and configurations.
/// - Parameter operation: A closure that takes `URLRequest` and `Configs` and returns a generic type `T`.
/// - Parameter operation: A closure that takes an URL request and `Configs` and returns a generic type `T`.
/// - Throws: Rethrows any errors encountered within the closure.
/// - Returns: The result of the closure of type `T`.
public func withRequest<T>(_ operation: (HTTPRequest, Configs) throws -> T) throws -> T {
Expand All @@ -107,15 +107,15 @@ public struct APIClient {
}

/// Asynchronously executes an operation with the current URLRequest and configurations.
/// - Parameter operation: A closure that takes `URLRequest` and `Configs`, and returns a generic type `T`.
/// - Parameter operation: A closure that takes an URL request and `Configs`, and returns a generic type `T`.
/// - Throws: Rethrows any errors encountered within the closure.
/// - Returns: The result of the closure of type `T`.
public func withRequest<T>(_ operation: (HTTPRequest, Configs) async throws -> T) async throws -> T {
let (request, configs) = try createRequest()
return try await operation(request, configs)
}

/// Build `URLRequest`
/// Build `HTTPRequest`
public func request() throws -> HTTPRequest {
try withRequest { request, _ in request }
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftAPIClient/Clients/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import FoundationNetworking
/// A struct representing an HTTP client capable of performing network requests.
public struct HTTPClient {

/// A closure that asynchronously retrieves data and an HTTP response for a given URLRequest and network configurations.
/// A closure that asynchronously retrieves data and an HTTP response for a given URL request and network configurations.
public var data: (HTTPRequest, RequestBody?, APIClient.Configs) async throws -> (Data, HTTPResponse)

/// Initializes a new `HTTPClient` with a custom data retrieval closure.
/// - Parameter data: A closure that takes a `URLRequest` and `APIClient.Configs`, then asynchronously returns `Data` and an `HTTPURLResponse`.
/// - Parameter data: A closure that takes a URL request and `APIClient.Configs`, then asynchronously returns `Data` and an `HTTPURLResponse`.
public init(_ data: @escaping (HTTPRequest, RequestBody?, APIClient.Configs) async throws -> (Data, HTTPResponse)) {
self.data = data
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftAPIClient/Clients/HTTPDownloadClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import FoundationNetworking

public struct HTTPDownloadClient {

/// A closure that asynchronously retrieves data and an HTTP response for a given URLRequest and network configurations.
/// A closure that asynchronously retrieves data and an HTTP response for a given URL request and network configurations.
public var download: (HTTPRequest, APIClient.Configs) async throws -> (URL, HTTPResponse)

/// Initializes a new `HTTPUploadClient` with a custom data retrieval closure.
/// - Parameter data: A closure that takes a `URLRequest` and `APIClient.Configs`, then asynchronously returns `Data` and an `HTTPURLResponse`.
/// Initializes a new `HTTPDownloadClient` with a custom data retrieval closure.
/// - Parameter data: A closure that takes a URL request and `APIClient.Configs`, then asynchronously returns `Data` and an `HTTPURLResponse`.
public init(
_ download: @escaping (HTTPRequest, APIClient.Configs) async throws -> (URL, HTTPResponse))
{
Expand Down
25 changes: 1 addition & 24 deletions Sources/SwiftAPIClient/Clients/NetworkClientCaller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct APIClientCaller<Response, Value, Result> {

/// Performs the network call with the provided request, configurations, and serialization closure.
/// - Parameters:
/// - request: The `URLRequest` for the network call.
/// - request: The URL request for the network call.
/// - configs: The configurations for the network call.
/// - serialize: A closure that serializes the response.
/// - Returns: The result of the network call.
Expand Down Expand Up @@ -232,27 +232,4 @@ public extension APIClient {
throw error
}
}

/// Sets a closure to be executed before making a network call.
///
/// - Parameters:
/// - closure: The closure to be executed before making a network call. It takes in an `inout URLRequest` and `APIClient.Configs` as parameters and can modify the request.
/// - Returns: The `APIClient` instance.
func beforeCall(_ closure: @escaping (inout URLRequest, APIClient.Configs) throws -> Void) -> APIClient {
configs {
let beforeCall = $0.beforeCall
$0.beforeCall = { request, configs in
try beforeCall(&request, configs)
try closure(&request, configs)
}
}
}
}

public extension APIClient.Configs {

var beforeCall: (inout URLRequest, APIClient.Configs) throws -> Void {
get { self[\.beforeCall] ?? { _, _ in } }
set { self[\.beforeCall] = newValue }
}
}
6 changes: 3 additions & 3 deletions Sources/SwiftAPIClient/Modifiers/AuthModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ public extension APIClient {
/// A struct representing an authentication modifier for network requests.
public struct AuthModifier {

/// A closure that modifies a `URLRequest` for authentication.
/// A closure that modifies a URL request for authentication.
public let modifier: (inout HTTPRequest, APIClient.Configs) throws -> Void

/// Initializes a new `AuthModifier` with a custom modifier closure.
/// - Parameter modifier: A closure that modifies a `URLRequest` and `APIClient.Configs` for authentication.
/// - Parameter modifier: A closure that modifies a URL request and `APIClient.Configs` for authentication.
public init(modifier: @escaping (inout HTTPRequest, APIClient.Configs) throws -> Void) {
self.modifier = modifier
}

/// Initializes a new `AuthModifier` with a custom modifier closure.
/// - Parameter modifier: A closure that modifies a `URLRequest` for authentication.
/// - Parameter modifier: A closure that modifies a URL request for authentication.
public init(modifier: @escaping (inout HTTPRequest) throws -> Void) {
self.init { request, _ in
try modifier(&request)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftAPIClient/Modifiers/RequestCompression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import FoundationNetworking

public extension APIClient {

/// Compresses outgoing `URLRequest` bodies using the `deflate` `Content-Encoding` and adds the
/// Compresses outgoing URL request bodies using the `deflate` `Content-Encoding` and adds the
/// appropriate header.
///
/// - Note: Most requests to most APIs are small and so would only be slowed down by applying this adapter. Measure the
Expand Down Expand Up @@ -86,7 +86,7 @@ private func adler32Checksum(of data: Data) -> UInt32 {
}
}

/// Type that determines the action taken when the `URLRequest` already has a `Content-Encoding` header.
/// Type that determines the action taken when the URL request already has a `Content-Encoding` header.
public enum DuplicateHeaderBehavior {

/// Throws a `DuplicateHeaderError`. The default.
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftAPIClient/Modifiers/RequestValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import Foundation
import FoundationNetworking
#endif

/// A struct for validating `URLRequest` instances.
/// A struct for validating URL request instances.
public struct RequestValidator {

/// A closure that validates a `URLRequest`.
/// A closure that validates an URL request.
/// - Throws: An error if validation fails.
public var validate: (_ request: HTTPRequest, _ body: RequestBody?, APIClient.Configs) throws -> Void

/// Initializes a new `RequestValidator` with a custom validation closure.
/// - Parameter validate: A closure that takes a `URLRequest` and throws an error if validation fails.
/// - Parameter validate: A closure that takes an URL request and throws an error if validation fails.
public init(validate: @escaping (_ request: HTTPRequest, _ body: RequestBody?, APIClient.Configs) throws -> Void) {
self.validate = validate
}
Expand All @@ -28,7 +28,7 @@ public extension RequestValidator {
public extension APIClient {

/// Sets a custom request validator for the network client.
/// - Parameter validator: The `RequestValidator` to be used for validating `URLRequest` instances.
/// - Parameter validator: The `RequestValidator` to be used for validating URL request instances.
/// - Returns: An instance of `APIClient` configured with the specified request validator.
func requestValidator(_ validator: RequestValidator) -> APIClient {
httpClientMiddleware(RequestValidatorMiddleware(validator: validator))
Expand Down

0 comments on commit bfdd287

Please sign in to comment.