Skip to content

Commit 431c084

Browse files
authored
Merge pull request #7 from kolyasev/v0.9.0
Merge v0.9.0 into master
2 parents f6b99f3 + 3702340 commit 431c084

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+733
-1268
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ DerivedData
2525
Carthage
2626

2727
# Swift Package Manager
28+
.swiftpm
2829
.build/

.swift-version

Lines changed: 0 additions & 1 deletion
This file was deleted.

.swiftlint.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
included:
2+
- Sources
3+
- Tests
4+
5+
disabled_rules:
6+
- identifier_name
7+
- todo

Cartfile

Lines changed: 0 additions & 2 deletions
This file was deleted.

Cartfile.resolved

Lines changed: 0 additions & 2 deletions
This file was deleted.

Package.resolved

Lines changed: 0 additions & 25 deletions
This file was deleted.

Package.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
// swift-tools-version:5.1
1+
// swift-tools-version:5.5
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
77
name: "SwiftJSONRPC",
88
platforms: [
9-
.macOS(.v10_12),
10-
.iOS(.v10),
11-
.tvOS(.v10),
12-
.watchOS(.v3)
9+
.macOS(.v10_15),
10+
.iOS(.v13),
11+
.tvOS(.v13),
12+
.watchOS(.v6)
1313
],
1414
products: [
1515
.library(
@@ -18,13 +18,11 @@ let package = Package(
1818
),
1919
],
2020
dependencies: [
21-
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.1.0"),
22-
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.13.1")
2321
],
2422
targets: [
2523
.target(
2624
name: "SwiftJSONRPC",
27-
dependencies: ["Alamofire", "PromiseKit"],
25+
dependencies: [],
2826
path: "Sources"
2927
),
3028
.testTarget(

README.md

Lines changed: 35 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
```swift
1313
import SwiftJSONRPC
14-
import PromiseKit
1514

1615
class UserService: RPCService {
17-
func vote(rating: Int) -> Promise<Int> {
18-
return invoke("vote", params: ["rating": rating])
16+
func vote(rating: Int) async throws -> Int {
17+
return try await invoke("vote", params: ["rating": rating])
1918
}
20-
21-
func create(name: String) -> Promise<UserModel> {
22-
return invoke("create", params: ["name": name])
19+
20+
func create(name: String) async throws -> User {
21+
return try await invoke("create", params: ["name": name])
2322
}
2423

2524
// And other JSON-RPC methods
@@ -39,84 +38,59 @@ let client = RPCClient(url: url)
3938
let service = MyService(client: client)
4039

4140
// Perform request
42-
service.vote(rating: 5)
43-
```
44-
45-
### Result Handling
46-
47-
SwiftJSONRPC uses [PromiseKit](https://github.com/mxcl/PromiseKit) to return result.
48-
49-
```swift
50-
service.vote(rating: 5)
51-
.done { newRating in
52-
// Handle result
53-
}
54-
.catch { error in
55-
// Handle error
56-
}
41+
try await service.vote(rating: 5)
5742
```
5843

59-
#### Result Serialization
44+
#### Result Decoding
6045

61-
SwiftJSONRPC provides built-in result serialization for `Int`, `String`, `Bool` types.
62-
63-
##### `Parcelable` Protocol
64-
65-
To serialize your custom type result from JSON you can implement `Parcelable` protocol.
66-
67-
```swift
68-
protocol Parcelable {
69-
init(params: [String: Any]) throws
70-
}
71-
```
72-
73-
For example:
46+
SwiftJSONRPC uses Swift's `Decodable` protocol to decode response objects.
7447

7548
```swift
76-
struct UserModel: Parcelable {
49+
struct User: Decodable {
7750
let id: String
7851
let name: String
79-
80-
required init(params: [String: Any]) throws {
81-
// Parse params to struct
82-
// ...
83-
}
8452
}
85-
```
86-
87-
> You can use libraries like [ObjectMapper](https://github.com/Hearst-DD/ObjectMapper), [MAPPER](https://github.com/LYFT/MAPPER) or other to adapt `Parcelable` protocol. Or you can adapt Swift 4 `Decodable`.
8853

89-
After that use this struct as `RPCService.Result` generic parameter:
90-
91-
```swift
9254
class UserService: RPCService {
93-
func create(name: String) -> Promise<UserModel> {
94-
return invoke("create", params: ["name": name])
55+
func getCurrentUser() async throws -> User {
56+
return try await invoke("getCurrentUser")
9557
}
9658
}
59+
60+
let user = try await userService.getCurrentUser()
61+
print("Current user ID = \(user.id), name = \(user.name)")
9762
```
63+
64+
If you need to modify `JSONDecoder`'s behaviour, use `RPCClient.coder.resultDecoder` for that.
65+
9866
```swift
99-
service.create(name: "testuser").done { user in
100-
print("User created with ID = \(user.id)")
101-
}
67+
client.coder.resultDecoder.dateDecodingStrategy = .iso8601
10268
```
10369

104-
Using array of `Parcelable` objects is also supported:
70+
#### Params Encoding
71+
72+
SwiftJSONRPC uses Swift's `Encodable` protocol to encode request params.
10573

10674
```swift
107-
extension UserService {
108-
func allUsers() -> Promise<[UserModel]> {
109-
return invoke("all_users")
110-
}
75+
struct Message: Encodable {
76+
let text: String
11177
}
112-
```
11378

79+
class MessageService: RPCService {
80+
func send(message: Message) async throws {
81+
return try await invoke("sendMessage", params: message)
82+
}
83+
}
11484

115-
## Advanced Usage
85+
let message = Message(text: "Hello World")
86+
try await messageService.send(message: message)
87+
```
11688

117-
### Request Executor
89+
If you need to modify `JSONEncoder`'s behaviour, use `RPCClient.coder.paramsEncoder` for that.
11890

119-
## Requirements
91+
```swift
92+
client.coder.paramsEncoder.dateDecodingStrategy = .iso8601
93+
```
12094

12195
## Installation
12296

@@ -146,7 +120,6 @@ dependencies: [
146120
## ToDo
147121

148122
- [ ] Add support for notification request object without an "id" member.
149-
- [ ] Remove `Parcelable` protocol and use `Decodable`.
150123

151124
## Author
152125

Sources/Libraries/Atomic/Atomic.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public final class Atomic<Value> {
3333
_value = value
3434
}
3535

36-
3736
/// Atomically replaces the contents of the variable.
3837
///
3938
/// Returns the old value.

0 commit comments

Comments
 (0)