{"id":1674,"url":"https://github.com/bricklife/JSONRPCKit","last_synced_at":"2025-08-02T04:32:14.927Z","repository":{"id":56577600,"uuid":"45905880","full_name":"bricklife/JSONRPCKit","owner":"bricklife","description":"A type-safe JSON-RPC 2.0 library purely written in Swift","archived":true,"fork":false,"pushed_at":"2020-08-29T22:18:28.000Z","size":156,"stargazers_count":177,"open_issues_count":9,"forks_count":59,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-16T02:50:29.564Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bricklife.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-10T10:52:47.000Z","updated_at":"2024-09-14T09:22:27.000Z","dependencies_parsed_at":"2022-08-15T21:20:26.308Z","dependency_job_id":null,"html_url":"https://github.com/bricklife/JSONRPCKit","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bricklife%2FJSONRPCKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bricklife%2FJSONRPCKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bricklife%2FJSONRPCKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bricklife%2FJSONRPCKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bricklife","download_url":"https://codeload.github.com/bricklife/JSONRPCKit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228439075,"owners_count":17920017,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:15:53.000Z","updated_at":"2024-12-06T08:31:25.008Z","avatar_url":"https://github.com/bricklife.png","language":"Swift","funding_links":[],"categories":["Representations","Networking","Libraries"],"sub_categories":["Email","Other free courses"],"readme":"# JSONRPCKit\n\n[![Build Status](https://travis-ci.org/bricklife/JSONRPCKit.svg?branch=master)](https://travis-ci.org/bricklife/JSONRPCKit)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![CocoaPods](https://img.shields.io/cocoapods/v/JSONRPCKit.svg)](https://cocoapods.org/)\n\nJSONRPCKit is a type-safe [JSON-RPC 2.0](http://www.jsonrpc.org/specification) library purely written in Swift.\n\n```swift\n// Generating request JSON\nlet batchFactory = BatchFactory(version: \"2.0\", idGenerator: NumberIdGenerator())\nlet request = Subtract(minuend: 42, subtrahend: 23)\nlet batch = batchFactory.create(request)\nbatch.requestObject // [\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": [42, 23], \"id\": 1]\n\n// Parsing response JSON\nlet responseObject: Any = [\"jsonrpc\": \"2.0\", \"result\": 19, \"id\": 1]\nlet response = try! batch.responses(from: responseObject)\nresponse // 19 (type of response is inferred from SubtractRequest.Response)\n```\n\n## Requirements\n\n- Swift 4.0 / Xcode 9.0 or later\n    - If you use Swift 3.1 (Xcode 8.3), you can use [2.0.3](https://github.com/bricklife/JSONRPCKit/tree/2.0.3) instead.\n- iOS 8.0 or later\n- macOS 10.9 or later\n- watchOS 2.0 or later\n- tvOS 9.0 or later\n- [Linux](https://swift.org/download/#linux) is also supported\n\n## Basic usage\n\n1. Define request type\n2. Generate request JSON\n3. Parse response JSON\n\n### Defining request type\n\nFirst of all, define a request type that conforms to `Request`.\n\n```swift\nstruct Subtract: JSONRPCKit.Request {\n    typealias Response = Int\n\n    let minuend: Int\n    let subtrahend: Int\n\n    var method: String {\n        return \"subtract\"\n    }\n\n    var parameters: Any? {\n        return [minuend, subtrahend]\n    }\n\n    func response(from resultObject: Any) throws -\u003e Response {\n        if let response = resultObject as? Response {\n            return response\n        } else {\n            throw CastError(actualValue: resultObject, expectedType: Response.self)\n        }\n    }\n}\n```\n\n\n### Generating request JSON\n\nTo generate request JSON, pass `Request` instances to `BatchFactory` instance, which has common JSON-RPC version and identifier generator.\nWhen `BatchFactory` instance receives request(s), it generates identifier(s) for the request(s) and request JSON by combining id, version, method and parameters.\n\n```swift\nlet batchFactory = BatchFactory(version: \"2.0\", idGenerator: NumberIdGenerator())\nlet request1 = Subtract(minuend: 42, subtrahend: 23)\nlet request2 = Subtract(minuend: 23, subtrahend: 42)\nlet batch = batchFactory.create(request1, request2)\n```\n\nThe request JSON is available in `batch.requestObject`. It looks like below:\n\n```json\n[\n  {\n    \"method\" : \"subtract\",\n    \"jsonrpc\" : \"2.0\",\n    \"id\" : 1,\n    \"params\" : [\n      42,\n      23\n    ]\n  },\n  {\n    \"method\" : \"subtract\",\n    \"jsonrpc\" : \"2.0\",\n    \"id\" : 2,\n    \"params\" : [\n      23,\n      42\n    ]\n  }\n]\n```\n\n\n### Parsing response JSON\n\nSuppose that following JSON is returned from server:\n\n```json\n[\n  {\n    \"result\" : 19,\n    \"jsonrpc\" : \"2.0\",\n    \"id\" : 1,\n    \"status\" : 0\n  },\n  {\n    \"result\" : -19,\n    \"jsonrpc\" : \"2.0\",\n    \"id\" : 2,\n    \"status\" : 0\n  }\n]\n```\n\nTo parse response object, execute `responses(from:)` of `Batch` instance.\nWhen `responses(from:)` is called, `Batch` finds corresponding response object by comparing request id and response id.\nAfter it find the response object, it executes `responses(from:)` of `Response` to get `Request.Response` from the response object.\n\n```swift\nlet responseObject = ...\nlet (response1, response2) = try! batch.responses(from: responseObject)\nprint(response1) // 19\nprint(response2) // -19\n```\n\n## JSON-RPC over HTTP by [APIKit](https://github.com/ishkawa/APIKit)\n\nAPIKit is a type-safe networking abstraction layer.\n\n### Defining HTTP request type\n\nAPIKit also has `RequestType` that represents HTTP request.\n\n```swift\nimport APIKit\n\nstruct MyServiceRequest\u003cBatch: JSONRPCKit.Batch\u003e: APIKit.Request {\n    let batch: Batch\n\n    typealias Response = Batch.Responses\n\n    var baseURL: URL {\n        return URL(string: \"https://api.example.com/\")!\n    }\n\n    var method: HTTPMethod {\n        return .post\n    }\n\n    var path: String {\n        return \"/\"\n    }\n\n    var parameters: Any? {\n        return batch.requestObject\n    }\n\n    func response(from object: Any, urlResponse: HTTPURLResponse) throws -\u003e Response {\n        return try batch.responses(from: object)\n    }\n}\n```\n\n### Sending HTTP/HTTPS request\n\n```swift\nlet batchFactory = BatchFactory(version: \"2.0\", idGenerator: NumberIdGenerator())\nlet request1 = Subtract(minuend: 42, subtrahend: 23)\nlet request2 = Subtract(minuend: 23, subtrahend: 42)\nlet batch = batchFactory.create(request1, request2)\nlet httpRequest = MyServiceRequest(batch: batch)\n\nSession.sendRequest(httpRequest) { result in\n    switch result {\n    case .Success(let response1, let response2):\n        print(response1.count) // CountCharactersResponse\n        print(response2.count) // CountCharactersResponse\n\n    case .Failure(let error):\n        print(error)\n    }\n}\n```\n\n## License\n\nJSONRPCKit is released under the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbricklife%2FJSONRPCKit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbricklife%2FJSONRPCKit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbricklife%2FJSONRPCKit/lists"}