Skip to content

Commit f9767be

Browse files
committed
Update README
1 parent dc74de3 commit f9767be

File tree

1 file changed

+49
-64
lines changed

1 file changed

+49
-64
lines changed

README.md

+49-64
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ JSONRPCKit is a [JSON-RPC 2.0](http://www.jsonrpc.org/specification) library pur
55
```swift
66
// Generating request JSON
77
let batchFactory = BatchFactory(version: "2.0", idGenerator: NumberIdGenerator())
8-
let request = SubtractRequest(lhs: 42, rhs: 23)
8+
let request = Subtract(minuend: 42, subtrahend: 23)
99
let batch = batchFactory.create(request)
1010
batch.requestObject // ["jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1]
1111

1212
// Parsing response JSON
13-
let responseObject: AnyObject = ["jsonrpc": "2.0", "result": 19, "id": 1]
14-
let response = try! batch.responsesFromObject(responseObject)
13+
let responseObject: Any = ["jsonrpc": "2.0", "result": 19, "id": 1]
14+
let response = try! batch.responses(from: responseObject)
1515
response // 19 (type of response is inferred from SubtractRequest.Response)
1616
```
1717

1818

1919
## Requirements
2020

21-
- Swift 2.2 or 2.3
21+
- Swift 3.0 or later
2222
- iOS 8.0 or later
2323

2424
## Basic usage
@@ -29,39 +29,28 @@ response // 19 (type of response is inferred from SubtractRequest.Response)
2929

3030
### Defining request type
3131

32-
First of all, define a request type that conforms to `RequestType`.
32+
First of all, define a request type that conforms to `Request`.
3333

3434
```swift
35-
struct CountCharactersRequest: RequestType {
36-
typealias Response = CountCharactersResponse
35+
struct Subtract: JSONRPCKit.Request {
36+
typealias Response = Int
3737

38-
let characters: String
38+
let minuend: Int
39+
let subtrahend: Int
3940

4041
var method: String {
41-
return "count_characters"
42+
return "subtract"
4243
}
4344

44-
var parameters: AnyObject? {
45-
return ["characters": characters]
45+
var parameters: Any? {
46+
return [minuend, subtrahend]
4647
}
4748

48-
func responseFromResultObject(resultObject: AnyObject) throws -> Response {
49-
return try CountCharactersResponse(object: resultObject)
50-
}
51-
}
52-
53-
struct CountCharactersResponse {
54-
let count: Int
55-
56-
init(object: AnyObject) throws {
57-
enum DecodeError: ErrorType {
58-
case MissingValueForKey(String)
59-
}
60-
61-
if let count = object["count"] as? Int {
62-
self.count = count
49+
func response(from resultObject: Any) throws -> Response {
50+
if let response = resultObject as? Response {
51+
return response
6352
} else {
64-
throw DecodeError.MissingValueForKey("count")
53+
throw CastError(actualValue: resultObject, expectedType: Response.self)
6554
}
6655
}
6756
}
@@ -70,13 +59,13 @@ struct CountCharactersResponse {
7059

7160
### Generating request JSON
7261

73-
To generate request JSON, pass `RequestType` instances to `BatchFactory` instance, which has common JSON-RPC version and identifier generator.
62+
To generate request JSON, pass `Request` instances to `BatchFactory` instance, which has common JSON-RPC version and identifier generator.
7463
When `BatchFactory` instance receives request(s), it generates identifier(s) for the request(s) and request JSON by combining id, version, method and parameters.
7564

7665
```swift
7766
let batchFactory = BatchFactory(version: "2.0", idGenerator: NumberIdGenerator())
78-
let request1 = CountCharactersRequest(characters: "tokyo")
79-
let request2 = CountCharactersRequest(characters: "california")
67+
let request1 = Subtract(minuend: 42, subtrahend: 23)
68+
let request2 = Subtract(minuend: 23, subtrahend: 42)
8069
let batch = batchFactory.create(request1, request2)
8170
```
8271

@@ -85,20 +74,22 @@ The request JSON is available in `batch.requestObject`. It looks like below:
8574
```json
8675
[
8776
{
77+
"method" : "subtract",
8878
"jsonrpc" : "2.0",
89-
"method" : "count_characters",
9079
"id" : 1,
91-
"params" : {
92-
"characters" : "tokyo"
93-
}
80+
"params" : [
81+
42,
82+
23
83+
]
9484
},
9585
{
86+
"method" : "subtract",
9687
"jsonrpc" : "2.0",
97-
"method" : "count_characters",
9888
"id" : 2,
99-
"params" : {
100-
"characters" : "california"
101-
}
89+
"params" : [
90+
23,
91+
42
92+
]
10293
}
10394
]
10495
```
@@ -111,31 +102,29 @@ Suppose that following JSON is returned from server:
111102
```json
112103
[
113104
{
105+
"result" : 19,
114106
"jsonrpc" : "2.0",
115107
"id" : 1,
116-
"result" : {
117-
"count" : 5
118-
}
108+
"status" : 0
119109
},
120110
{
111+
"result" : -19,
121112
"jsonrpc" : "2.0",
122113
"id" : 2,
123-
"result" : {
124-
"count" : 10
125-
}
114+
"status" : 0
126115
}
127116
]
128117
```
129118

130-
To parse response object, execute `responsesFromObject(_:)` of `BatchType` instance.
131-
When `responsesFromObject(_:)` is called, `BatchType` finds corresponding response object by comparing request id and response id.
132-
After it find the response object, it executes `responsesFromObject(_:)` of `Response` to get `Request.Response` from the response object.
119+
To parse response object, execute `responses(from:)` of `Batch` instance.
120+
When `responses(from:)` is called, `Batch` finds corresponding response object by comparing request id and response id.
121+
After it find the response object, it executes `responses(from:)` of `Response` to get `Request.Response` from the response object.
133122

134123
```swift
135124
let responseObject = ...
136-
let (response1, response2) = try! batch.responsesFromObject(responseObject)
137-
print(response1) // CountCharactersResponse(count: 5)
138-
print(response2) // CountCharactersResponse(count: 10)
125+
let (response1, response2) = try! batch.responses(from: responseObject)
126+
print(response1) // 19
127+
print(response2) // -19
139128
```
140129

141130
## JSON-RPC over HTTP by [APIKit](https://github.com/ishkawa/APIKit)
@@ -149,29 +138,29 @@ APIKit also has `RequestType` that represents HTTP request.
149138
```swift
150139
import APIKit
151140

152-
struct MyServiceRequest<Batch: BatchType>: APIKit.RequestType {
153-
typealias Response = Batch.Responses
154-
141+
struct MyServiceRequest<Batch: JSONRPCKit.Batch>: APIKit.Request {
155142
let batch: Batch
156143

157-
var baseURL: NSURL {
144+
typealias Response = Batch.Responses
145+
146+
var baseURL: URL {
158147
return NSURL(string: "https://api.example.com/")!
159148
}
160149

161150
var method: HTTPMethod {
162-
return .POST
151+
return .post
163152
}
164153

165154
var path: String {
166155
return "/"
167156
}
168157

169-
var parameters: AnyObject? {
158+
var parameters: Any? {
170159
return batch.requestObject
171160
}
172161

173-
func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) throws -> Response {
174-
return try batch.responsesFromObject(object)
162+
func response(from object: Any, urlResponse: HTTPURLResponse) throws -> Response {
163+
return try batch.responses(from: object)
175164
}
176165
}
177166
```
@@ -180,8 +169,8 @@ struct MyServiceRequest<Batch: BatchType>: APIKit.RequestType {
180169

181170
```swift
182171
let batchFactory = BatchFactory(version: "2.0", idGenerator: NumberIdGenerator())
183-
let request1 = CountCharactersRequest(message: "tokyo")
184-
let request2 = CountCharactersRequest(message: "california")
172+
let request1 = Subtract(minuend: 42, subtrahend: 23)
173+
let request2 = Subtract(minuend: 23, subtrahend: 42)
185174
let batch = batchFactory.create(request1, request2)
186175
let httpRequest = MyServiceRequest(batch: batch)
187176

@@ -197,10 +186,6 @@ Session.sendRequest(httpRequest) { result in
197186
}
198187
```
199188

200-
## Migrating from JSONRPCKit 0.x
201-
202-
`git diff 0.6.0 develop/1.0 -- Example`
203-
204189
## License
205190

206191
JSONRPCKit is released under the [MIT License](LICENSE.md).

0 commit comments

Comments
 (0)