Coder Social home page Coder Social logo

angeloffury7 / alamofirerouter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from corlaonline/alamofirerouter

0.0 0.0 0.0 166 KB

AlamofireRouter is a simple Alamofire router

License: MIT License

Shell 5.84% Ruby 0.65% Objective-C 0.41% Swift 93.11%

alamofirerouter's Introduction

AlamofireRouter

[![CI Status](http://img.shields.io/travis/Alex Corlatti/AlamofireRouter.svg?style=flat)](https://travis-ci.org/Alex Corlatti/AlamofireRouter) Version License Platform

AlamofireRouter is a simple Alamofire router. If you need swift 2.3 version, use the 0.1.0 pod version

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 9.0+ / Mac OS X 10.9+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 8.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.0.0+ is required to build AlamofireRouter

To integrate AlamofireRouter into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

pod 'AlamofireRouter'

Then, run the following command:

$ pod install

Usage

You have to create an enum that conform RouterProtocol, so you need variable router and function URLRequest are mandatories.

In router you have to set your API base URL.

In URLRequest you can create your endpoints by function router.endPoint.

The function router.endPoint has four parameters:

  • path: String the path for the endpoint
  • method: Alamofire.HTTPMethod the method of the request, by default is .get
  • parameters: [String : Any] the parameters if necessary, by default is [ : ]
  • encoding: ParameterEncoding the encoding type, by default is URLEncoding.default
  • headers: [[String: String]] the header parameters if necessary, by default is [[:]]

Create your router API

import AlamofireRouter

enum MyAPI: RouterProtocol { 

    case myRequest(myParameter: String)
    case otherRequest(otherParameter: [String: Any])
    case allPersonalizedRequest(param: String)

    var router: Router { return Router(baseURL: "http://YOUR_API_BASE_URL.com/") }

    public func asURLRequest() throws -> URLRequest {

        switch self {

            case .myRequest(let myParameter):

                return router.endPoint(path: "mypath/\(myParameter)")

            case .otherRequest(let otherParameter):

                return router.endPoint(path: "otherPath", method: .post, parameters: otherParameter)

            case .allPersonalizedRequest(let param):

                return router.endPoint(path: "myPath", method: .patch, parameters: ["par" : param], encoding: JSONEncoding.prettyPrinted, headers: [["Content-type": "json"]])

        }

    }

}

Use it with Alamofire

Alamofire.request(MyAPI.myRequest(myParameter: "aValue")).response { resp in 

	// Do some stuff

}

Use it with AlamofireUIManager

let netManager = AlamofireUIManager.sharedInstance

netManager.request(MyAPI.myRequest(myParameter: "aValue"), completionHandler: { json in
	
	// Do some stuff
	
})

Author

Alex Corlatti, [email protected]

License

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

alamofirerouter's People

Contributors

corlaonline 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.