Shram 0.1.1

Shram 0.1.1

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

Maks Tishchenko 维护。



Shram 0.1.1

  • 作者:
  • Maxim Tischenko

简单的 Http 请求和映射

Shram - 是一个易于使用但功能强大的框架,用于通过 HTTP 请求与 RESTful 网络服务进行通信。Shram 将帮助您仅用几行代码即可向服务器发送请求。该框架的主要优势是清晰和易用。但这并不是 Shram 的所有特性。Shram 支持从服务器响应中映射对象。因此,您现在可以轻松查询服务器上的任何数据,并且将其用作现成的对象。无需手动构建它们。Shram 会为您完成。有关更多信息,请参阅以下内容。

支持的平台

  • IOS

如何安装

使用 Cocoapods,您需要在 podfile 中写入 pod 'Shram', '0.1.0’

工作原理

如上所述,Shram 非常易于使用。让我们来梳理一下。

您可以创建 ShramRequest 的对象。

var request = ShramRequest(URL: "http://www.sample.com/api/method", method: "GET")

然后可以设置以下参数

  • 参数: [String : AnyObject]?
  • 头部: [String : String]?
  • 映射到: ShramMappingProtocol.Type?
  • 解析键: [String]?
  • 内容类型: ContentType?
  • 超时: TimeInterval //默认值是 30.0

发送请求时,您可以使用以下方法

send(completion, failure) 用于普通请求

download(completion, progress, failure) 用于带有进度条的下载请求

upload(completion, progress, failure) 用于带有进度条的上传请求

例如

let params = [
        "someKey" : "someValue"
    ]

var request = ShramRequest(URL: "http://www.sample.com/api/method", method: "GET")
request.parameters = params as [String : AnyObject]?
req.send(completion: { (req, data, resp) in
            
}) { (req, err, resp) in
            
}

#另一种方法#

###方法 GET###

要发送简单的 GET 请求,需要编写以下内容

Shram.GET("http://www.sample.com/api/method",
          completion: { (request, data, response) in
            
     },
          failure: { (request, error, response) in
     }
)

非常简单。但是如果我们想传递一些参数或头部信息呢?由于 Swift 允许为函数参数设置默认值,因此您无需寻找其他方法。GET 方法的完整版本如下

Shram.GET("http://www.sample.com/api/method",
          params: params,
          headers: headers,
          mapTo: Type,
          withParseKeys: ["firstKey", "secondKey", ...],
          completion: { (request, data, response) in
     
     },
          failure: { (request, error, response) in
     
     }
)

让我们看看这些参数

  • URL: API 方法的字符串值。
  • params: 包含键(服务器参数)和值(应发送的参数)的字典。
  • 头部: 包含 HTTP 头部的字典。
  • 映射到: 您想要获得的对象类型。此类型应实现 ShramMappingProtocol。
  • withParseKeys:用于解析嵌套对象服务器响应的键的字符串数组。例如,服务器使用键“data”返回数据。
{
    "data" : {
        "name": "userName",
        "age": "userAge"
    }
}

要获取响应中所需的数据,您需要将 withParseKeys: ["data"] 作为参数设置到 GET 函数中。

  • 完成:在服务器成功响应时调用闭包。返回

    • request:ShramRequest
    • data:Data?
    • response:ShramResponse?
  • 失败:当请求失败或服务器返回错误时调用闭包。返回

    • request:ShramRequest
    • error:Error?
    • response:ShramResponse?

如您所见,“params”、“headers”、“mapTo”、“withParseKeys”都是可选的,您不必在没有必要时传递它们给函数。

###POST 方法###

方法 POST 类似。有简化和完整版本

Shram.POST("http://www.sample.com/api/method",
           contentType: contentType,
           completion: { (request, data, response) in
                    
     },
           failure: { (request, error, response) in
            
     }
)

Shram.POST("http://www.sample.com/api/method",
           params: params,
           contentType: contentType,
           headers: headers,
           mapTo: Type,
           withParseKeys: ["firstKey", "secondKey", ...],
           completion: { (request, data, response) in
     
     },
           failure: { (request, error, response) in
     
     }
)

在此方法中,您可以看到新的参数

  • contentType:支持三种内容类型
    • application/json (.JSON)
    • application/x-www-form-urlencoded (.URLENCODED)
    • multipart/form-data (.MULTIPART_FORM_DATA)

###PUT 方法###

Shram.PUT(http://www.sample.com/api/method,
          params: params,
          contentType: contentType,
          completion: { (request, data, response) in
                    
     },
          failure: { (request, error, response) in
     }
)

Shram.PUT(http://www.sample.com/api/method,
          params: params,
          contentType: contentType,
          headers: headers,
          mapTo: Type,
          withParseKeys: ["firstKey", "secondKey", ...],
          completion: { (request, data, response) in
                    
    },
         failure: {(request, error, response) in
    }
)

###DELETE 方法###

Shram.DELETE("http://www.sample.com/api/method",
             completion: { (request, data, response) in
                        
     },
             failure: { (request, error, response) in
     }
)

Shram.DELETE("http://www.sample.com/api/method",
             params: params,
             headers: headers,
             mapTo: Type,
             withParseKeys: ["firstKey", "secondKey", ...],
             completion: { (request, data, response) in
                        
     },
             failure: { (request, error, response) in
     }
)

映射

Shram不仅从服务器获取响应,还创建模型实例。为了实现这一点,您需要通过 'mapTo' 参数传入您模型的类型。您的模型应实现 ShramMappingProtocol 协议。

让我们尝试从服务器获取用户并将响应映射到 Person 模型。

##响应:##

{
    data: [
        {
            name: "John",
            last_name: "Doe",
            age: "30",
            gender: "male"
        },
        {
            name: "sarah",
            last_name: "Doe",
            age: "25",
            gender: "female"
         }
    ]
}

##我们的模型:##

class Person: NSObject, ShramMappingProtocol
{
    var name: String?
    var lastName: String?
    var age: NSNumber?
    var gender: String?
    
    required override init() {
        super.init()
        setAssociation(serverKey: "name", propertyName: "name")
        setAssociation(serverKey: "last_name", propertyName: "lastName")
        setAssociation(serverKey: "age", propertyName: "age")
        setAssociation(serverKey: "gender", propertyName: "gender")
    }
}

##GET 方法:##

Shram.GET("http://www.sample.com/api/method",
          mapTo: Person.self,
          withParseKeys: ["data"],
          completion: { (request, data, response) in
             let users = response?.mapings
     },
          failure: { (request, error, response) in
            
     }
)

如您所见,我们的模型实现了 ShramMappingProtocol,现在它具有 setAssociation 函数。此函数在模型属性和响应中的键之间建立连接。创建的模型对象在响应的 mapings 属性中可用。它还支持嵌套对象。为此目的,您需要使用

setRelatedAssociation(serverKey: "server_key", 
                      propertyName: "modelPropertyName", 
                      relatedClass: Type)

relatedClass 应实现 ShramMappingProtocol,并且 init 应类似于上面的 Person 模型被覆写。

##嵌套对象的限制:##您的模型结构应在响应中是相同的对象。这个问题将很快得到解决。