First of all, define a request type that conforms to Request.
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 JSON
To generate request JSON, pass Request instances to BatchFactory instance, which has common JSON-RPC version and identifier generator.
When BatchFactory instance receives request(s), it generates identifier(s) for the request(s) and request JSON by combining id, version, method and parameters.
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 batch.requestObject. It looks like below:
To parse response object, execute responses(from:) of Batch instance.
When responses(from:) is called, Batch finds corresponding response object by comparing request id and response id.
After it find the response object, it executes responses(from:) of Response to get Request.Response from the response object.
let responseObject = ...
let (response1, response2) = try! batch.responses(from: responseObject)
print(response1) // 19
print(response2) // -19
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)
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)
}
}
JSONRPCKit
JSONRPCKit is a type-safe JSON-RPC 2.0 library purely written in Swift.
Requirements
Basic usage
Defining request type
First of all, define a request type that conforms to
Request
.Generating request JSON
To generate request JSON, pass
Request
instances toBatchFactory
instance, which has common JSON-RPC version and identifier generator. WhenBatchFactory
instance receives request(s), it generates identifier(s) for the request(s) and request JSON by combining id, version, method and parameters.The request JSON is available in
batch.requestObject
. It looks like below:Parsing response JSON
Suppose that following JSON is returned from server:
To parse response object, execute
responses(from:)
ofBatch
instance. Whenresponses(from:)
is called,Batch
finds corresponding response object by comparing request id and response id. After it find the response object, it executesresponses(from:)
ofResponse
to getRequest.Response
from the response object.JSON-RPC over HTTP by APIKit
APIKit is a type-safe networking abstraction layer.
Defining HTTP request type
APIKit also has
RequestType
that represents HTTP request.Sending HTTP/HTTPS request
License
JSONRPCKit is released under the MIT License.