Skip to content

Commit 19c4c61

Browse files
authored
docs: update README.md (#86)
1 parent a1c6652 commit 19c4c61

1 file changed

Lines changed: 104 additions & 2 deletions

File tree

README.md

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ Typhoon is a modern, lightweight Swift framework that provides elegant and robus
2121
🎯 **Type-Safe** - Leverages Swift's type system for compile-time safety
2222
🔧 **Configurable** - Flexible retry parameters for any use case
2323
📱 **Cross-Platform** - Works on iOS, macOS, tvOS, watchOS, and visionOS
24-
**Lightweight** - Minimal footprint with zero dependencies
24+
**Lightweight** - Minimal footprint with zero dependencies
25+
🧾 **Pluggable Logging** – Integrates with OSLog or custom loggers
26+
🌐 **URLSession Integration** – Retry network requests with a single parameter
2527
🧪 **Well Tested** - Comprehensive test coverage
2628

2729
## Table of Contents
@@ -34,6 +36,10 @@ Typhoon is a modern, lightweight Swift framework that provides elegant and robus
3436
- [Constant Strategy](#constant-strategy)
3537
- [Exponential Strategy](#exponential-strategy)
3638
- [Exponential with Jitter Strategy](#exponential-with-jitter-strategy)
39+
- [Custom Strategy](#custom-strategy)
40+
- [Chain Strategy](#chain-strategy)
41+
- [Logging](#logging)
42+
- [URLSession Integration](urlsession-integration)
3743
- [Common Use Cases](#common-use-cases)
3844
- [Communication](#communication)
3945
- [Documentation](#documentation)
@@ -95,7 +101,7 @@ do {
95101

96102
### Retry Strategies
97103

98-
Typhoon provides three powerful retry strategies to handle different failure scenarios:
104+
Typhoon provides six powerful retry strategies to handle different failure scenarios:
99105

100106
```swift
101107
/// A retry strategy with a constant number of attempts and fixed duration between retries.
@@ -120,6 +126,15 @@ case exponential(
120126
case custom(retry: UInt, strategy: IRetryDelayStrategy)
121127
```
122128

129+
Additionally, Typhoon allows composing multiple retry strategies into a single policy using a chained strategy:
130+
131+
```
132+
RetryPolicyStrategy.chain([
133+
.constant(retry: 2, dispatchDuration: .seconds(1)),
134+
.exponential(retry: 3, dispatchDuration: .seconds(2))
135+
])
136+
```
137+
123138
### Constant Strategy
124139

125140
Best for scenarios where you want predictable, fixed delays between retries:
@@ -315,6 +330,93 @@ The total retry count is calculated automatically from the sum of all entries
315330

316331
Each strategy in the chain uses **local indexing**, meaning every phase starts its delay calculation from zero. This ensures each strategy behaves predictably regardless of its position in the chain.
317332

333+
## Logging
334+
335+
Typhoon provides a lightweight logging abstraction that allows you to integrate retry diagnostics into your existing logging system.
336+
337+
The framework defines a simple ILogger protocol:
338+
339+
```
340+
public protocol ILogger: Sendable {
341+
func info(_ message: String)
342+
func warning(_ message: String)
343+
func error(_ message: String)
344+
}
345+
```
346+
347+
You can plug in any logging framework by implementing this protocol.
348+
349+
### Using Apple's OSLog
350+
351+
Typhoon includes built-in support for Apple's OSLog system via Logger:
352+
353+
```
354+
import Typhoon
355+
import OSLog
356+
357+
let logger = Logger(subsystem: "com.example.network", category: "retry")
358+
359+
let retryService = RetryPolicyService(
360+
strategy: .exponential(retry: 3, dispatchDuration: .seconds(1)),
361+
logger: logger
362+
)
363+
```
364+
365+
All retry attempts, failures, and final errors will be reported through the provided logger.
366+
367+
You can also integrate third-party loggers like SwiftLog or custom analytics systems.
368+
369+
## URLSession Integration
370+
371+
Typhoon provides built-in integration with URLSession, allowing you to apply retry policies directly to network requests with minimal boilerplate.
372+
373+
Instead of wrapping network calls manually, you can call retry-enabled methods directly on URLSession.
374+
375+
### Fetch Data with Retry
376+
377+
```
378+
import Typhoon
379+
380+
let (data, response) = try await URLSession.shared.data(
381+
from: URL(string: "https://api.example.com/users")!,
382+
retryPolicy: .exponential(
383+
retry: 3,
384+
jitterFactor: 0.1,
385+
dispatchDuration: .seconds(1)
386+
)
387+
)
388+
```
389+
390+
### Using URLRequest
391+
392+
```
393+
var request = URLRequest(url: URL(string: "https://api.example.com/users")!)
394+
request.httpMethod = "GET"
395+
396+
let (data, response) = try await URLSession.shared.data(
397+
for: request,
398+
retryPolicy: .constant(retry: 3, dispatchDuration: .seconds(1))
399+
)
400+
```
401+
402+
### Upload Requests
403+
404+
```
405+
let (data, response) = try await URLSession.shared.upload(
406+
for: request,
407+
from: bodyData,
408+
retryPolicy: .exponential(retry: 3, dispatchDuration: .seconds(1))
409+
)
410+
```
411+
412+
### Download Requests
413+
414+
```
415+
let (fileURL, response) = try await URLSession.shared.download(
416+
for: request,
417+
retryPolicy: .exponential(retry: 4, dispatchDuration: .seconds(2))
418+
)
419+
```
318420

319421
## Common Use Cases
320422

0 commit comments

Comments
 (0)