Swiftpack.co - kolyasev/SwiftJSONRPC as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by kolyasev.
kolyasev/SwiftJSONRPC 0.9.0
JSON-RPC 2.0 client for Swift
⭐️ 19
🕓 1 year ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/kolyasev/SwiftJSONRPC.git", from: "0.9.0")

SwiftJSONRPC

Usage

Defining a Service

import SwiftJSONRPC

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

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

    // And other JSON-RPC methods
}

You can define as many services as you want depending on your requirements.

Making a Request

// Init JSON-RPC client
let url = URL(string: "http://example.com/rpc")!
let client = RPCClient(url: url)

// Init JSON-RPC service
let service = MyService(client: client)

// Perform request
try await service.vote(rating: 5)

Result Decoding

SwiftJSONRPC uses Swift's Decodable protocol to decode response objects.

struct User: Decodable {
    let id: String
    let name: String
}

class UserService: RPCService {
    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.

client.coder.resultDecoder.dateDecodingStrategy = .iso8601

Params Encoding

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

struct Message: Encodable {
    let text: String
}

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

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

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

client.coder.paramsEncoder.dateDecodingStrategy = .iso8601

Installation

CocoaPods

SwiftJSONRPC is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SwiftJSONRPC"

Carthage

github "kolyasev/SwiftJSONRPC"

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/kolyasev/SwiftJSONRPC.git", .upToNextMajor(from: "0.9.0"))
]

ToDo

  • ☐ Add support for notification request object without an "id" member.

Author

Denis Kolyasev, [email protected]

License

SwiftJSONRPC is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 19
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

0.9.0
1 year ago
  • Replace Alamofire dependency with URLSession
  • Replace PromiseKit dependency with async/await
  • Use Encodable/Decodable for params and result serialisation and deserialisation

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics