在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:bricklife/JSONRPCKit开源软件地址:https://github.com/bricklife/JSONRPCKit开源编程语言:Swift 96.1%开源软件介绍:JSONRPCKitJSONRPCKit is a type-safe JSON-RPC 2.0 library purely written in Swift. // Generating request JSON
let batchFactory = BatchFactory(version: "2.0", idGenerator: NumberIdGenerator())
let request = Subtract(minuend: 42, subtrahend: 23)
let batch = batchFactory.create(request)
batch.requestObject // ["jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1]
// Parsing response JSON
let responseObject: Any = ["jsonrpc": "2.0", "result": 19, "id": 1]
let response = try! batch.responses(from: responseObject)
response // 19 (type of response is inferred from SubtractRequest.Response) Requirements
Basic usage
Defining request typeFirst of all, define a request type that conforms to struct Subtract: JSONRPCKit.Request {
typealias Response = Int
let minuend: Int
let subtrahend: Int
var method: String {
return "subtract"
}
var parameters: Any? {
return [minuend, subtrahend]
}
func response(from resultObject: Any) throws -> Response {
if let response = resultObject as? Response {
return response
} else {
throw CastError(actualValue: resultObject, expectedType: Response.self)
}
}
} Generating request JSONTo generate request JSON, pass let batchFactory = BatchFactory(version: "2.0", idGenerator: NumberIdGenerator())
let request1 = Subtract(minuend: 42, subtrahend: 23)
let request2 = Subtract(minuend: 23, subtrahend: 42)
let batch = batchFactory.create(request1, request2) The request JSON is available in [
{
"method" : "subtract",
"jsonrpc" : "2.0",
"id" : 1,
"params" : [
42,
23
]
},
{
"method" : "subtract",
"jsonrpc" : "2.0",
"id" : 2,
"params" : [
23,
42
]
}
] Parsing response JSONSuppose that following JSON is returned from server: [
{
"result" : 19,
"jsonrpc" : "2.0",
"id" : 1,
"status" : 0
},
{
"result" : -19,
"jsonrpc" : "2.0",
"id" : 2,
"status" : 0
}
] To parse response object, execute let responseObject = ...
let (response1, response2) = try! batch.responses(from: responseObject)
print(response1) // 19
print(response2) // -19 APIKitJSON-RPC over HTTP byAPIKit is a type-safe networking abstraction layer. Defining HTTP request typeAPIKit also has import APIKit
struct MyServiceRequest<Batch: JSONRPCKit.Batch>: APIKit.Request {
let batch: Batch
typealias Response = Batch.Responses
var baseURL: URL {
return URL(string: "https://api.example.com/")!
}
var method: HTTPMethod {
return .post
}
var path: String {
return "/"
}
var parameters: Any? {
return batch.requestObject
}
func response(from object: Any, urlResponse: HTTPURLResponse) throws -> Response {
return try batch.responses(from: object)
}
} Sending HTTP/HTTPS requestlet batchFactory = BatchFactory(version: "2.0", idGenerator: NumberIdGenerator())
let request1 = Subtract(minuend: 42, subtrahend: 23)
let request2 = Subtract(minuend: 23, subtrahend: 42)
let batch = batchFactory.create(request1, request2)
let httpRequest = MyServiceRequest(batch: batch)
Session.sendRequest(httpRequest) { result in
switch result {
case .Success(let response1, let response2):
print(response1.count) // CountCharactersResponse
print(response2.count) // CountCharactersResponse
case .Failure(let error):
print(error)
}
} LicenseJSONRPCKit is released under the MIT License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论