← Winnitude! Search  ·  Reader Mode (857 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 44 LINKS

GitHub - orbstack/SwiftJSONRPC

Repository navigation

SwiftJSONRPC
Public
forked from kolyasev/SwiftJSONRPC

About

No description, website, or topics provided.

Resources

Readme License Activity Custom properties

Stars

2 stars

Watchers

1 watching

Forks

0 forks Report repository
Branches

Tags
Go to Branches page Go to Tags page    

Latest commit

 

History


106 Commits
View commit history for this file.

Folders and files

Name Name Last commit message Last commit date
.github/ workflows .github/ workflows    
Sources Sources    
SwiftJSONRPC.xcodeproj SwiftJSONRPC.xcodeproj    
Tests Tests    
.gitignore .gitignore    
.swiftlint.yml .swiftlint.yml    
LICENSE LICENSE    
Package.swift Package.swift    
README.md README.md    
SwiftJSONRPC.podspec SwiftJSONRPC.podspec    

Repository files navigation

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, kolyasev@gmail.com

License

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

About

No description, website, or topics provided.

Resources

Readme License Activity Custom properties

Stars

2 stars

Watchers

1 watching

Forks

0 forks Report repository

Releases

Packages

Contributors

Languages