Skip to content

Simple lightweight networking layer written on top of URLSession. This will provide a clean, separate layer for networking.

License

Notifications You must be signed in to change notification settings

AnbalaganD/EagleNet

Repository files navigation

This library aims to provide a simple and elegant approach to writing network requests. It's designed to be lightweight, adding a thin layer on top of URLSession without excessive engineering overhead.

Our primary objectives are:

  • Lightweight networking library: Keep the library small and efficient.
  • Easy to maintain: Prioritize clear code and modular design for maintainability.
  • Beginner friendly: Make the library accessible to developers of all levels.
  • Well documented: Provide comprehensive documentation to guide usage.
  • Customizable and testable: Allow for flexibility and ensure code quality through testing.

Currently, this library supports basic HTTP data requests (GET, POST, PUT, DELETE) and includes a small file upload feature using multipart/form-data. These capabilities address the majority of network communication needs in most applications.

For detailed information on feature status, please refer to the Roadmap file

Swift Package manager (SPM)

EagleNet is available through SPM. Use below URL to add as a dependency

dependencies: [
    .package(url: "https://github.com/AnbalaganD/EagleNet", .upToNextMajor(from: "1.0.5"))
]

Making a GET Request

import EagleNet

struct User: Decodable {
    let id: Int
    let name: String
    let email: String
}

// Basic GET request
let user: User = try await EagleNet.networkService.get(
    url: "https://api.example.com/users/1"
)

Making a POST Request

struct CreateUser: Encodable {
    let name: String
    let email: String
}

struct UserResponse: Decodable {
    let id: Int
    let name: String
}

let newUser = CreateUser(name: "Anbalagan D", email: "[email protected]")
let response: UserResponse = try await EagleNet.networkService.post(
    url: "https://api.example.com/users",
    body: newUser
)

File Upload

let imageData = // ... your image data ...
let response: UploadResponse = try await EagleNet.networkService.upload(
    url: "https://api.example.com/upload",
    parameters: [
        .file(
            key: "avatar",
            fileName: "profile.jpg",
            data: imageData,
            mimeType: .jpegImage
        ),
        .text(key: "username", value: "Anbu")
    ],
    progress: { bytesTransferred, totalBytes in
        let progress = Float(bytesTransferred) / Float(totalBytes)
        print("Upload progress: \(Int(progress * 100))%")
    }
)

Request Interceptors

struct AuthInterceptor: RequestInterceptor {
    let token: String
    
    func modify(request: URLRequest) async throws -> URLRequest {
        var modifiedRequest = request
        modifiedRequest.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
        return modifiedRequest
    }
}

// Add interceptor to network service
EagleNet.networkService.addRequestInterceptor(
    AuthInterceptor(token: "your-auth-token")
)

Response Interceptor

struct LoggingInterceptor: ResponseInterceptor {
    func modify(data: Data, urlResponse: URLResponse) async throws -> (Data, URLResponse) {
        if let httpResponse = urlResponse as? HTTPURLResponse {
            print("Response Status Code: \(httpResponse.statusCode)")
            print("Response Headers: \(httpResponse.allHeaderFields)")
            
            if let responseString = String(data: data, encoding: .utf8) {
                print("Response Body: \(responseString)")
            }
        }
        return (data, urlResponse)
    }
}

// Add response interceptor to network service
EagleNet.networkService.addResponseInterceptor(LoggingInterceptor())

Author

Anbalagan D

License

EagleNet is available under the MIT license. See the LICENSE file for more info.

About

Simple lightweight networking layer written on top of URLSession. This will provide a clean, separate layer for networking.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages