Coder Social home page Coder Social logo

hanetwork's Introduction

HANetwork

It's a Alamofire basic requestes wrapper

This wrapping can help you to use the Alamofire library in a very simple way, Follow the steps:

  • Make requests with URLRequestConvertible
  • Call method from any ViewController and receive three possible cases:
    • Success as single object, data response type [String: Any]
    • Success as collection of objects, data response type [String: Any]
    • Error as tuple of (NSError, Data), if you want parse response body from server.

Usage


Make URLRequestConvertible enum:

import Alamofire

enum DemoRouter: URLRequestConvertible {
    
    case getPostInfo(parameters: Parameters)
    
    var method: HTTPMethod {
        switch self {
        case .getPostInfo:
            return .get
        }
    }
    
    var path: String {
        switch self {
        case .getPostInfo:
            return "https://your_url_base/path/path"            
        }
    }
    
    // MARK: URLRequestConvertible
    
    func asURLRequest() throws -> URLRequest {
        let url = try Constants.URL.baseURL.asURL()
        var urlRequest = URLRequest(url: url.appendingPathComponent(path))
        urlRequest.httpMethod = method.rawValue
        
        switch self {
        case .getPostInfo(let parameters):
            urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
        }
        return urlRequest
    }
}

Call from ViewController:

  1. Instance wrapper:
var network: RealNetworkRequest = RealNetworkRequest(session: NetworkSessionManager.shared)
  1. Calling your URLRequestConvertible:
network.request(router: DemoRouter.getPostInfo(["your_params_here":"param"])) { (result) in            
           
           switch result {                
            case .success(.array (let response)):
                    debugPrint("Collection: ----> \(response)")
                
            case .success(.object (let response)):
                    debugPrint("Single: ----> \(response)")
                
            case .error(.basic (let error)):
                debugPrint("Error: \(error)")
                
            }
        }

You have three methods for call Wrapper:

    // Basic
    func request(router: URLRequestConvertible,
                 completion: @escaping (Result<Json, NetworkError>) -> Void) -> Void
    
    // With RequestAdapter, for custom Headers
    func request(router: URLRequestConvertible,
                 adapter: RequestAdapter?,
                 completion: @escaping (Result<Json, NetworkError>) -> Void) -> Void
    
    // Or more customizable params and data
    func request( _ url: URL,
                  method: HTTPMethod,
                  parameters: [String: Any]?,
                  headers: [String: String]?,
                  completion: @escaping (Result<Json, NetworkError>) -> Void) -> Void

hanetwork's People

Contributors

litoarias avatar

Stargazers

 avatar

Watchers

 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.