A Swift package that makes network requests easier and more maintainable in your iOS, macOS, and other Apple platform applications. It provides a type-safe, clean API with powerful features like environment switching, authentication handling, and JSON mapping.
- β¨ Type-safe API endpoints using Swift enums
- π Built-in environment switching (live, test, preview)
- π Optional authentication handling
- πΊοΈ Flexible JSON mapping and response processing
- π Comprehensive logging for debugging
- πͺ Full async/await support
Here's how easy it is to define and use API endpoints with Networking:
enum MyEndpoint {
case getUser(userId: String)
case updateProfile(name: String, email: String)
case createPost(threadId: String, post: Post)
}
extension MyEndpoint: Endpoint {
var baseURL: URL { URL(string: "https://your-api.com/api/v1")! }
var path: String {
switch self {
case let .getUser(userId): "/users/\(userId)"
case .updateProfile: "/users/me"
case .createPost: "/posts"
}
}
var method: HttpMethod {
switch self {
case .getUser: .get
case .updateProfile: .put
case .createPost: .post
}
}
var task: HttpTask {
switch self {
case .getUser:
return .none
case let .updateProfile(name, email):
return .rawBody([
"user_name": name,
"email_address": email
])
case let .createPost(threadId, post):
return .encodableBodyAndQuery(
body: post,
queryParameters: ["thread": threadId]
)
}
}
}
// Making requests
let controller = NetworkingController<MyEndpoint, MyError>()
do {
let user: User = try await controller.request(.getUser(userId: "123"))
// Handle the user data
} catch {
// Handle MyError
}
Add Networking to your project via Swift Package Manager:
-
In Xcode, go to File > Swift Packages > Add Package Dependency.
-
Enter the repository URL:
https://github.com/telemtobi/swift-networking.git
-
Select your preferred version and finish.
Easily switch between different environments:
// Configure with different environments
let controller = NetworkingController<MyEndpoint, MyError>(
environment: .live // For production
// or .test // For unit testing
// or .preview // For SwiftUI previews
)
// Works great with PointFree's Dependencies package
extension MyApiClient: DependencyKey {
static let liveValue = MyApiClient(environment: .live)
static let testValue = MyApiClient(environment: .test)
static let previewValue = MyApiClient(environment: .preview)
}
Integrate your authentication provider:
class MyAuthenticator: Authenticator {
var state: AuthenticationState { .reachable }
func authenticate() async throws -> Bool {
// Your authentication logic
return true
}
func mapRequest(_ request: inout URLRequest) {
// Add auth headers, tokens, etc.
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
}
let controller = NetworkingController<MyEndpoint, MyError>(
authenticator: MyAuthenticator()
)
Transform API responses before decoding:
struct User: Decodable, JsonMapper {
let id: String
let name: String
static func map(_ data: Data) -> Data {
// Transform response data if needed
return data
}
}
Configure logging per endpoint or globally:
// Per endpoint
extension MyEndpoint: Endpoint {
var shouldPrintLogs: Bool {
switch self {
case .sensitiveData: false
default: true
}
}
}
// Global configuration
Networking.DebugConfiguration.shouldPrintLogs = true
- Swift 5.9 or later
- Xcode 15.0 or later
- iOS 13.0 / macOS 10.15 / tvOS 13.0 / watchOS 6.0 or later
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.