Skip to content

Commit

Permalink
Merge pull request #77 from wesky93/viridianforge/prep-1.17
Browse files Browse the repository at this point in the history
Viridianforge/prep 1.17
  • Loading branch information
ViridianForge authored Apr 23, 2024
2 parents 0731b5d + b47be5c commit 70cce7f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 123 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.17](https://github.com/wesky93/grpc_requests/releases/tag/v0.1.17) - 2024-04-22

## Added

- Support for custom message parsing in both async and sync clients

## Removed

- Removed singular FileDescriptor getter methods and Method specific field descriptor
methods as laid out previously.

## [0.1.16](https://github.com/wesky93/grpc_requests/releases/tag/v0.1.16) - 2024-03-03

## Added
Expand Down
21 changes: 21 additions & 0 deletions src/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ result = await greeter.HelloEveryone(requests_data)
results = [x async for x in await greeter.SayHelloOneByOne(requests_data)]
```

## Setting a Client's message_to_dict behavior

By utilizing `CustomArgumentParsers`, behavioral arguments can be passed to
message_to_dict at time of Client instantiation. This is available for both
synchronous and asynchronous clients.

```python
client = Client(
"localhost:50051",
message_parsers=CustomArgumentParsers(
message_to_dict_kwargs={
"preserving_proto_field_name": True,
"including_default_value_fields": True,
}
),
)
yield client
```

[Review the json_format documentation for what kwargs are available to message_to_dict.](https://googleapis.dev/python/protobuf/latest/google/protobuf/json_format.html)

## Retrieving Information about a Server

All forms of clients expose methods to allow a user to query a server about its
Expand Down
2 changes: 1 addition & 1 deletion src/grpc_requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
)
from .client import Client, ReflectionClient, StubClient, get_by_endpoint

__version__ = "0.1.16"
__version__ = "0.1.17"
21 changes: 0 additions & 21 deletions src/grpc_requests/aio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import sys
import warnings
from enum import Enum
from functools import partial
from typing import (
Expand Down Expand Up @@ -511,26 +510,6 @@ async def _get_service_names(self):
services = tuple([s.name for s in resp.list_services_response.service])
return services

async def get_file_descriptor_by_name(self, name):
warnings.warn(
"This function is deprecated, and will be removed in the 0.1.17 release. Use get_file_descriptors_by_name() instead.",
DeprecationWarning,
)
request = reflection_pb2.ServerReflectionRequest(file_by_filename=name)
result = await self._reflection_single_request(request)
proto = result.file_descriptor_response.file_descriptor_proto[0]
return descriptor_pb2.FileDescriptorProto.FromString(proto)

async def get_file_descriptor_by_symbol(self, symbol):
warnings.warn(
"This function is deprecated, and will be removed in the 0.1.17 release. Use get_file_descriptors_by_symbol() instead.",
DeprecationWarning,
)
request = reflection_pb2.ServerReflectionRequest(file_containing_symbol=symbol)
result = await self._reflection_single_request(request)
proto = result.file_descriptor_response.file_descriptor_proto[0]
return descriptor_pb2.FileDescriptorProto.FromString(proto)

async def get_file_descriptors_by_name(self, name):
request = reflection_pb2.ServerReflectionRequest(file_by_filename=name)
result = await self._reflection_single_request(request)
Expand Down
30 changes: 1 addition & 29 deletions src/grpc_requests/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import sys
import warnings
from enum import Enum
from functools import partial
from typing import (
Expand All @@ -23,7 +22,7 @@
from google.protobuf.json_format import MessageToDict, ParseDict
from grpc_reflection.v1alpha import reflection_pb2, reflection_pb2_grpc

from .utils import describe_descriptor, describe_request, load_data
from .utils import describe_descriptor, load_data

if sys.version_info >= (3, 8):
import importlib.metadata
Expand Down Expand Up @@ -443,13 +442,6 @@ def get_service_descriptor(self, service):
"""
return self._desc_pool.FindServiceByName(service)

def describe_method_request(self, service, method):
warnings.warn(
"This function is deprecated, and will be removed in the 0.1.17 release. Use describe_descriptor() instead.",
DeprecationWarning,
)
return describe_request(self.get_method_descriptor(service, method))

def describe_request(self, service, method):
return describe_descriptor(
self.get_method_descriptor(service, method).input_type
Expand Down Expand Up @@ -525,26 +517,6 @@ def _get_service_names(self):
services = tuple([s.name for s in resp.list_services_response.service])
return services

def get_file_descriptor_by_name(self, name):
warnings.warn(
"This function is deprecated, and will be removed in the 0.1.17 release. Use get_file_descriptors_by_name() instead.",
DeprecationWarning,
)
request = reflection_pb2.ServerReflectionRequest(file_by_filename=name)
result = self._reflection_single_request(request)
proto = result.file_descriptor_response.file_descriptor_proto[0]
return descriptor_pb2.FileDescriptorProto.FromString(proto)

def get_file_descriptor_by_symbol(self, symbol):
warnings.warn(
"This function is deprecated, and will be removed in the 0.1.17 release. Use get_file_descriptors_by_symbol() instead.",
DeprecationWarning,
)
request = reflection_pb2.ServerReflectionRequest(file_containing_symbol=symbol)
result = self._reflection_single_request(request)
proto = result.file_descriptor_response.file_descriptor_proto[0]
return descriptor_pb2.FileDescriptorProto.FromString(proto)

def get_file_descriptors_by_name(self, name):
request = reflection_pb2.ServerReflectionRequest(file_by_filename=name)
result = self._reflection_single_request(request)
Expand Down
19 changes: 0 additions & 19 deletions src/grpc_requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
from google.protobuf.descriptor import (
Descriptor,
EnumDescriptor,
MethodDescriptor,
OneofDescriptor,
)

import warnings

# String descriptions of protobuf field types
FIELD_TYPES = [
Expand Down Expand Up @@ -37,23 +35,6 @@ def load_data(_path):
return data


def describe_request(method_descriptor: MethodDescriptor) -> dict:
"""
Provide a dictionary that describes the fields of a Method request
with a string description of their types.
:param method_descriptor: MethodDescriptor
:return: dict - a mapping of field names to their types
"""
warnings.warn(
"This function is deprecated, and will be removed in the 0.1.17 release. Use describe_descriptor() instead.",
DeprecationWarning,
)
description = {}
for field in method_descriptor.input_type.fields:
description[field.name] = FIELD_TYPES[field.type - 1]
return description


def describe_descriptor(descriptor: Descriptor, indent: int = 0) -> str:
"""
Prints a human readable description of a protobuf descriptor.
Expand Down
18 changes: 0 additions & 18 deletions src/tests/async_reflection_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,6 @@ async def test_get_service_descriptor():
assert service_descriptor.name == "Greeter"


@pytest.mark.asyncio
async def test_get_file_descriptor_by_name():
client = AsyncClient("localhost:50051")
file_descriptor = await client.get_file_descriptor_by_name("helloworld.proto")
assert file_descriptor.name == "helloworld.proto"
assert file_descriptor.package == "helloworld"
assert file_descriptor.syntax == "proto3"


@pytest.mark.asyncio
async def test_get_file_descriptor_by_symbol():
client = AsyncClient("localhost:50051")
file_descriptor = await client.get_file_descriptor_by_symbol("helloworld.Greeter")
assert file_descriptor.name == "helloworld.proto"
assert file_descriptor.package == "helloworld"
assert file_descriptor.syntax == "proto3"


@pytest.mark.asyncio
async def test_get_file_descriptors_by_name():
client = AsyncClient(
Expand Down
35 changes: 0 additions & 35 deletions src/tests/reflection_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,6 @@ def test_unary_unary(helloworld_reflection_client):
assert response == {"message": "Hello, sinsky!"}


def test_describe_method_request(client_tester_reflection_client):
request_description = client_tester_reflection_client.describe_method_request(
"client_tester.ClientTester", "TestUnaryUnary"
)
expected_request_description = {
"factor": "INT32",
"readings": "FLOAT",
"uuid": "UINT64",
"sample_flag": "BOOL",
"request_name": "STRING",
"extra_data": "BYTES",
}
assert (
request_description == expected_request_description
), f"Expected: {expected_request_description}, Actual: {request_description}"


def test_describe_request(client_tester_reflection_client):
request_description = client_tester_reflection_client.describe_request(
"client_tester.ClientTester", "TestUnaryUnary"
Expand Down Expand Up @@ -239,24 +222,6 @@ def test_get_service_descriptor(helloworld_reflection_client):
assert service_descriptor.name == "Greeter"


def test_get_file_descriptor_by_name(helloworld_reflection_client):
file_descriptor = helloworld_reflection_client.get_file_descriptor_by_name(
"helloworld.proto"
)
assert file_descriptor.name == "helloworld.proto"
assert file_descriptor.package == "helloworld"
assert file_descriptor.syntax == "proto3"


def test_get_file_descriptor_by_symbol(helloworld_reflection_client):
file_descriptor = helloworld_reflection_client.get_file_descriptor_by_symbol(
"helloworld.Greeter"
)
assert file_descriptor.name == "helloworld.proto"
assert file_descriptor.package == "helloworld"
assert file_descriptor.syntax == "proto3"


def test_get_file_descriptors_by_name():
client = Client("localhost:50053", descriptor_pool=descriptor_pool.DescriptorPool())
file_descriptor = client.get_file_descriptors_by_name("dependencies.proto")
Expand Down

0 comments on commit 70cce7f

Please sign in to comment.