Swiftpack.co - Package - haiyangCool/NetWorkManager

NetWorkManager

NetWorkManager 提供一个简单的网络 API 请求管理

如何安装?

1、通过Swift Package Manager 管理
Xcode -> File -> Swift Package -> Add Package Dependency ,打开 Repository 选择页面
输入 https://github.com/haiyangCool/NetWorkManager.git

2、或者直接把NetWorkManager Package代码拷贝到项目中

如何使用?

1、每个API 对应一个 ApiManager ,继承自APIManager, 而且必须实现APIManagerDelegate协议
2、必须为ApiManager提供 Service (实现APIManagerService协议),Service 管理她提供的所有Api请求

DemoApiManager

class DemoApiManager: APIManager {

    override init() {
        super.init()
        delegate = self
        validator = self
        httpMethod = .GET
        cachePolicy = .memory
        service = DemoService.shared
    }
}

extension DemoApiManager: APIManagerDelegate {
    func requestType() -> String {
        return "GET"
    }
    
    func apiAddress() -> String {
        return "c/top/list.json"
    }
    
    func serviceIdentifier() -> String {
        return "DemoService"
    }
}

extension DemoApiManager: APIManagerValidator {
    func paramsIsCorrect(_ manager: APIManager, params: [String : String]?) -> APIManagerErrorType {
        return .default
    }

    func responseIsCorrect(_ manager: APIManager, response: Dictionary<String, Any>?) -> APIManagerErrorType {
        return .default
    }
}

And Service

class DemoService: NSObject {
    static let shared = DemoService()
    private override init() {
        super.init()
    }
}

extension DemoService: APIManagerService {
    
    func environment() -> String {
        return "develop"
    }
    
    func isHandleApiManagerError(_ manager: APIManager, errorType type: String) -> Bool {
        return false
    }
 
    func serviceAddress() -> String {
        
        let currentEnvironment = APIServiceEnvironment(rawValue: environment())
        if currentEnvironment == .develop {
            return "http://expand.video.iqiyi.com/"
        }
        if currentEnvironment == .release {
            return "http://expand.video.iqiyi.com/"
        }
        return "http://expand.video.iqiyi.com/"
    }
}

ViewController

let _ = demoApiManager.loadData()

extension ViewController: APIManagerResultDelegate, APIManagerParamsSource {

    func success(_ manager: APIManager) {
        let data:Dictionary<String,Any>? = manager.fetchData(with: nil) as? Dictionary<String, Any>
        print("Success Data \(data)")
    }

    func faild(_ manager: APIManager) {
    print("faild : \(manager.faildType())")
    }

    func parameters(_ manager: APIManager) -> [String : String]? {
        return [
            "key":"value",
        ]
    }
}

DO

Have a nice day

Github

link
Stars: 1

Dependencies

Used By

Total: 0

Releases

- 2020-02-11 07:07:32