
Netti is a modular, type-safe Swift networking library built on top of Alamofire. It aims to streamline network operations with a consistent and protocol-oriented approach, utilizing modern Swift features like async/await and Swift concurrency.
- ✅ Protocol-oriented networking architecture
- ✅ Support for multiple HTTP methods (GET, POST, PUT, DELETE, etc.)
- ✅ Type-safe response parsing with
Decodable
- ✅ Configurable headers, timeout, and encoding strategies
- ✅ Alamofire-powered request execution with SSL pinning support
- ✅ Built-in JSON encoding/decoding strategies (e.g., snake_case conversion)
- ✅ Network monitoring via
NWPathMonitor
- ✅ Mock data support for testing
- iOS 14.0+ / macOS 11+ / tvOS 14+ / watchOS 7+ / macCatalyst 14+
- Swift 6.1+
- Alamofire
Use Swift Package Manager:
// swift-tools-version:6.1
import PackageDescription
let package = Package(
name: "YourPackageName",
dependencies: [
.package(url: "https://github.com/Stof83/Netti.git", from: "1.0.0")
],
targets: [
.target(
name: "YourTargetName",
dependencies: [
.product(name: "Netti", package: "Netti")
]
)
]
)
enum MyAPIs {
case users
}
extension MyAPIs: HTTPRequest {
var baseURL: URL? { URL(string: "https://api.example.com") }
var basePath: String { "v1" }
var path: String {
switch self {
case .users: "users"
}
}
var headers: HTTPHeaders { ["Authorization": "Bearer token"] }
}
struct UserRequest: Encodable {
let id: Int
}
struct UserResponse: Decodable {
let id: Int
let name: String
let email: String
}
let netti = Netti(service: MyNetworkService()) // Inject your own implementation
Task {
do {
let userRequest = UserRequest(id: 42)
let response: HTTPResponse<UserResponse> = try await netti.send(
MyAPIs.users,
parameters: userRequest,
method: .post
)
// Success
let user = response.data
print("User name: \(user.name), email: \(user.email)")
print("Status code: \(response.response?.statusCode ?? -1)")
} catch HTTPRequestError.requestFailed(let error) {
print("Network error: \(error)")
} catch HTTPRequestError.decodingFailed(let error) {
print("Decoding failed: \(error)")
} catch {
print("Unexpected error: \(error)")
}
}
Defines a request structure, including:
baseURL
,basePath
, andpath
- Headers and timeout
- Method to convert into
URLRequest
Wrap server responses including:
- Raw request and response
- Data or file URL
- Error if any
Handles JSON encoding/decoding with customizable key strategies and date formats.
Observes real-time network connectivity changes using Combine/SwiftUI.
Protocol defining the network layer.
Alamofire-based implementation of NetworkService
. Handles:
- Request encoding
- Response validation
- Caching and SSL pinning
This SDK is provided under the MIT License. See LICENSE for details. Feel free to use, modify, and distribute it as per the terms of the license.
Contributions are welcome! Please open issues and submit pull requests for features, fixes, or improvements.