Giter Site home page Giter Site logo

ahmetkgunay / networklayer Goto Github PK

View Code? Open in Web Editor NEW
16.0 4.0 0.0 121 KB

Protocol Oriented Generic Network Layer example with Alamofire and Swift4

License: MIT License

Swift 94.20% Ruby 0.06% Objective-C 0.42% Shell 5.32%
swift4 generic-programming generic-functions generics alamofire protocol-oriented network networking

networklayer's Introduction

NetworkLayer

Language MIT License Twitter: @ahmtgny

Protocol Oriented Generic Network Layer example with Alamofire and Swift4

Usage

Usage is so simple:

Just create Request Class and implement the Requestable protocol, in example project i created UserRequest to fetch user info from Github API

Requestable protocol will force you to implement some usefull functions as shown below.

final class UserRequest: Requestable {

    typealias ResponseType = UserResponse

    private var userName : String

    init(userName:String) {
        self.userName = userName
    }
    
    var baseUrl: URL {
        return  URL(string: "https://api.github.com/")!
    }

    var endpoint: String {
        return "users/\(self.userName)"
    }

    var method: Network.Method {
        return .get
    }

    var query: Network.QueryType {
        return .path
    }

    var parameters: [String : Any]? {
        return nil
    }

    var headers: [String : String]? {
        return defaultJSONHeader
    }

    var timeout: TimeInterval {
        return 30.0
    }

    var cachePolicy: NSURLRequest.CachePolicy {
        return .reloadIgnoringLocalAndRemoteCacheData
    }
}

Meanwhile you should create also responseModel and implement the Codable protocol your model. Example project shows UserResponse as shown below:

And Thats all! You do not have to decode the response. Network class does it for you.. :)

public struct UserResponse: Codable {

    public let userName: String?
    public let userId: Int?
    public let avatarUrl: String?

    enum CodingKeys: String, CodingKey {
        case userName = "login", userId = "id", avatarUrl = "avatar_url"
    }
}

Request from ViewController

    override func viewDidLoad() {
        super.viewDidLoad()

        // Assign request instance to cancel in future
        // Just call cancel() to assigned instance
        _ = Network.request(req: UserRequest(userName: "ahmetkgunay")) { (result) in

            switch result {
                case .success(let userResponse):
                    print(userResponse)
                case .cancel(let cancelError):
                    print(cancelError!)
                case .failure(let error):
                    print(error!)
            }
        }
    }

Installation

  • By cloning the project into your repository

Author

Ahmet Kazım Günay, [email protected]

License

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

networklayer's People

Contributors

ahmetkgunay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.