Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ on:
jobs:
test-swift:
name: Test Swift Code
runs-on: macos-14
runs-on: macos-15
steps:
- name: Configure Xcode
uses: maxim-lobanov/setup-xcode@v1
Expand Down Expand Up @@ -51,7 +51,7 @@ jobs:

lint-code:
name: Lint Swift Code
runs-on: macos-14
runs-on: macos-15
steps:
- name: Configure Xcode
uses: maxim-lobanov/setup-xcode@v1
Expand All @@ -67,8 +67,8 @@ jobs:
restore-keys: |
${{ runner.os }}-spm-
- name: Install SwiftLint
run: brew install swift-format peripheryapp/periphery/periphery
run: brew install peripheryapp/periphery/periphery
- name: Lint code
run: Scripts/lint-swift-code
- name: Scan for dead code
run: periphery scan --strict --config config/periphery.yml
run: periphery scan --config config/periphery.yml --relative-results --format github-actions
7 changes: 4 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.10
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

// swift-format-ignore-file
Expand Down Expand Up @@ -41,6 +41,10 @@ let package = Package(
),
.testTarget(
name: "HPNetworkTests",
dependencies: ["HPNetwork"]
),
.testTarget(
name: "HPNetworkMockTests",
dependencies: ["HPNetwork", "HPNetworkMock"]
),
]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

## Installation

Starting with v4 HPNetwork is only available via Swift Package Manager.
`HPNetwork` is available via Swift Package Manager:

- Package.swift: `.package(url: "https://github.com/henrik-dmg/HPNetwork", from: "4.0.0")`
- Package.swift: `.package(url: "https://github.com/henrik-dmg/HPNetwork", from: "5.0.0")`
- Xcode: `https://github.com/henrik-dmg/HPNetwork`

## Documentation
Expand Down
2 changes: 1 addition & 1 deletion Scripts/format-swift-code
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

swift-format format \
xcrun swift-format format \
--recursive \
--parallel \
--in-place \
Expand Down
2 changes: 1 addition & 1 deletion Scripts/lint-swift-code
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

swift-format lint \
xcrun swift-format lint \
--recursive \
--parallel \
--strict \
Expand Down
2 changes: 1 addition & 1 deletion Sources/HPNetwork/Authorization/Authorization.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

/// A type that specifies authorization for a network request.
public protocol Authorization {
public protocol Authorization: Sendable {

/// The value that the `Authorization` header-field will be set to.
var headerString: String { get }
Expand Down
71 changes: 0 additions & 71 deletions Sources/HPNetwork/ConnectionMonitor.swift

This file was deleted.

29 changes: 15 additions & 14 deletions Sources/HPNetwork/Documentation.docc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ A flexible, protocol-based networking stack written in Swift.

## Installation

Starting with v4 HPNetwork is only available via Swift Package Manager.
`HPNetwork` is available via Swift Package Manager:

- Package.swift: `.package(url: "https://github.com/henrik-dmg/HPNetwork", from: "4.0.0")`
- Package.swift: `.package(url: "https://github.com/henrik-dmg/HPNetwork", from: "5.0.0")`
- Xcode: `https://github.com/henrik-dmg/HPNetwork`

## Scheduling Requests
Expand All @@ -21,7 +21,7 @@ Scheduling a request is as easy as this:
let response = try await request.response()
```

The `response` is a ``NetworkResponse`` containing the output and statisticsof the request.
The `response` is a ``NetworkResponse`` containing the output and statistics of the request.

You can also get an async result:

Expand All @@ -42,7 +42,7 @@ let task = request.schedule { result in
}
```

You can also use pretty much the same API with ``NetworkClient`` (useful for being able to mock requests in tests).
You can also use pretty much use the same API with ``NetworkClient`` (useful for being able to mock requests in tests).

## Creating Requests

Expand All @@ -56,15 +56,15 @@ In the most simple terms, that means you supply a `URL` and a request method.
```swift
struct BasicDataRequest: DataRequest {

typealias Output = Data
typealias Output = Data

var requestMethod: HTTPRequest.Method {
.get
}
var requestMethod: HTTPRequest.Method {
.get
}

func makeURL() throws -> URL {
// construct your URL here
}
func makeURL() throws -> URL {
// construct your URL here
}

}
```
Expand All @@ -86,8 +86,8 @@ struct BasicDataRequest: DataRequest {
}

let basicRequest = BasicDataRequest(
url: URL(string: "https://panhans.dev/"),
requestMethod: .get
url: URL(string: "https://panhans.dev/"),
requestMethod: .get
)
```

Expand Down Expand Up @@ -120,4 +120,5 @@ struct BasicDecodableRequest<Output: Decodable>: DecodableRequest {
### Request Authorization

To add authorization to a request, simply supply a ``Authorization`` instance to your request.
You can either use ``BasicAuthorization`` for basic authentication with a username and password, or ``BearerAuthorization`` for bearer token authorization or implement you own custom ``Authorization`` type.
You can either use ``BasicAuthorization`` for basic authentication with a username and password,
or ``BearerAuthorization`` for bearer token authorization or implement you own custom ``Authorization`` type.
40 changes: 24 additions & 16 deletions Sources/HPNetwork/Header Fields/HTTPFieldBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@ import HTTPTypes
@resultBuilder
public enum HTTPFieldBuilder {

public static func buildBlock(_ components: [HTTPField]...) -> [HTTPField] {
components.flatMap { $0 }
// MARK: - Expression

public static func buildExpression(_ expression: HTTPField) -> [HTTPField] {
[expression]
}

public static func buildOptional(_ component: [HTTPField]?) -> [HTTPField] {
component ?? []
public static func buildExpression(_ expression: HTTPField?) -> [HTTPField] {
expression.flatMap { [$0] } ?? []
}

public static func buildOptional(_ component: [HTTPField?]?) -> [HTTPField] {
component?.compactMap { $0 } ?? []
public static func buildExpression(_ expression: [HTTPField]) -> [HTTPField] {
expression
}

/// Add support for both single and collections of constraints.
public static func buildExpression(_ expression: HTTPField) -> [HTTPField] {
[expression]
// MARK: - Optional

public static func buildOptional(_ component: [HTTPField]?) -> [HTTPField] {
component ?? []
}

public static func buildExpression(_ expression: [HTTPField]) -> [HTTPField] {
expression
// MARK: - Limited Availability

public static func buildLimitedAvailability(_ component: [HTTPField]) -> [HTTPField] {
component
}

/// Add support for if statements.
// MARK: - Branching

public static func buildEither(first components: [HTTPField]) -> [HTTPField] {
components
}
Expand All @@ -34,12 +40,14 @@ public enum HTTPFieldBuilder {
components
}

public static func buildArray(_ components: [[HTTPField]]) -> [HTTPField] {
components.flatMap { $0 }
// MARK: - Partial

public static func buildPartialBlock(first components: [HTTPField]) -> [HTTPField] {
components
}

public static func buildLimitedAvailability(_ component: [HTTPField]) -> [HTTPField] {
component
public static func buildPartialBlock(accumulated: [HTTPField], next: [HTTPField]) -> [HTTPField] {
accumulated + next
}

}
20 changes: 8 additions & 12 deletions Sources/HPNetwork/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ public protocol NetworkClientProtocol {
func result<Request: NetworkRequest>(
_ request: Request,
delegate: (any URLSessionTaskDelegate)?
) async -> Request.RequestResult
) async -> Request.NetworkResult

func schedule<Request: NetworkRequest>(
_ request: Request,
delegate: (any URLSessionTaskDelegate)?,
finishingQueue: DispatchQueue,
completion: @escaping (Request.RequestResult) -> Void
) -> Task<Void, Never>
delegate: (any URLSessionTaskDelegate)?
) -> Request.NetworkTask

}

Expand All @@ -44,17 +42,15 @@ public final class NetworkClient: NetworkClientProtocol {
public func result<Request: NetworkRequest>(
_ request: Request,
delegate: (any URLSessionTaskDelegate)? = nil
) async -> Request.RequestResult {
) async -> Request.NetworkResult {
await request.result(urlSession: urlSession, delegate: delegate)
}

public func schedule<Request>(
public func schedule<Request: NetworkRequest>(
_ request: Request,
delegate: (any URLSessionTaskDelegate)? = nil,
finishingQueue: DispatchQueue = .main,
completion: @escaping (Request.RequestResult) -> Void
) -> Task<Void, Never> where Request: NetworkRequest {
request.schedule(urlSession: urlSession, delegate: delegate, finishingQueue: finishingQueue, completion: completion)
delegate: (any URLSessionTaskDelegate)? = nil
) -> Request.NetworkTask where Request.Output: Sendable {
request.schedule(urlSession: urlSession, delegate: delegate)
}

}
Loading