Skip to content

Commit

Permalink
✨ add client cli. #17
Browse files Browse the repository at this point in the history
  • Loading branch information
perillaroc committed Apr 20, 2022
1 parent 547599f commit 1b908ac
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
Empty file added takler/client/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions takler/client/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from takler.client.cli import app


if __name__ == "__main__":
app()
42 changes: 42 additions & 0 deletions takler/client/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import typer

from takler.client.service_client import TaklerServiceClient


app = typer.Typer()


@app.command()
def init(
task_id: str = typer.Option(...),
node_path: str = typer.Option(...),
host: str = typer.Option(None, help="takler service host"),
port: int = typer.Option(None, help="takler service port"),
):
client = TaklerServiceClient(host=host, port=port)
client.start()
client.run_command_init(node_path=node_path, task_id=task_id)
client.shutdown()


@app.command()
def complete(
node_path: str = typer.Option(...),
host: str = typer.Option(None, help="takler service host"),
port: int = typer.Option(None, help="takler service port"),
):
client = TaklerServiceClient(host=host, port=port)
client.start()
client.run_command_complete(node_path=node_path)
client.shutdown()


@app.command()
def show(
host: str = typer.Option(None, help="takler service host"),
port: int = typer.Option(None, help="takler service port"),
):
client = TaklerServiceClient(host=host, port=port)
client.start()
client.run_request_show()
client.shutdown()
71 changes: 71 additions & 0 deletions takler/client/service_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import asyncio
from typing import Optional

import grpc

from takler.server.protocol.takler_pb2_grpc import TaklerServerStub
from takler.server.protocol import takler_pb2
from takler.logging import get_logger


logger = get_logger("client")


class TaklerServiceClient:
def __init__(self, host: str = "localhost", port: int = 50051):
self.host = host # type: str
self.port = port # type: int
self.channel = None # type: Optional[grpc.Channel]
self.stub = None # type: Optional[TaklerServerStub]

@property
def listen_address(self) -> str:
"""
str: gRPC server's listen address
"""
return f'{self.host}:{self.port}'

def create_channel(self):
self.channel = grpc.insecure_channel(self.listen_address)

def close_channel(self):
self.channel.close()
self.channel = None

def create_stub(self):
self.stub = TaklerServerStub(self.channel)
return self.stub

def start(self):
self.create_channel()
self.create_stub()

def shutdown(self):
self.close_channel()

def run_command_init(self, node_path: str, task_id: str):
response = self.stub.RunInitCommand(
takler_pb2.InitCommand(
child_options=takler_pb2.ChildCommandOptions(
node_path=node_path,
),
task_id=task_id
)
)
print(f"received: {response.flag}")

def run_command_complete(self, node_path: str):
response = self.stub.RunCompleteCommand(
takler_pb2.CompleteCommand(
child_options=takler_pb2.ChildCommandOptions(
node_path=node_path,
)
)
)
print(f"received: {response.flag}")

def run_request_show(self):
response = self.stub.RunShowRequest(
takler_pb2.ShowRequest()
)
print(response.output)

0 comments on commit 1b908ac

Please sign in to comment.