Skip to content

Commit 201ba12

Browse files
authored
Add usage documentation to readme (#12)
1 parent e1c54a7 commit 201ba12

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Title/IssueLink
2+
3+
## Summary
4+
5+
Please summarize the change that was made and why it was made.
6+
7+
8+
#### Characteristics
9+
10+
- [ ] Feature completed
11+
- [ ] Dark code
12+
13+
14+
#### Tests
15+
16+
- [ ] Tests are included in this PR
17+
- [ ] Tests were not included in this PR because ____
18+
19+
20+
#### Build/Tests
21+
22+
- [ ] Builds and runs
23+
- [ ] Unit tests pass

README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# HTTPEngine
2-
![Swift](https://github.com/JZDesign/HTTPEngine/workflows/Swift/badge.svg) [![SPM compatible](https://img.shields.io/badge/SPM-compatible-e66f20.svg?style=flat)](https://github.com/apple/swift-package-manager) [![Docs](https://img.shields.io/badge/Jazzy-Documentation-634fb3.svg?style=flat)](https://jzdesign.github.io/HTTPEngine/)
2+
![Swift](https://github.com/JZDesign/HTTPEngine/workflows/Swift/badge.svg) [![SPM compatible](https://img.shields.io/badge/SPM-Compatible-e66f20.svg?style=flat)](https://github.com/apple/swift-package-manager) [![Docs](https://img.shields.io/badge/Jazzy-Documentation-634fb3.svg?style=flat)](https://jzdesign.github.io/HTTPEngine/) [![License](https://img.shields.io/badge/License-MIT-335577.svg?style=flat)](https://github.com/JZDesign/HTTPEngine/blob/master/LICENSE)
33

44
A convenience wrapper around Swift's Combine and URLSession to make `URLRequests`
55

@@ -13,3 +13,64 @@ dependencies: [
1313
## [View Documentation](https://jzdesign.github.io/HTTPEngine/)
1414

1515
Documentation generated by [Jazzy](https://github.com/realm/jazzy).
16+
17+
18+
## Usage
19+
### Get and decode
20+
```swift
21+
struct Recipes: Codable {
22+
let id: String
23+
let imageURLs: [String]
24+
let title: String
25+
let ingredients: [Ingredient]
26+
let steps: [Step]
27+
}
28+
29+
let engine = HTTPEngine()
30+
31+
engine
32+
.get([Recipes].self, url: "https://my-recipes.com/baby-back-ribs")
33+
.assertNoFailure() // don't do this
34+
.sink { recipes in
35+
}
36+
37+
```
38+
39+
### Post with encode and decode
40+
41+
```swift
42+
struct NewUser: Codable {
43+
let userName, email, password: String
44+
}
45+
46+
struct NewUserResponse: Codable {
47+
let id, accessToken, scope: String
48+
}
49+
50+
let newUser = NewUser(userName: "Dudemus", email: "mydude1@electronmail.com", password: "This_R3@LLy_5h0uld_b3_encrypt3d")
51+
let engine = HTTPEngine()
52+
53+
engine
54+
.post(NewUserResponse.self, url: "https://auth.somedomain.com", body: newUser, validator: { $0 == 202 })
55+
.catch {
56+
// handle non 202 response or other errors
57+
}
58+
.assign(to: \.user, on: UserStore)
59+
60+
```
61+
62+
### Standard Requests
63+
64+
```swift
65+
let engine = HTTPEngine()
66+
67+
engine
68+
.makeRequest(method: .delete, url: "https://not-google.com/", body: data, header: headers, validator: { $0 == 204 })
69+
.catch {
70+
// handle non 204 response or other errors
71+
}
72+
.sink { data in
73+
// handle response data
74+
}
75+
76+
```

Sources/HTTPEngine/Errors.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public struct Errors {
3737
/// - Returns: Error?
3838
///
3939
/// ```swift
40+
/// code ~= 200...299 -> nil // Success!
4041
/// code ~= 300...399 -> Errors.Response.redirect(statusCode)
4142
/// code ~= 400 -> Errors.Response.ClientError.badRequest_400
4243
/// code ~= 401 -> Errors.Response.ClientError.invalidCredentials_401

Sources/HTTPEngine/Utilities.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ infix operator ??? : TernaryPrecedence
3939
/// - right: Error
4040
/// - Throws: The error from the right
4141
/// - Returns: The unwrapped optional from the left
42+
///
43+
/// ```swift
44+
/// var x: Int? = nil
45+
/// let y = try x ??? SomeError() // Throws some Error
46+
///
47+
/// var value: Int? = 1
48+
/// let z = try value ??? SomeError() // unwraps value
49+
/// ```
50+
///
4251
public func ???<T>(_ left: Optional<T>, right: Error) throws -> T {
4352
guard let value = left else { throw right }
4453
return value

Tests/HTTPEngineTests/HTTPEngineTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ final class HTTPEngineTests: XCTestCase {
7272
}
7373

7474
HTTPEngine()
75-
.makeRequest(method: .get, url: "https://google.com", validator: { $0 == 202 })
75+
.makeRequest(method: .get, url: "https://google.com", validator: { $0 == 202 })
7676
.assertError(test: self) {
7777
switch $0 {
7878
case Errors.Response.unexpectedStatusCode(let response):

0 commit comments

Comments
 (0)