-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from JoshYuJump/main
Reduced the client code size (breaking changes from 1.x)
- Loading branch information
Showing
8 changed files
with
142 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ indent_style = space | |
tab_width = 2 | ||
|
||
[*.py] | ||
max_line_length = 100 | ||
max_line_length = 99 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[style] | ||
based_on_style = facebook | ||
COLUMN_LIMIT = 100 | ||
COLUMN_LIMIT = 99 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '1.1.3' | ||
__version__ = '2.0.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,41 @@ | ||
import logging | ||
# Generated by the bali-cli 2.x. DO NOT EDIT! | ||
|
||
from grpc import insecure_channel | ||
from google.protobuf.empty_pb2 import Empty | ||
import logging | ||
|
||
from . import {{ filename }}_pb2, {{ filename }}_pb2_grpc, {{ filename }}_schema | ||
from . import ( | ||
{{ filename }}_pb2 as pb2, | ||
{{ filename }}_pb2_grpc as pb2_grpc, | ||
{{ filename }}_schema as schemas, | ||
) | ||
from .._config import GRPC_ADDRESS, GRPC_LOGGER, GRPC_CACHE | ||
from .._utils import MessageToDict, ParseDict, make_cache_key | ||
from .._utils import ClientMixin | ||
|
||
try: | ||
from .._config import GRPC_CHANNEL_OPTIONS | ||
except ImportError: | ||
GRPC_CHANNEL_OPTIONS = None | ||
|
||
__all__ = ["{{ service | replace("Service", "") }}Client"] | ||
__all__ = ["{{ service_cls }}"] | ||
|
||
logger = logging.getLogger(GRPC_LOGGER) | ||
|
||
|
||
class {{ service | replace("Service", "") }}Client: | ||
class {{ service_cls }}(ClientMixin): | ||
URL = f"{GRPC_ADDRESS['host']}:{GRPC_ADDRESS['port']}" | ||
s = schemas = {{ filename }}_schema | ||
{% for method in methods %} | ||
def {{ method[0] | decamelize }}(self{% if method[1] == "google.protobuf.Empty" %}{% else %}, schema: {{ filename }}_schema.{{ method[1] }}{% endif %}, *, fail_silently=False, cache_timeout=0, refresh=False) -> {{ filename }}_schema.{{ method[2] }}: | ||
|
||
service_name, rpc_name = "{{ service }}", "{{ method[0] }}" | ||
if GRPC_CACHE: | ||
cache_key = make_cache_key(service_name, rpc_name, {% if method[1] != "google.protobuf.Empty" %}schema{% endif %}) | ||
if refresh: | ||
GRPC_CACHE.delete(cache_key) | ||
logger.info("gRPC client of %s.%s cache cleared: %s", service_name, rpc_name, cache_key) | ||
# check cache | ||
if cache_timeout: | ||
cached = GRPC_CACHE.get(cache_key) | ||
if cached is not None: | ||
logger.info("gRPC client of %s.%s received(hit cache): %s", service_name, rpc_name, cached) | ||
return cached | ||
CHANNEL_OPTIONS = GRPC_CHANNEL_OPTIONS | ||
pb2 = pb2 | ||
pb2_grpc = pb2_grpc | ||
|
||
with insecure_channel(self.URL, options=GRPC_CHANNEL_OPTIONS) as channel: | ||
stub = {{ filename }}_pb2_grpc.{{ service }}Stub(channel) | ||
service_name, rpc_name = "{{ service }}", "{{ method[0] }}" | ||
{% if method[1] == "google.protobuf.Empty" %} | ||
logger.info("gRPC client of %s.%s send nothing", service_name, rpc_name) | ||
request = Empty() | ||
{% else %} | ||
payload = schema.dict() | ||
logger.info("gRPC client of %s.%s send: %s", service_name, rpc_name, payload) | ||
request = ParseDict(payload, {{ filename }}_pb2.{{ method[1] }}()) | ||
{% endif %} | ||
try: | ||
response = stub.{{ method[0] }}(request) | ||
except Exception as e: | ||
logger.error("gRPC client of %s.%s exception: %s", service_name, rpc_name, e) | ||
if not fail_silently: | ||
raise | ||
logger.warning(repr(e)) | ||
reply = {} | ||
else: | ||
reply = MessageToDict(response, True, True) | ||
|
||
logger.info("gRPC client of %s.%s received: %s", service_name, rpc_name, reply) | ||
result = {{ filename }}_schema.{{ method[2] }}(**reply) | ||
# write cache | ||
if GRPC_CACHE and cache_timeout: | ||
GRPC_CACHE.set(cache_key, result, timeout=cache_timeout) | ||
|
||
return {{ filename }}_schema.{{ method[2] }}(**reply) | ||
logger = logger | ||
cache = GRPC_CACHE | ||
|
||
s = schemas | ||
schemas = schemas | ||
{% for method in methods %} | ||
def {{ method[0] | decamelize }}(self{% if method[1] == "google.protobuf.Empty" %}{% else %}, schema: schemas.{{ method[1] }}{% endif %}, *, fail_silently=False, cache_timeout=0, refresh=False) -> schemas.{{ method[2] }}: | ||
service, rpc_name, request_protobuf = "{{ service }}", "{{ method[0] }}", "{{ method[1] }}" | ||
response_schema_cls = schemas.{{ method[2] }} | ||
schema = {% if method[1] == "google.protobuf.Empty" %}None{% else %}schema{% endif %} | ||
cache_options = {'timeout': cache_timeout, 'refresh': refresh} | ||
return self.request(service, rpc_name, request_protobuf, schema, response_schema_cls, fail_silently, cache_options) | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-r requirements.txt | ||
|
||
coverage==6.2 | ||
pytest==6.1.2 | ||
pytest-asyncio>=0.15.0 | ||
pytest-cov==2.12.0 | ||
twine==3.2.0 | ||
wheel==0.35.1 |