Skip to content

hummasyousuf/NetworkingKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NetworkingKit

A lightweight, modular, and powerful Swift networking library built using async/await, Combine, and Alamofire. Supports GET, POST, and multipart requests with built-in error handling.


πŸš€ Features

  • βœ… Async/Await Support
  • βœ… Combine Publisher Support
  • βœ… Multipart Upload (Image, Video, PDF, etc.)
  • βœ… Custom Error Handling
  • βœ… Easy to extend
  • βœ… Built with Swift 5 and Alamofire

πŸ“¦ Installation

Swift Package Manager (SPM)

Go to Xcode > File > Add Packages and add the following URL:

https://github.com/hummasyousuf/NetworkingKit

πŸ“² Usage

1. Import the Package

import NetworkingKit
import Combine

2. SwiftUI Injection

@StateObject var viewModel: ContentViewModel
private var cancellable = Set<AnyCancellable>()

init() {
    let networking = Networking(baseURLString: "<your-baseurl>")
    _viewModel = StateObject(wrappedValue: ContentViewModel(networking: networking))
}

3. UIKit Injection

var viewModel: ContentViewModel
private var cancellable = Set<AnyCancellable>()

init() {
    let networking = Networking(baseURLString: "<your-baseurl>")
    viewModel = ContentViewModel(networking: networking)
    super.init(nibName: nil, bundle: nil)
}

4. GET & POST (Async/Await)

func getUsers() async {
    do {
        let model = try await networking.get(
            endPoint: "<your-endpoint>",
            headers: [:],
            query: [:],
            modelType: [YourModel].self
        )
        print("Model:", model)
    } catch {
        print("Error:", error.localizedDescription)
    }
}

func createPost() async {
    let params: [String: Any] = [
        "title": "Developer",
        "body": "iOS Developer",
        "userId": 1
    ]
    
    do {
        let model = try await networking.post(
            endPoint: "<your-endpoint>",
            headers: [:],
            params: params,
            modelType: YourModel.self
        )
        print("Model:", model)
    } catch {
        print("Error:", error.localizedDescription)
    }
}

5. GET & POST (Combine)

func fetchAllPosts(_ query: [String: Any]) {
    getPosts(query)
        .receive(on: DispatchQueue.main)
        .sink { completion in
            switch completion {
            case .failure(let error):
                print("Error: \(error.localizedDescription)")
            case .finished:
                break
            }
        } receiveValue: { model in
            print("Model:", model)
        }
        .store(in: &cancellable)
}

func getPosts(_ query: [String: Any]) -> AnyPublisher<[YourModel], NetworkError> {
    networking.get(
        endPoint: "<your-endpoint>",
        headers: [:],
        query: query,
        modelType: [YourModel].self
    )
}

func createPost() {
    let params: [String: Any] = [
        "title": "Developer",
        "body": "iOS Developer",
        "userId": 1
    ]
    
    networking.post(
        endPoint: "<your-endpoint>",
        params: params,
        headers: [:],
        modelType: YourModel.self
    )
    .receive(on: DispatchQueue.main)
    .sink { completion in
        switch completion {
        case .failure(let error):
            print("Error: \(error.localizedDescription)")
        case .finished:
            break
        }
    } receiveValue: { model in
        print("Model:", model)
    }
    .store(in: &cancellable)
}

6. Multipart Upload (Image, Video, PDF, etc.)

func uploadMedia(type: DOCUMENTTYPE, fileURL: URL) {
    do {
        let fileData = try Data(contentsOf: fileURL)
        let postData = PostData(value: fileData, type: type)
        
        let params: [String: Any] = ["file": postData]
        
        networking.multipart(
            endPoint: "<your-endpoint>",
            method: .post,
            parameters: params,
            header: [:]
        ) { progress in
            print("Upload Progress:", progress.fractionCompleted)
        }
        .sink { completion in
            if case .failure(let error) = completion {
                print("Upload failed:", error.localizedDescription)
            }
        } receiveValue: { data, response in
            if let httpResponse = response as? HTTPURLResponse,
               (200...299).contains(httpResponse.statusCode),
               let data = data {
                do {
                    let model = try JSONDecoder().decode(YourModel.self, from: data)
                    print("Model:", model)
                } catch {
                    print("Error:", error.localizedDescription)
                }
            } else {
                print("Invalid upload response")
            }
        }
        .store(in: &cancellables)
        
    } catch {
        print("File read failed:", error.localizedDescription)
    }
}

πŸ“„ License

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages