cats-oss/grpc-swift-client
:repeat: Client-side library that depends on SwiftGRPC which is a library of gRPC written in Swift.
SwiftGRPCClient
If you use SwiftGRPC, you can do Unary connection using generated protocol or struct as follows.
let service = Echo_EchoServiceClient(address: "YOUR_SERVER_ADDRESS")
var requestMessage = Echo_EchoRequest()
requestMessage.text = "message"
_ = try? service.get(requestMessage) { responseMessage, callResult in
}The get method above can get a message by sending arbitrary message, but with this method you can not get the information of the logged-in user. For example, if you want to get user information, you will need to prepare the following methods.
var requestUser = Example_UserRequest()
requestUser.id = "user_id"
_ = try? service.getUser(requestUser) { responseUser, callResult in
}In this way, when connecting using a certain request, a special method is required to execute the request.
With SwiftGRPCClient, data is the only method to make a Unary request.
let session = Session(address: "YOUR_SERVER_ADDRESS")
session.stream(with: EchoUnaryRequest(text: "message"))
.data { result in
}It is possible to get the user's login information just by changing the request.
session.stream(with: GetUserRequest(id: "user_id"))
.data { result in
}See also SwiftGRPCClient document.
Requirements
- Swift 5.0
- SwiftGRPC 0.9.1
How to Install
CocoaPods
Add the following to your Podfile:
pod 'SwiftGRPCClient'protoc-gen-swiftgrpc-client
protoc-gen-swiftgrpc-client is a plugin for Protocol Buffers. It automatically defines requests, responses and methods used when connecting using SwiftGRPCClient.
See also protoc-gen-swiftgrpc-client document.
Requirements
- Swift 5.0
- SwiftProtobuf 1.5.0
How to get plugin
Execute the following command.
$ make genExplain generated code
As an example, prepare the following .proto.
syntax = "proto3";
package echo;
service Echo {
rpc Get(EchoRequest) returns (EchoResponse) {}
}
message EchoRequest {
string text = 1;
}
message EchoResponse {
string text = 1;
}protoc creates .swift file.
// MARK: - Echo Request Method
enum Echo_EchoMethod: String, CallMethod {
case get = "Get"
static let service = "echo.Echo"
}
// MARK: - Echo_Echo Get Request
protocol _Echo_EchoGetRequest {
typealias InputType = Echo_EchoRequest
typealias OutputType = Echo_EchoResponse
}
protocol Echo_EchoGetRequest: _Echo_EchoGetRequest, UnaryRequest {}
extension Echo_EchoGetRequest {
var method: CallMethod {
return Echo_EchoMethod.get
}
}Define the Request object using protocol in the generated .swift.
struct EchoGetRequest: Echo_EchoGetRequest {
var request = Echo_EchoRequest()
init(text: String) {
request.text = text
}
}LICENSE
Under the MIT license. See LICENSE file for details.
Package Metadata
Repository: cats-oss/grpc-swift-client
Stars: 50
Forks: 7
Open issues: 3
Default branch: master
Primary language: swift
License: MIT
Topics: client-side, grpc, ios, swift
README: README.md