NBUtility 0.1.0

NBUtility 0.1.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017年6月
SwiftSwift 版本3.0
SPM支持 SPM

Fahid Attique 维护。



 
依赖关系
Alamofire>= 0
ObjectMapper>= 0
AlamofireObjectMapper>= 0
QorumLogs>= 0
 

NBUtility 0.1.0

  • Fahid Attique

Alt text

功能

Routable.swift 功能

  • 它支持所有 HTTP 请求方法,即 GET、POST、PUT 和 DELETE
  • 它支持多部分请求,用于将数据上传到您的服务器
  • 它提供三种调用 HTTP 请求方法的方式:
    • request
      • 此方法的响应将是 JSONTopModel。您可以按照需要对其进行解析
    • requestForObject
      • 此方法的响应将是自动映射的对象。您只需在请求参数中发送 mapperClass
    • requestForArray
      • 此方法的响应将是自动映射的对象数组。您只需在请求参数中发送 mapperClass
  • 它为多部分 API 调用提供相同的三个方法
  • 它允许您通过只有一个布尔参数“authorized”来授权 API 调用
  • 它提供了对服务器错误处理的灵活性
  • 它将处理所有 API 调用的响应序列化错误

特殊功能

  • 面向协议的网络管理器
  • 提供所有 HTTP 和多部分请求方法的共享实现
  • 允许您提供 Routable 协议中定义的任何请求方法的自定义实现
  • 高度结构化,使用抽象和命名空间技术

依赖关系

使用说明

Routable.swift

  • 它是一个面向协议的网络管理器,具有以下特性
  • 所有网络调用都将使用 JSONTopModel 作为基础类解析其响应。您可以在 Routable.swift 中找到其实现

步骤 1

  • 在您的 Xcode 项目中创建一个 ServiceManager.swift 文件

步骤 2

  • 创建一个 enum Endpoint 以添加应用程序特定的端点。使其符合 Directible 以从端点创建可定位的 URL。
  • 它看起来像以下示例
enum Endpoint: Directable {

    
    //      Setup the base Url of your application according to the application mode

    static var baseUrl: String {

        var baseUrl = ""

        switch appMode {
        
            case .test:
                baseUrl = "http://apidev.accuweather.com/currentconditions"
            case .production:
                baseUrl = "http://apidev.accuweather.com/currentconditions"
        }

        return baseUrl
    }




    //      Define some endpoints like this,

    case weatherConditions,
    countryList





    //      Implement the protocol method to make your app specific end points fully directable as Url
    
    func directableURLString() -> String {

        var servicePath = ""

        switch (self) {

            case .weatherConditions:
            servicePath = "get-weather-conditions"

            case .countryList:
            servicePath = "get-countries-data"
        }

        let tail = "api"
        return Endpoint.baseUrl + "/" + tail + "/" + servicePath
    }
}

第3步

  • 创建一个用于管理API调用的类ServiceManager,并使其符合协议 Routable
  • 它看起来像以下示例
class ServiceManager: Routable {



//      You need to implement this method to send App specific headers with your API calls

    func authorizationHeadersIf(_ authorized: Bool) -> [String : String]? {

        //  You can send Open Auth Token, Appversion, API version and many more as per your need
        
        return ["app-version":"1.0"]
    }



//      You need to implement this method to validate the error of your server and you can take many decisions here with server's error status code


    func handleServerError(_ result: DataResponse<JSONTopModal>, failure: FailureErrorBlock!) -> Bool {

        let resultValue = result.value!

        if resultValue.isError {
            if resultValue.statusCode == -1 {
                //     handleTokenError(resultValue.message)
            }
            else {
                failure(NSError(errorMessage: resultValue.message, code: resultValue.statusCode))
            }

            return true
        }

        return false
    }
}

第5步

  • 在您的控制器中,您可以使用ServiceManager来管理您的网络调用

  • 以下是一个简单API调用的示例:

    fileprivate let manager:ServiceManager = ServiceManager()

    manager.request(.get, service: Endpoint.countryList, success: { (response, jsonTopModelResult) in
        
        print("proceed with your jsonTopModelResult")
    
    }, failure: { (error) in

        print("Handle error")
    })
  • 在相同的API调用中添加“authorized”布尔参数,以添加授权头
    manager.request(.get, service: Endpoint.countryList, authorized: true, success: { (response, jsonTopModelResult) in

        print("proceed with your jsonTopModelResult")

    }, failure: { (error) in

        print("Handle error")
    })

高级用法

  • 使用Routable协议的帮助自动映射获取国家列表(自定义对象的数组)
    manager.requestForArray(.get, service: Endpoint.countryList, mapperClass: Country.self, success: { (response, countries) in

        print("proceed with your countries list")
    
    }, failure: { (error) in

        print("Handle error")
    })
  • 使用Routable协议的帮助自动映射获取天气状况(自定义对象)
    manager.requestForObject(.get, service: Endpoint.weatherConditions, mapperClass: WeatherCondition.self, success: { (response, weatherConditions) in

        print("proceed with your weather Conditions")

    }, failure: { (error) in

        print("Handle error")
    })
  • 相同的API调用可以作为多部分请求上传数据到您的服务器。使用Routable协议的多部分请求方法。

许可

NBUtility在MIT许可下可用。有关更多信息,请参阅LICENSE文件。

作者

法希德·阿蒂克 - (https://github.com/fahidattique55)