测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可 | MIT |
发布上次发布 | 2017年8月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Maks Tishchenko 维护。
Shram - 是一个易于使用但功能强大的框架,用于通过 HTTP 请求与 RESTful 网络服务进行通信。Shram 将帮助您仅用几行代码即可向服务器发送请求。该框架的主要优势是清晰和易用。但这并不是 Shram 的所有特性。Shram 支持从服务器响应中映射对象。因此,您现在可以轻松查询服务器上的任何数据,并且将其用作现成的对象。无需手动构建它们。Shram 会为您完成。有关更多信息,请参阅以下内容。
使用 Cocoapods,您需要在 podfile 中写入 pod 'Shram', '0.1.0’
。
如上所述,Shram 非常易于使用。让我们来梳理一下。
您可以创建 ShramRequest 的对象。
var request = ShramRequest(URL: "http://www.sample.com/api/method", method: "GET")
然后可以设置以下参数
发送请求时,您可以使用以下方法
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
}
)
让我们看看这些参数
{
"data" : {
"name": "userName",
"age": "userAge"
}
}
要获取响应中所需的数据,您需要将 withParseKeys: ["data"]
作为参数设置到 GET 函数中。
完成:在服务器成功响应时调用闭包。返回
失败:当请求失败或服务器返回错误时调用闭包。返回
如您所见,“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
}
)
在此方法中,您可以看到新的参数
###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 模型被覆写。
##嵌套对象的限制:##您的模型结构应在响应中是相同的对象。这个问题将很快得到解决。