Swiftpack.co -  SergeMaslyakov/Network-iOS as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
SergeMaslyakov/Network-iOS
Network framework for iOS
.package(url: "https://github.com/SergeMaslyakov/Network-iOS.git", from: "v1.1")

Network-iOS framework

No dendencies, just a plain URLSession

Swift 5+

Installation

  1. as carthage dependency: add in cartfile github "SergeMaslyakov/Network-iOS"
  2. as submodule in the project: to add in your workspace as a sub-project

How to:

1 - Create an instance of NetworkClient, smth like that


let configuration = NetworkLayer.Configuration(timeout: 10,
                                               baseURL: "https://github.com",
                                               sessionDelegate: sessionDelegateProvider,
                                               authProvider: DefaultAuthorizationProvider(),
                                               responseDecoder: JSONResponseDecoder(),
                                               requestEncoder: JSONResponseEncoder(),
                                               defaultBehaviors: [DebugBehavior(logger: logger)])

let networkClient = NetworkClientImpl(configuration: configuration, 
                                      sessionConfiguration: config.sessionConfiguration)

2 - API Endpoint descriptors, e.g.

struct SignUpEndpoint: EndpointDescriptor {

    private let data: SignUpDataModel
    private let encoder = WWWFormRequestEncoder()

    init(data: SignUpDataModel) {
        self.data = data
    }

    // MARK: - EndpointDescriptor

    let path: String = "auth/sign_up"
    let method: HTTPMethod = .post

    var headers: HTTPHeaders {
        return ["Content-Type": "application/x-www-form-urlencoded"]
    }

    var customEncoder: NetworkRequestEncoding? {
        return encoder
    }

    var params: [String: Any]? {
        return data.toJSON()
    }

    var authRequired: Bool {
        return false
    }

}

3 - Send request (generic or plain request)

let endpoint = SignUpEndpoint(data: data)
var task: URLSessionDataTask?

let completion: ((Result<T, NetworkError>) -> Void) = { result in
    switch result {
    case .success(let model):
        // do smth with model
        print(model)
    case .failure(let error):
        // do smth with error
        print(error)
    }
}

do {
    task = try networkClient.sendRequest(endpoint: endpoint, completion: completion)
} catch {
    // catch validation, coding errors
    print(error)
}

GitHub

link
Stars: 1
Last commit: 4 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Release v1.1
1 year ago
  • min ios version is 11
  • minor fixes and improvements

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