Skip to content

Commit beb8bd2

Browse files
committed
Docs
1 parent 51b25da commit beb8bd2

21 files changed

+297
-85
lines changed

.github/workflows/docs.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Documentation
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
env:
9+
SWIFT_VERSION: 5.6
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout Package
16+
uses: actions/checkout@v2
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Download Swift
21+
run: wget -q https://download.swift.org/swift-$SWIFT_VERSION-release/ubuntu2004/swift-$SWIFT_VERSION-RELEASE/swift-$SWIFT_VERSION-RELEASE-ubuntu20.04.tar.gz
22+
- name: Extract Swift
23+
run: tar xzf swift-$SWIFT_VERSION-RELEASE-ubuntu20.04.tar.gz
24+
- name: Add Swift toolchain to PATH
25+
run: |
26+
echo "$GITHUB_WORKSPACE/swift-$SWIFT_VERSION-RELEASE-ubuntu20.04/usr/bin" >> $GITHUB_PATH
27+
28+
- name: Build documentation
29+
run: >
30+
swift package --allow-writing-to-directory docs \
31+
generate-documentation \
32+
--target AsyncHTTP \
33+
--disable-indexing \
34+
--transform-for-static-hosting \
35+
--hosting-base-path /AsyncHTTP \
36+
--output-path docs
37+
38+
- name: Publish documentation to GitHub Pages
39+
uses: JamesIves/github-pages-deploy-action@4.1.7
40+
with:
41+
branch: gh-pages
42+
folder: docs
43+
git-config-name: László Teveli
44+
git-config-email: tevelee@gmail.com
45+

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/Packages
55
*.xcodeproj
66
xcuserdata/
7-
.swiftpm
7+
.swiftpm
8+
/docs

Package.resolved

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"pins" : [
3+
{
4+
"identity" : "swift-docc-plugin",
5+
"kind" : "remoteSourceControl",
6+
"location" : "https://github.com/apple/swift-docc-plugin",
7+
"state" : {
8+
"revision" : "3303b164430d9a7055ba484c8ead67a52f7b74f6",
9+
"version" : "1.0.0"
10+
}
11+
}
12+
],
13+
"version" : 2
14+
}

Package.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.6
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
@@ -12,7 +12,9 @@ let package = Package(
1212
products: [
1313
.library(name: "AsyncHTTP", targets: ["AsyncHTTP"]),
1414
],
15-
dependencies: [],
15+
dependencies: [
16+
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
17+
],
1618
targets: [
1719
.target(name: "AsyncHTTP", dependencies: []),
1820
.testTarget(name: "AsyncHTTPTests", dependencies: ["AsyncHTTP"]),

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ let request = try HTTPRequest().configured { request in
1515
request.retryStrategy = .immediately(maximumNumberOfAttempts: 3)
1616
}
1717

18-
let loader: some HTTPLoader = URLSession.shared.http
18+
let loader: some HTTPLoader = URLSession.shared.httpLoader()
1919
.applyServerEnvironment()
2020
.applyTimeout(default: 30)
2121
.applyRetryStrategy()
2222
.deduplicate()
2323
.throttle(maximumNumberOfRequests: 2)
2424

2525
let response: HTTPResponse = try await loader.load(request)
26-
let body: MyResponseStruct = response.jsonBody()
26+
let body: MyResponseStruct = try response.jsonBody()
2727

2828
print(request.formatted())
2929
print(response.formatted())
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# ``AsyncHTTP``
2+
3+
Generic Swift networking library with async/await
4+
5+
## Overview
6+
7+
AsyncHTTP
8+
9+
## Topics
10+
11+
### Requests
12+
13+
- ``HTTPRequest``
14+
- ``HTTPRequestOption``
15+
- ``HTTPRequestBody``
16+
17+
### Loading Requests
18+
19+
- ``Loader``
20+
- ``Loaders``
21+
- ``AnyLoader``
22+
- ``HTTPLoader``
23+
- ``CompositeLoader``
24+
- ``HTTPResponse``
25+
26+
### HTTP
27+
28+
- ``HTTPMethod``
29+
- ``HTTPHeader``
30+
- ``AuthorizationHeader``
31+
- ``HTTPStatus``
32+
- ``HTTPVersion``
33+
- ``MIMEType``
34+
- ``URIScheme``
35+
36+
### Timeout
37+
38+
- ``Timeout``
39+
- ``TimeoutOption``
40+
41+
### Retry Strategy
42+
43+
- ``RetryStrategy``
44+
- ``RetryStrategyWrapper``
45+
- ``RetryStrategyOption``
46+
- ``Backoff``
47+
48+
### Server Environment
49+
50+
- ``ServerEnvironment``
51+
- ``ServerEnvironmentOption``
52+
53+
### Identifying Requests
54+
55+
- ``HTTPRequestIdentifier``
56+
- ``HTTPRequestIdentifierOption``
57+
58+
### Formatting
59+
60+
- ``Formatter``
61+
- ``CURLHTTPRequestFormatter``
62+
- ``StandardHTTPRequestFormatter``
63+
- ``StandardHTTPResponseFormatter``
64+
- ``Formattible``
65+
- ``DefaultFormattible``
66+
67+
### Utils
68+
69+
- ``Converted``
70+
- ``ConversionStrategy``
71+
- ``TwoWayConversionStrategy``
72+
- ``DecoderStrategy``
73+
- ``EncoderStrategy``
74+
- ``HTTPFormattible``

Sources/AsyncHTTP/Loader.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import Foundation
2+
#if canImport(FoundationNetworking)
3+
import FoundationNetworking
4+
#endif
25

36
public protocol Loader {
47
associatedtype Input

Sources/AsyncHTTP/Loaders/Decode.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#if canImport(Combine)
12
import Combine
3+
#endif
24
import Foundation
35

46
extension Loader where Output: Decodable {

Sources/AsyncHTTP/Loaders/HTTP.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Foundation
2+
#if canImport(FoundationNetworking)
3+
import FoundationNetworking
4+
#endif
25

36
extension Loader where Input == URLRequest, Output == (Data, URLResponse) {
4-
public func http(modify: ((HTTPRequest, inout URLRequest) -> Void)? = nil) -> Loaders.HTTP<Self> { .init(loader: self, modify: modify) }
7+
public func httpLoader(modify: ((HTTPRequest, inout URLRequest) -> Void)? = nil) -> Loaders.HTTP<Self> { .init(loader: self, modify: modify) }
58
}
69

710
public protocol HTTPLoader: Loader where Input == HTTPRequest, Output == HTTPResponse {}

Sources/AsyncHTTP/Loaders/URLSession.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
import Foundation
2+
#if canImport(FoundationNetworking)
3+
import FoundationNetworking
24

35
extension URLSession {
4-
public var data: Loaders.URLSessionData {
6+
func data(for urlRequest: URLRequest) async throws -> (Data, URLResponse) {
7+
try await withCheckedThrowingContinuation { continuation in
8+
let task = dataTask(with: urlRequest) { data, response, error in
9+
guard let data = data, let response = response else {
10+
let error = error ?? URLError(.badServerResponse)
11+
return continuation.resume(throwing: error)
12+
}
13+
continuation.resume(returning: (data, response))
14+
}
15+
task.resume()
16+
}
17+
}
18+
}
19+
#endif
20+
21+
extension URLSession {
22+
public var dataLoader: Loaders.URLSessionData {
523
.init(urlSession: self)
624
}
725

8-
public var http: Loaders.HTTP<Loaders.URLSessionData> {
9-
data.http()
26+
public var httpLoader: Loaders.HTTP<Loaders.URLSessionData> {
27+
dataLoader.httpLoader()
1028
}
1129
}
1230

Sources/AsyncHTTP/Model/HTTPRequest.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import Foundation
2+
#if canImport(FoundationNetworking)
3+
import FoundationNetworking
4+
#endif
25

36
public struct HTTPRequest: Equatable, Hashable, Sendable {
47
public var method: HTTPMethod

Sources/AsyncHTTP/Model/HTTPRequestBody.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#if canImport(Combine)
12
import Combine
3+
#endif
24
import Foundation
35

46
public struct HTTPRequestBody: Equatable, Hashable, Sendable {

Sources/AsyncHTTP/Model/HTTPResponse.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
#if canImport(Combine)
12
import Combine
3+
#endif
24
import Foundation
5+
#if canImport(FoundationNetworking)
6+
import FoundationNetworking
7+
#endif
38

49
public struct HTTPResponse {
510
public let request: HTTPRequest

Sources/AsyncHTTP/Model/HTTPStatus.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import Foundation
2+
#if canImport(FoundationNetworking)
3+
import FoundationNetworking
4+
#endif
25

36
public struct HTTPStatus: Equatable, Hashable, Codable {
47
public let code: Int

Sources/AsyncHTTP/Plugins/IdentifyRequests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,19 @@ extension HTTPRequest: Identifiable {
2020
}
2121

2222
extension Loader {
23-
public func identifyRequests(generateIdentifier: @escaping (HTTPRequest) -> String = generateUUID) -> Loaders.ApplyRequestIdentity<Self> where Input == HTTPRequest {
23+
public func identifyRequests(generateIdentifier: @escaping (HTTPRequest) -> String = { _ in UUID().uuidString }) -> Loaders.ApplyRequestIdentity<Self> where Input == HTTPRequest {
2424
.init(loader: self, generateIdentifier: generateIdentifier)
2525
}
2626
}
2727

28-
public let generateUUID: (HTTPRequest) -> String = { _ in UUID().uuidString }
2928

3029
extension Loaders {
3130
public struct ApplyRequestIdentity<Wrapped: Loader>: CompositeLoader where Wrapped.Input == HTTPRequest {
32-
3331
private let loader: Wrapped
3432
private let generateIdentifier: (HTTPRequest) -> String
3533

3634
init(loader: Wrapped,
37-
generateIdentifier: @escaping (HTTPRequest) -> String = generateUUID) {
35+
generateIdentifier: @escaping (HTTPRequest) -> String = { _ in UUID().uuidString }) {
3836
self.loader = loader
3937
self.generateIdentifier = generateIdentifier
4038
}

Sources/AsyncHTTP/Plugins/Retry.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ public enum RetryStrategyOption: HTTPRequestOption {
1010

1111
public struct RetryStrategyWrapper: Hashable {
1212
let wrapped: RetryStrategy
13+
private var objectID: ObjectIdentifier {
14+
ObjectIdentifier(type(of: wrapped))
15+
}
1316

1417
public func hash(into hasher: inout Hasher) {
15-
hasher.combine(ObjectIdentifier(type(of: wrapped)))
18+
hasher.combine(objectID)
1619
}
1720

1821
public static func == (lhs: RetryStrategyWrapper, rhs: RetryStrategyWrapper) -> Bool {
19-
ObjectIdentifier(lhs.wrapped) == ObjectIdentifier(lhs.wrapped)
22+
lhs.objectID == rhs.objectID
2023
}
2124
}
2225

Sources/AsyncHTTP/Utils/Combine.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Foundation
2+
3+
#if !canImport(Combine)
4+
public protocol TopLevelDecoder {
5+
associatedtype Input
6+
7+
func decode<T: Decodable>(_ type: T.Type, from: Input) throws -> T
8+
}
9+
10+
extension JSONDecoder: TopLevelDecoder {
11+
public typealias Input = Data
12+
}
13+
14+
public protocol TopLevelEncoder {
15+
associatedtype Output
16+
17+
func encode<T: Encodable>(_ value: T) throws -> Output
18+
}
19+
20+
extension JSONEncoder: TopLevelEncoder {
21+
public typealias Output = Data
22+
}
23+
#endif

0 commit comments

Comments
 (0)