Latest0.1.9
Homepagehttps://github.com/mohansinghthagunna/CreatuNetwork
LicenseMIT
Platformsios 10.0
DependenciesRxSwift, RxCocoa, Moya/RxSwift, ReachabilitySwift
Authors

CI Status
Version
License
Platform

Example

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

Requirements

  • Swift 4 or higher
  • target IOS 10 or higher

Linked Library

  • Alamofire (4.7.0)
  • KeychainSwift (10.0.0)
  • Moya (11.0.1)
  • ReachabilitySwift (4.1.0)
  • Result (3.2.4)
  • RxCocoa (4.1.2)
  • RxSwift (4.1.2)

Installation

CreatuNetwork is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'CreatuNetwork'

Api Model

In you api add extention [ApiTargetType]()


import Foundation
import Moya
import CreatuNetwork

enum UserApi {
    case login([String: Any])
    case logout([String: Any])
    case register([String: Any])
}

extension UserApi: ApiTargetType {
    var checkTokenValidity: Bool {
        return false
    }

    var baseURL: URL {
        return URL(string: "{{ YOUR-BASE-URL-HERE }}")!
    }

    var path: String {
        switch self {
        case .register:
            return "{{ REGISTER }}"
        case .login:
            return "{{ LOGIN}}"
        case .logout:
            return "{{ LOGOUT }}"
        }
    }

    var method: Moya.Method {
        switch self {
        case .register,
             .login,
             .logout:
            return .post
        }
    }

    var sampleData: Data {
        return Data()
    }

    var task: Task {
        switch self {
        case .register(let parameters),
             .login(let parameters),
             .logout(let parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        }
    }

    var headers: [String: String]? {
        return ["Accept": "application/json"]
    }

    var authTokenType: AuthHeaderType {
        switch self {
        case .login:
            return .basic
        case .register,
             .logout:
            return .bearer
        }
    }
}

Request

  • request
    Network.request(UserApi.login(["username": "{{ USERNAME }}", "password": "{{ PASSWORD }}"])).subscribe(onNext: {(response) in
                    debugPrint(response)
                }, onError: { (error) in
                    debugPrint(error)
                }).disposed(by: bag)
  • network check
    Network.available() -> Bool // true if netowk available

    Response

  • response model
    var shouldTokenRefresh = false // true if your token out of date and you need to refresh token 
    var response: Any? // your response data
    var message: String? // error message
    var success = true // true if your request successfully completed

For token based life cycle

  • if custom authorization header

    Authorize.customHeader {get set}
  • get access token
    Authorize.accessToken {get}
  • get refresh token

    Authorize.refreshToken {get}
  • get authorize model

    Authorize.authorizeModel { get }
  • set access token with client id and client secret

    Authorize.setAccessToken(_ clientId: String, clientSecret: String) -> Bool
  • set accesstoken with token string

    Authorize.setAccessToken(_ token: String) -> Bool
  • clear all authorize saved data

    Authorize.clearSavedData() -> Bool
  • update authorize model

    Authorize.updateAuthorize(_ authorize: AuthorizeModel) -> Bool
  • update authorize model with json
    Authorize.updateAuthorize(_ authorize: [String: Any]) -> Bool 

    Author

mohansinghthagunna, [email protected]

License

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

Latest podspec

{
    "name": "CreatuNetwork",
    "version": "0.1.9",
    "summary": "This is Network request library. depends upon RXSwift, RxXoxoa, Moya/RxSwift, ReachabilitySwift",
    "description": "## Requirementsn- Swift 4 or highern- target IOS 10 or highernn## Linked Librarynn- Alamofire (4.7.0)n- KeychainSwift (10.0.0)n- Moya (11.0.1)n- ReachabilitySwift (4.1.0)n- Result (3.2.4)n- RxCocoa (4.1.2)n- RxSwift (4.1.2)nn## InstallationnnCreatuNetwork is available through [CocoaPods](http://cocoapods.org). To installnit, simply add the following line to your Podfile:nn```rubynpod 'CreatuNetwork'n```nn## Api ModelnIn you api add extention [ApiTargetType]()n```Swiftnnimport Foundationnimport Moyanimport CreatuNetworknnenum UserApi {n    case login([String: Any])n        case logout([String: Any])n            case register([String: Any])n                }nn                extension UserApi: ApiTargetType {n                    var checkTokenValidity: Bool {n                        return falsen                    }nn                    var baseURL: URL {n                        return URL(string: "{{ YOUR-BASE-URL-HERE }}")!n                    }nn                    var path: String {n                        switch self {n                            case .register:n                                return "{{ REGISTER }}"n                                case .login:n                                    return "{{ LOGIN}}"n                                    case .logout:n                                        return "{{ LOGOUT }}"n                                        }n                                        }nn                                        var method: Moya.Method {n                                            switch self {n                                                case .register,n                                                    .login,n                                                    .logout:n                                                    return .postn                                                    }n                                                    }nn                                                    var sampleData: Data {n                                                        return Data()n                                                    }nn                                                    var task: Task {n                                                        switch self {n                                                            case .register(let parameters),n                                                                .login(let parameters),n                                                                .logout(let parameters):n                                                                return .requestParameters(parameters: parameters, encoding: URLEncoding.default)n                                                                }n                                                                }nn                                                                var headers: [String: String]? {n                                                                    return ["Accept": "application/json"]n                                                                }nn                                                                var authTokenType: AuthHeaderType {n                                                                    switch self {n                                                                        case .login:n                                                                            return .basicn                                                                            case .register,n                                                                                .logout:n                                                                                return .bearern                                                                                }n                                                                                }n                                                                                }nn                                                                                ```nn                                                                                ## Requestnn                                                                                - requestn                                                                                ```Swiftn                                                                                Network.request(UserApi.login(["username": "{{ USERNAME }}", "password": "{{ PASSWORD }}"])).subscribe(onNext: {(response) inn                                                                                                                                                                                       debugPrint(response)n                                                                                                                                                                                       }, onError: { (error) inn                                                                                                                                                                                       debugPrint(error)n                                                                                                                                                                                       }).disposed(by: bag)n                                                                                                                                                                                       ```n                                                                                                                                                                                       - network checkn                                                                                                                                                                                       ```Swiftn                                                                                                                                                                                       Network.available() -> Bool // true if netowk availablen                                                                                                                                                                                       ```n                                                                                                                                                                                       ## Responsen                                                                                                                                                                                       - response modeln                                                                                                                                                                                       ```swiftn                                                                                                                                                                                       var shouldTokenRefresh = false // true if your token out of date and you need to refresh tokenn                                                                                                                                                                                       var response: Any? // your response datan                                                                                                                                                                                       var message: String? // error messagen                                                                                                                                                                                       var success = true // true if your request successfully completedn                                                                                                                                                                                       ```nn                                                                                                                                                                                       ## For token based life cyclenn                                                                                                                                                                                       - if custom authorization headern                                                                                                                                                                                       ```Swiftn                                                                                                                                                                                       Authorize.customHeader {get set}n                                                                                                                                                                                       ```nn                                                                                                                                                                                       - get access tokenn                                                                                                                                                                                       ```Swiftn                                                                                                                                                                                       Authorize.accessToken {get}n                                                                                                                                                                                       ```n                                                                                                                                                                                       - get refresh tokenn                                                                                                                                                                                       ```Swiftn                                                                                                                                                                                       Authorize.refreshToken {get}n                                                                                                                                                                                       ```nn                                                                                                                                                                                       - get authorize modeln                                                                                                                                                                                       ```Swiftn                                                                                                                                                                                       Authorize.authorizeModel { get }n                                                                                                                                                                                       ```nn                                                                                                                                                                                       - set access token with client id and client secretn                                                                                                                                                                                       ```Swiftn                                                                                                                                                                                       Authorize.setAccessToken(_ clientId: String, clientSecret: String) -> Booln                                                                                                                                                                                       ```nn                                                                                                                                                                                       - set accesstoken with token stringn                                                                                                                                                                                       ```Swiftn                                                                                                                                                                                       Authorize.setAccessToken(_ token: String) -> Booln                                                                                                                                                                                       ```nn                                                                                                                                                                                       - clear all authorize saved datan                                                                                                                                                                                       ```Swiftn                                                                                                                                                                                       Authorize.clearSavedData() -> Booln                                                                                                                                                                                       ```nn                                                                                                                                                                                       - update authorize modeln                                                                                                                                                                                       ```Swiftn                                                                                                                                                                                       Authorize.updateAuthorize(_ authorize: AuthorizeModel) -> Booln                                                                                                                                                                                       ```nn                                                                                                                                                                                       - update authorize model with jsonn                                                                                                                                                                                       ```Swiftn                                                                                                                                                                                       Authorize.updateAuthorize(_ authorize: [String: Any]) -> Booln                                                                                                                                                                                       ```",
    "homepage": "https://github.com/mohansinghthagunna/CreatuNetwork",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "mohansinghthagunna": "[email protected]"
    },
    "source": {
        "git": "https://github.com/mohansinghthagunna/CreatuNetwork.git",
        "tag": "0.1.9"
    },
    "social_media_url": "https://twitter.com/sngmon",
    "platforms": {
        "ios": "10.0"
    },
    "swift_version": "4.0",
    "source_files": "CreatuNetwork/**/*",
    "dependencies": {
        "RxSwift": [
            "~> 4.1.2"
        ],
        "RxCocoa": [
            "~> 4.1.2"
        ],
        "Moya/RxSwift": [
            "~> 11.0.1"
        ],
        "ReachabilitySwift": [
            "~> 4.1.0"
        ]
    }
}

Pin It on Pinterest

Share This