如何在iOS应用中快速进行gRPC调用

问题描述 投票:0回答:1

我正在尝试使用https://github.com/grpc/grpc-swift访问某些Google Cloud RPC API)>

我在做什么与Natural Language Example非常相似。我可以从master分支获得使用0.10.0版本的版本,但自述文件说该分支已被弃用,应使用'nio'分支。不幸的是,示例代码与项目不同步,因此无法编译。我尽力修复了该代码,但无法使其正常工作。

class NaturalLanguageRPCTests {
    let authToken : String
    var service : Google_Cloud_Language_V1_LanguageServiceServiceClient?
    var clientConnection : ClientConnection?
    let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)

    init(authToken: String) {
        self.authToken = authToken
    }

    func createService() {
        let host = "language.googleapis.com"
        let port = 443

        let tls = ClientConnection.Configuration.TLS(configuration: TLSConfiguration.forClient(applicationProtocols: ["h2"]))
        let configuration = ClientConnection.Configuration(target: .hostAndPort(host, port),
                                                           eventLoopGroup: self.eventLoopGroup,
                                                           errorDelegate: self,
                                                           connectivityStateDelegate: self,
                                                           tls: tls)
                                                           //connectionBackoff: ConnectionBackoff?)
        self.clientConnection = ClientConnection(configuration: configuration)
    }



    func makeRPCCall() {
        guard let connection = self.clientConnection, connection.connectivity.state == .ready else {
            fatalError()
        }
        if self.service == nil {
            self.service = Google_Cloud_Language_V1_LanguageServiceServiceClient(connection: connection)
        }
        guard let service = self.service else {
            print("SERVICE HASN'T BEEN CREATED YET")
            return
        }
        // Use CallOptions to send the auth token (necessary) and set a custom timeout (optional).
        let http_headers = HTTPHeaders([("authorization", "Bearer " + self.authToken)])
        let headers = HPACKHeaders(httpHeaders: http_headers)
        let timeout = try! GRPCTimeout.seconds(30)
        let callOptions = CallOptions(customMetadata: headers, timeout: timeout)
        print("CALL OPTIONS\n\(callOptions)\n")

        // Construct the API request.
        var document = Google_Cloud_Language_V1_Document()
        document.type = .plainText
        document.content = "The Caterpillar and Alice looked at each other for some time in silence: at last the Caterpillar took the hookah out of its mouth, and addressed her in a languid, sleepy voice. `Who are you?' said the Caterpillar."

        var features = Google_Cloud_Language_V1_AnnotateTextRequest.Features()
        features.extractSyntax = true
        features.extractEntities = true
        features.extractDocumentSentiment = true
        features.extractEntitySentiment = true
        features.classifyText = true

        var request = Google_Cloud_Language_V1_AnnotateTextRequest()
        request.document = document
        request.features = features
        print("REQUEST MESSAGE\n\(request)")


        // Create/start the API call.
        let call = service.annotateText(request, callOptions: callOptions)
        call.response.whenSuccess { response in
            print("CALL SUCCEEDED WITH RESPONSE\n\(response)")
        }
        call.response.whenFailure { error in
            print("CALL FAILED WITH ERROR\n\(error)")
        }
        do {
            // wait() on the status to stop the program from exiting.
            let status = try call.status.wait()
            print("CALL STATUS\n\(status)")
        } catch {
            print("EXAMPLE FAILED WITH ERROR\n\(error)")
        }
    }
}

当我打电话时test.createService()并监视连接状态,它转换为“就绪”,因此我认为正在建立连接。

但是,当我打电话给test.makeRPCCall()时,我得到了错误。从日志中:

2019-11-29T20:10:23-0800 info: path=/google.cloud.language.v1.LanguageService/AnnotateText 
request_id=0EAC0A92-2842-4D5A-BC94-B0BD4D6171EF starting rpc

CALL STATUS
unimplemented (12): invalid HTTP status: Not Found
2019-11-29T20:10:48-0800 info: duration_ms=46 request_id=0EAC0A92-2842-4D5A-BC94-B0BD4D6171EF status_code=12 rpc call finished
CALL FAILED WITH ERROR
unimplemented (12): invalid HTTP status: Not Found


我确信这是我做错的简单事情,但我无法一生为我工作。如果有人对此有所了解,我将非常感谢您的帮助。谢谢。

我正在尝试使用https://github.com/grpc/grpc-swift访问某些google cloud RPCs API,与我的自然语言示例非常相似。我能够获得使用...

ios swift grpc
1个回答
0
投票

回答我自己的问题。

热门问题
推荐问题
最新问题