Swiftpack.co - amattit/Networking as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by amattit.
amattit/Networking 1.0.8
My Networking
⭐️ 0
🕓 33 weeks ago
iOS macOS
.package(url: "https://github.com/amattit/Networking.git", from: "1.0.8")

Networking

Сетевая библиотека для домашнего использования.

Работа с пакетом

Установка

Добавьте зависимость в своем проекте на этот пакет

dependencies: [
    .package(url: "https://github.com/amattit/Networking", from: "1.0.0")
],

Использование

Опишите API, например в формате enum

enum API {
    enum User {
        case byId(UUID)
    }
}

Подпишите API под протокол APICall

extension API.User: APICall {
    
    private var encoder: JSONEncoder { JSONEncoder() }
    
    var path: String {
        switch self {
        case .byId(let id):
            return "/v1/users/\(id)"
        }
    }
    
    var method: String {
        switch self {
        
        case .byId:
            return "GET"
        }
    }
    
    var headers: [String : String]? {
        return [
            "Content-Type":"application/json",
            "rqUid":UUID().description,
        ]
    }
    
    var query: [String : String]? {
        return nil
    }
    
    func body() throws -> Data? {
        return nil
    }
}

Для совершения вызова создайте в своем проекте репозиторий, например UserRepository и добавьте нужные функции и подпишите его под протокол WebRepository

import Networking

struct UserRepository: WebRepository {
    var session: URLSession = .shared
    var baseURL = "https://somehost.ru"
    var queue: DispatchQueue = DispatchQueue(label: "web_repository_queue")
    
    func fetchUserBy(id: UUID) -> AnyPublisher<API.User.UUserResponse, Error> {
        call(endpoint: API.User.byId(id))
    }
}

Реализуйте обработку паблишера

public func fetchUserBy(_ id: UUID) {
    userRepository.fetchUserBy(id: id)
        .map { $0 }
        .sink(receiveCompletion: check(_:)) { (response) in
            self.user = User(with: response.user)
        }
        .store(in: &store)
}

GitHub

link
Stars: 0
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

fix platforms in SPM
1 year ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics