Skip to content

An intuitive Swift networking library for seamless, scalable, and maintainable API integration πŸš€

License

Notifications You must be signed in to change notification settings

TelemTobi/swift-networking

Repository files navigation

Swift Networking

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.

Swift SPM License

Features

  • ✨ 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

Quick Example

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
}

Installation

Swift Package Manager

Add Networking to your project via Swift Package Manager:

  1. In Xcode, go to File > Swift Packages > Add Package Dependency.

  2. Enter the repository URL:

    https://github.com/telemtobi/swift-networking.git
    
  3. Select your preferred version and finish.

Usage

Environment Management

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)
}

Authentication

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()
)

JSON Mapping

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
    }
}

Logging Control

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

Requirements

  • Swift 5.9 or later
  • Xcode 15.0 or later
  • iOS 13.0 / macOS 10.15 / tvOS 13.0 / watchOS 6.0 or later

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

An intuitive Swift networking library for seamless, scalable, and maintainable API integration πŸš€

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages