func wait(client *speech.Client, opName string) (*speechpb.AsyncRecognizeResponse, error) {
	ctx := context.Background()

	opClient := longrunningpb.NewOperationsClient(client.Connection())
	var op *longrunningpb.Operation
	var err error
	for {
		op, err = opClient.GetOperation(ctx, &longrunningpb.GetOperationRequest{
			Name: opName,
		})
		if err != nil {
			return nil, err
		}
		if op.Done {
			break
		}
		time.Sleep(500 * time.Millisecond)
	}

	switch {
	case op.GetError() != nil:
		return nil, fmt.Errorf("recieved error in response: %v", op.GetError())
	case op.GetResponse() != nil:
		var resp speechpb.AsyncRecognizeResponse
		if err := proto.Unmarshal(op.GetResponse().Value, &resp); err != nil {
			return nil, err
		}
		return &resp, nil
	}

	// should never happen.
	return nil, errors.New("no response")
}
示例#2
0
// InternalNewOperation is for use by the google Cloud Libraries only.
//
// InternalNewOperation returns an long-running operation, abstracting the raw pb.Operation.
// The conn parameter refers to a server that proto was received from.
func InternalNewOperation(conn *grpc.ClientConn, proto *pb.Operation) *Operation {
	return &Operation{
		c:     pb.NewOperationsClient(conn),
		proto: proto,
	}
}