Bruynet

main

This is the network layer implemented in Swift using Combine framework.
barisuyar/Bruynet

Bruynet

Bruynet is a network layer implemented in Swift using Combine framework.

Explanation

Calling Protocol

We have a protocol which name is 'Calling' and it has a function which is responsible to request and respond it's result.

public protocol Calling {
    func call<T: Decodable>(type: T.Type, endpoint: Endpoint) -> AnyPublisher<T, Error>
}

You can create a mock struct which conforms 'Calling' protocol to be able to write test cases.

Bruynet

Bruynet is a struct that conforms 'Calling' protocol. I should say it is hearth of this Swift Package.

Endpoint

Endpoint is a struct which has your request path, http body object as Data and HttpMethod (get, post..).

You can create a get request by first initilazer.

init(path: String, httpMethod: HTTPMethod = .get)

If you want to create a post request with httpBody, you have to use second initilazer.

init<C>(path: String, body: C? = nil, httpMethod: HTTPMethod = .post) where C: Encodable

BaseURL is the constant base string of your backend. You can assing it at the beginning of application.

URLSession.shared.baseURL = ""

Requestable

Requestable is a protocol which has a property named 'networkProtocol' type of 'Calling'. You will create an struct which conforms 'Requestable' and add your own backend-specific functions.

public struct MemberRequest: Requestable {
    public var networkProtocol: Calling
    
    public init(networkProtocol: Calling = Bruynet()) {
        self.networkProtocol = networkProtocol
    }
}

If you want to use this struct to write test, you can create a struct which conforms 'Calling' then create 'MemberRequest' using that struct, you will be able to use your own mock data.

Installation

You will be able to add this package to your project easily by SPM.

File -> Swift Packages -> Add Package Dependency

and find 'Bruynet' in that huge list then add it to your project.

I recommend to you use latest version.

Meta

Barış UYAR – @prospectchabaris.uyar@hotmail.com

Github

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 18:55:29 GMT-0900 (Hawaii-Aleutian Daylight Time)