Giter Club home page Giter Club logo

grpc-swift-client's Introduction

Swift gRPC Client

Version License Platform

Client-side library that depends on SwiftGRPC which is a library of gRPC written in Swift. Basically it is used the function of Core part of SwiftGRPC, but it is made to make client implementation easier.


โš ๏ธ WARNING : If there is the breaking change in SwiftGRPC, this library may not be updatable.


The following two modules are included.

SwiftGRPCClient

It is a plugin to use when running at runtime. Link to the application or framework.

protoc-gen-swiftgrpc-client

It is a Protocol Buffer's plugin for creating the functions necessary to use SwiftGRPCClient. Use protoc to generate .swift from .proto.

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 gen

Explain 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.

grpc-swift-client's People

Contributors

kane-liu avatar kyoheig3 avatar shoheiyokoyama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grpc-swift-client's Issues

Add Tests

It should add test and check coverage.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.