Skip to content

socialmohansingh/CreatuNetwork

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CreatuNetwork

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 (Latest)
  • 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, mohansingh_thagunna@outlook.com

License

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