Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marcoincerti/netkit
NetKit is a lightweight Swift networking library built with async/await. It offers modular endpoint definitions, optional caching for GET requests, and configurable retry logic for resilient network calls. Designed for iOS 13+ and macOS, NetKit simplifies integrating robust networking into your apps
https://github.com/marcoincerti/netkit
async-await swift
Last synced: 4 days ago
JSON representation
NetKit is a lightweight Swift networking library built with async/await. It offers modular endpoint definitions, optional caching for GET requests, and configurable retry logic for resilient network calls. Designed for iOS 13+ and macOS, NetKit simplifies integrating robust networking into your apps
- Host: GitHub
- URL: https://github.com/marcoincerti/netkit
- Owner: marcoincerti
- Created: 2025-02-12T11:26:15.000Z (9 days ago)
- Default Branch: main
- Last Pushed: 2025-02-12T11:47:17.000Z (9 days ago)
- Last Synced: 2025-02-12T12:50:47.162Z (9 days ago)
- Topics: async-await, swift
- Language: Swift
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# NetKit
Please open PR to make and improve this library
NetKit is a lightweight Swift networking library built with Swift 5.5+ and async/await. It provides a clean, modular API for performing network requests with features such as built-in caching for GET requests and configurable retry logic for resilient communication. Designed for iOS 13+ and macOS 10.15+, NetKit simplifies networking integration in your Swift projects.## Features
- **Modern Swift Concurrency**: Leverage Swift's async/await for clear, sequential asynchronous code
- **Protocol-Driven Design**: Define type-safe API endpoints using the `Endpoint` protocol
- **Intelligent Caching**: Automatically cache GET request responses with configurable options
- **Resilient Communication**: Built-in retry logic with configurable attempts and delay intervals
- **Highly Testable**: Easily inject custom `URLSession` instances for testing and configuration
- **Type-Safe Responses**: Automatic decoding of JSON responses into your Swift models## Requirements
- Swift 5.5+
- iOS 13.0+ / macOS 10.15+
- Xcode 13.0+## Installation
### Swift Package Manager
Add NetKit to your Xcode project:
1. Select **File** → **Add Packages...**
2. Enter the package URL: `https://github.com/marcoincerti/netKit.git`
3. Choose your preferred version or branch
4. Click **Add Package**Alternatively, add NetKit as a dependency in your `Package.swift`:
```swift
dependencies: [
.package(url: "https://github.com/marcoincerti/netKit.git", from: "1.0.0")
]
```## Usage
### Create model and request if needed
```swift
// Example model for a user profile.
public struct Profile: Codable, Identifiable {
public let id: String
public let name: String
public let email: String
// Add other properties as needed.
}// Example model for updating a profile.
public struct ProfileUpdateRequest: Codable {
public let name: String?
public let email: String?
public init(name: String? = nil, email: String? = nil) {
self.name = name
self.email = email
}
}
```### Defining an Endpoint
Create type-safe API endpoints using the `Endpoint` protocol:
```swift
import NetKitpublic enum ProfileService: Endpoint {
case getProfile(userID: String)
case updateProfile(userID: String, update: ProfileUpdateRequest)public var baseURL: URL {
// Replace with your actual base URL.
return URL(string: "https://api.example.com")!
}
public var path: String {
switch self {
case .getProfile(let userID),
.updateProfile(let userID, _):
return "profiles/\(userID)"
}
}
public var method: HTTPMethod {
switch self {
case .getProfile:
return .GET
case .updateProfile:
return .PUT
}
}
public var queryItems: [URLQueryItem]? {
// Return query items if needed. For this example, none are required.
return nil
}
public var headers: [String: String]? {
switch self {
case .updateProfile:
// Specify that the request body is JSON.
return ["Content-Type": "application/json"]
default:
return nil
}
}
public var body: Data? {
switch self {
case .updateProfile(_, let update):
return try? JSONEncoder().encode(update)
default:
return nil
}
}
}
```### Making Network Requests
#### Decoded Response
Fetch and automatically decode JSON responses into Swift models:
```swift
public func fetchProfile(userID: String) async throws -> Profile {
let endpoint = ProfileService.getProfile(userID: userID)
let profile: Profile = try await request(endpoint: endpoint)
return profile
}
/// Updates the profile for the specified user.
public func updateProfile(userID: String, with update: ProfileUpdateRequest) async throws -> Profile {
let endpoint = ProfileService.updateProfile(userID: userID, update: update)
let updatedProfile: Profile = try await request(endpoint: endpoint)
return updatedProfile
}
```
## Advanced Usage### Custom URLSession Configuration
```swift
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30
configuration.waitsForConnectivity = truelet customSession = URLSession(configuration: configuration)
let service = BaseNetworkService(session: customSession)
```### Customizing Cache Behavior
```swift
let service = BaseNetworkService()
service.isCachingEnabled = true
service.cachePolicy = .reloadIgnoringLocalCacheData
```## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.