Skip to content

Commit

Permalink
Merge pull request #7 from kolyasev/v0.9.0
Browse files Browse the repository at this point in the history
Merge v0.9.0 into master
  • Loading branch information
kolyasev authored Jul 11, 2022
2 parents f6b99f3 + 3702340 commit 431c084
Show file tree
Hide file tree
Showing 47 changed files with 733 additions and 1,268 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ DerivedData
Carthage

# Swift Package Manager
.swiftpm
.build/
1 change: 0 additions & 1 deletion .swift-version

This file was deleted.

7 changes: 7 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
included:
- Sources
- Tests

disabled_rules:
- identifier_name
- todo
2 changes: 0 additions & 2 deletions Cartfile

This file was deleted.

2 changes: 0 additions & 2 deletions Cartfile.resolved

This file was deleted.

25 changes: 0 additions & 25 deletions Package.resolved

This file was deleted.

14 changes: 6 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// swift-tools-version:5.1
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SwiftJSONRPC",
platforms: [
.macOS(.v10_12),
.iOS(.v10),
.tvOS(.v10),
.watchOS(.v3)
.macOS(.v10_15),
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6)
],
products: [
.library(
Expand All @@ -18,13 +18,11 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.1.0"),
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.13.1")
],
targets: [
.target(
name: "SwiftJSONRPC",
dependencies: ["Alamofire", "PromiseKit"],
dependencies: [],
path: "Sources"
),
.testTarget(
Expand Down
97 changes: 35 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

```swift
import SwiftJSONRPC
import PromiseKit

class UserService: RPCService {
func vote(rating: Int) -> Promise<Int> {
return invoke("vote", params: ["rating": rating])
func vote(rating: Int) async throws -> Int {
return try await invoke("vote", params: ["rating": rating])
}
func create(name: String) -> Promise<UserModel> {
return invoke("create", params: ["name": name])

func create(name: String) async throws -> User {
return try await invoke("create", params: ["name": name])
}

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

// Perform request
service.vote(rating: 5)
```

### Result Handling

SwiftJSONRPC uses [PromiseKit](https://github.com/mxcl/PromiseKit) to return result.

```swift
service.vote(rating: 5)
.done { newRating in
// Handle result
}
.catch { error in
// Handle error
}
try await service.vote(rating: 5)
```

#### Result Serialization
#### Result Decoding

SwiftJSONRPC provides built-in result serialization for `Int`, `String`, `Bool` types.

##### `Parcelable` Protocol

To serialize your custom type result from JSON you can implement `Parcelable` protocol.

```swift
protocol Parcelable {
init(params: [String: Any]) throws
}
```

For example:
SwiftJSONRPC uses Swift's `Decodable` protocol to decode response objects.

```swift
struct UserModel: Parcelable {
struct User: Decodable {
let id: String
let name: String

required init(params: [String: Any]) throws {
// Parse params to struct
// ...
}
}
```

> 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`.

After that use this struct as `RPCService.Result` generic parameter:

```swift
class UserService: RPCService {
func create(name: String) -> Promise<UserModel> {
return invoke("create", params: ["name": name])
func getCurrentUser() async throws -> User {
return try await invoke("getCurrentUser")
}
}

let user = try await userService.getCurrentUser()
print("Current user ID = \(user.id), name = \(user.name)")
```

If you need to modify `JSONDecoder`'s behaviour, use `RPCClient.coder.resultDecoder` for that.

```swift
service.create(name: "testuser").done { user in
print("User created with ID = \(user.id)")
}
client.coder.resultDecoder.dateDecodingStrategy = .iso8601
```

Using array of `Parcelable` objects is also supported:
#### Params Encoding

SwiftJSONRPC uses Swift's `Encodable` protocol to encode request params.

```swift
extension UserService {
func allUsers() -> Promise<[UserModel]> {
return invoke("all_users")
}
struct Message: Encodable {
let text: String
}
```

class MessageService: RPCService {
func send(message: Message) async throws {
return try await invoke("sendMessage", params: message)
}
}

## Advanced Usage
let message = Message(text: "Hello World")
try await messageService.send(message: message)
```

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

## Requirements
```swift
client.coder.paramsEncoder.dateDecodingStrategy = .iso8601
```

## Installation

Expand Down Expand Up @@ -146,7 +120,6 @@ dependencies: [
## ToDo

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

## Author

Expand Down
1 change: 0 additions & 1 deletion Sources/Libraries/Atomic/Atomic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public final class Atomic<Value> {
_value = value
}


/// Atomically replaces the contents of the variable.
///
/// Returns the old value.
Expand Down
Loading

0 comments on commit 431c084

Please sign in to comment.