SwiftGRPCClient 0.7.2

SwiftGRPCClient 0.7.2

Kyohei Ito 维护。



  • 作者:
  • Kyohei Ito

Swift gRPC Client

Carthage compatible Version License Platform

这是一个客户端库,它依赖于 SwiftGRPC,这是一个用 Swift 编写的 gRPC 库。基本上它使用了 SwiftGRPCCore 部分的函数,但它是为了使客户端实现更简单而创建的。


⚠️ 警告:如果 SwiftGRPC 有破坏性变更,此库可能无法更新。


包含以下两个模块。

SwiftGRPCClient

这是一个在运行时使用的插件。链接到应用程序或框架。

protoc-gen-swiftgrpc-client

它是生成使用 SwiftGRPCClient 所需函数的 Protocol Buffer 插件。使用 protoc.proto 生成 .swift

SwiftGRPCClient

如果您使用 SwiftGRPC,您可以使用生成的 protocolstruct 进行如下 Unary 连接。

let service = Echo_EchoServiceClient(address: "YOUR_SERVER_ADDRESS")
var requestMessage = Echo_EchoRequest()
requestMessage.text = "message"
_ = try? service.get(requestMessage) { responseMessage, callResult in
}

上面的 get 方法可以通过发送任意的 message 来获取 message,但使用这种方法您无法获取登录用户的详细信息。例如,如果您想获取用户信息,您将需要准备以下方法。

var requestUser = Example_UserRequest()
requestUser.id = "user_id"
_ = try? service.getUser(requestUser) { responseUser, callResult in
}

这样,在使用特定的请求连接时,需要使用特殊的方法来执行请求。

SwiftGRPCClient 中,data 是唯一用于执行 Unary 请求的方法。

let session = Session(address: "YOUR_SERVER_ADDRESS")
session.stream(with: EchoUnaryRequest(text: "message"))
    .data { result in
    }

只需改变请求即可获取用户的登录信息。

session.stream(with: GetUserRequest(id: "user_id"))
    .data { result in
    }

请参阅 SwiftGRPCClient 文档。

需求

  • Swift 5.0
  • SwiftGRPC 0.9.1

安装方法

CocoaPods

将以下内容添加到您的 Podfile

pod 'SwiftGRPCClient'

Carthage

将以下内容添加到您的 Cartfile

github "cats-oss/grpc-swift-client"

然后,将 BoringSSLCgRPCSwiftProtobufSwiftGRPCSwiftGRPCClient 添加到链接二进制文件中,并运行 carthage copy-frameworks

protoc-gen-swiftgrpc-client

protoc-gen-swiftgrpc-clientProtocol Buffers 的插件。它自动定义了在使用 SwiftGRPCClient 连接时使用的请求、响应和方法。

请参阅 protoc-gen-swiftgrpc-client 文档。

要求

  • Swift 5.0
  • SwiftProtobuf 1.5.0

如何获取插件

执行以下命令。

$ make gen

解释生成的代码

例如,准备以下 .proto 文件。

syntax = "proto3";

package echo;

service Echo {
    rpc Get(EchoRequest) returns (EchoResponse) {}
}

message EchoRequest {
    string text = 1;
}

message EchoResponse {
    string text = 1;
}

protoc 生成 .swift 文件。

// 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
    }
}

在生成的 .swift 文件中使用 protocol 定义 Request 对象。

struct EchoGetRequest: Echo_EchoGetRequest {
    var request = Echo_EchoRequest()

    init(text: String) {
        request.text = text
    }
}

许可证

在 MIT 许可证下。详细信息请见 LICENSE 文件。