-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from dirtyhenry/docs
Document and demo Transport
- Loading branch information
Showing
5 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
Examples/BlocksApp/BlocksApp/Features/TransportDemoView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import Blocks | ||
import SwiftUI | ||
|
||
class DummyJSONClient { | ||
static let defaultTransport: Transport = RetryTransport( | ||
wrapping: StatusCodeCheckingTransport( | ||
wrapping: LoggingTransport( | ||
wrapping: URLSession.shared, | ||
subsystem: "net.mickf.blocks" | ||
) | ||
) | ||
) | ||
|
||
enum RandomQuote { | ||
struct Response: Codable { | ||
let id: Int | ||
let quote: String | ||
let author: String | ||
} | ||
} | ||
|
||
func randomQuote() async throws -> Endpoint<RandomQuote.Response> { | ||
let url = try URL.dummyJSONURLComponents(path: "/quotes/random") | ||
return Endpoint(json: .get, url: url) | ||
} | ||
} | ||
|
||
extension URL { | ||
static func dummyJSONURLComponents(path: String) throws -> URL { | ||
var urlComponents = URLComponents() | ||
urlComponents.scheme = "https" | ||
urlComponents.host = "dummyjson.com" | ||
urlComponents.path = path | ||
|
||
guard let url = urlComponents.url else { | ||
throw TransportError.unmetURLComponentsRequirements | ||
} | ||
|
||
return url | ||
} | ||
} | ||
|
||
struct TransportDemoView: View { | ||
let readme: LocalizedStringKey = """ | ||
A `Transport` can compose behaviors dealing with URL requests. | ||
The one demoed here will: | ||
1. **Load data** from the internet (`URLSession.shared`); | ||
2. **Log requests** and responses to OSLog (`LoggingTransport`); | ||
3. **Throw** if the status code is an error one (`StatusCodeCheckingTransport`); | ||
4. **Retry** the request if it failed a couple of times (`RetryTransport`). | ||
""" | ||
|
||
@State var taskState: TaskState = .notStarted | ||
@State var quote: String? | ||
@State var author: String? | ||
|
||
var body: some View { | ||
VStack(spacing: 32) { | ||
VStack(alignment: .leading) { | ||
Text(readme) | ||
}.frame(maxWidth: .infinity, alignment: .leading) | ||
Divider() | ||
TaskStateButton( | ||
"Load quote", | ||
runningTitleKey: "Loading quote…", | ||
action: { | ||
Task { | ||
do { | ||
taskState = .running | ||
let quoteResponse = try await DummyJSONClient.defaultTransport.load( | ||
DummyJSONClient().randomQuote() | ||
) | ||
taskState = .completed | ||
quote = quoteResponse.quote | ||
author = quoteResponse.author | ||
} catch { | ||
taskState = .failed(errorDescription: error.localizedDescription) | ||
} | ||
} | ||
}, | ||
disabledWhenCompleted: false, | ||
state: taskState | ||
) | ||
.buttonStyle(.borderedProminent) | ||
.padding(32) | ||
VStack { | ||
Text(quote ?? "") | ||
Text(author ?? "") | ||
.font(.caption) | ||
} | ||
Spacer() | ||
} | ||
.padding() | ||
.navigationTitle("Transport Demo") | ||
} | ||
} | ||
|
||
#Preview { | ||
TransportDemoView() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters