GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - orbstack/SwiftJSONRPC

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

106 Commits
106 Commits

Folders and files

NameName
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, [email protected]

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

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.