Skip to content

Commit

Permalink
fix: add support for protobuf 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea committed Apr 27, 2024
1 parent c38dd1e commit a650198
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 43 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
run: |
python -m pip install --upgrade setuptools pip wheel
python -m pip install nox
- name: Install pytest
run: |
python -m pip install pytest
- name: Run docs
run: |
nox -s docs
Expand All @@ -33,6 +36,9 @@ jobs:
run: |
python -m pip install --upgrade setuptools pip wheel
python -m pip install nox
- name: Install pytest
run: |
python -m pip install pytest
- name: Run docfx
run: |
nox -s docfx
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
run: |
python -m pip install --upgrade setuptools pip wheel
python -m pip install nox
- name: Install pytest
run: |
python -m pip install pytest
- name: Run lint
run: |
nox -s lint
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
run: |
python -m pip install --upgrade setuptools pip wheel
python -m pip install nox
- name: Install pytest
run: |
python -m pip install pytest
- name: Run mypy
run: |
nox -s mypy
15 changes: 14 additions & 1 deletion .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
option: ["", "_grpc_gcp", "_wo_grpc"]
option: ["", "_grpc_gcp", "_wo_grpc", "_prerelease"]
python:
- "3.7"
- "3.8"
Expand All @@ -26,6 +26,16 @@ jobs:
python: 3.8
- option: "_wo_grpc"
python: 3.9
- option: "_prerelease"
python: 3.7
- option: "_prerelease"
python: 3.8
- option: "_prerelease"
python: 3.9
- option: "_prerelease"
python: 3.10
- option: "_prerelease"
python: 3.11
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -37,6 +47,9 @@ jobs:
run: |
python -m pip install --upgrade setuptools pip wheel
python -m pip install nox
- name: Install pytest
run: |
python -m pip install pytest
- name: Run unit tests
env:
COVERAGE_FILE: .coverage${{ matrix.option }}-${{matrix.python }}
Expand Down
91 changes: 66 additions & 25 deletions google/api_core/operations_v1/transports/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@
from google.longrunning import operations_pb2 # type: ignore
from google.protobuf import empty_pb2 # type: ignore
from google.protobuf import json_format # type: ignore
import google.protobuf

import grpc
from .base import DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO, OperationsTransport

PROTOBUF_VERSION = google.protobuf.__version__

OptionalRetry = Union[retries.Retry, object]

DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down Expand Up @@ -184,11 +188,22 @@ def _list_operations(
"google.longrunning.Operations.ListOperations"
]

request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True,
)
# For backwards compatibility with protobuf 3.x 4.x
# Remove once support for protobuf 3.x and 4.x is dropped
# https://github.com/googleapis/python-api-core/issues/643
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True, # type: ignore # backward compatibility
)
else:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
always_print_fields_with_no_presence=True,
)

transcoded_request = path_template.transcode(http_options, **request_kwargs)

uri = transcoded_request["uri"]
Expand All @@ -199,7 +214,6 @@ def _list_operations(
json_format.ParseDict(transcoded_request["query_params"], query_params_request)
query_params = json_format.MessageToDict(
query_params_request,
including_default_value_fields=False,
preserving_proto_field_name=False,
use_integers_for_enums=False,
)
Expand Down Expand Up @@ -265,11 +279,22 @@ def _get_operation(
"google.longrunning.Operations.GetOperation"
]

request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True,
)
# For backwards compatibility with protobuf 3.x 4.x
# Remove once support for protobuf 3.x and 4.x is dropped
# https://github.com/googleapis/python-api-core/issues/643
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True, # type: ignore # backward compatibility
)
else:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
always_print_fields_with_no_presence=True,
)

transcoded_request = path_template.transcode(http_options, **request_kwargs)

uri = transcoded_request["uri"]
Expand All @@ -280,7 +305,6 @@ def _get_operation(
json_format.ParseDict(transcoded_request["query_params"], query_params_request)
query_params = json_format.MessageToDict(
query_params_request,
including_default_value_fields=False,
preserving_proto_field_name=False,
use_integers_for_enums=False,
)
Expand Down Expand Up @@ -339,11 +363,21 @@ def _delete_operation(
"google.longrunning.Operations.DeleteOperation"
]

request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True,
)
# For backwards compatibility with protobuf 3.x 4.x
# Remove once support for protobuf 3.x and 4.x is dropped
# https://github.com/googleapis/python-api-core/issues/643
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True, # type: ignore # backward compatibility
)
else:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
always_print_fields_with_no_presence=True,
)
transcoded_request = path_template.transcode(http_options, **request_kwargs)

uri = transcoded_request["uri"]
Expand All @@ -354,7 +388,6 @@ def _delete_operation(
json_format.ParseDict(transcoded_request["query_params"], query_params_request)
query_params = json_format.MessageToDict(
query_params_request,
including_default_value_fields=False,
preserving_proto_field_name=False,
use_integers_for_enums=False,
)
Expand Down Expand Up @@ -411,19 +444,28 @@ def _cancel_operation(
"google.longrunning.Operations.CancelOperation"
]

request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True,
)
# For backwards compatibility with protobuf 3.x 4.x
# Remove once support for protobuf 3.x and 4.x is dropped
# https://github.com/googleapis/python-api-core/issues/643
if PROTOBUF_VERSION[0:2] in ["3.", "4."]:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
including_default_value_fields=True, # type: ignore # backward compatibility
)
else:
request_kwargs = json_format.MessageToDict(
request,
preserving_proto_field_name=True,
always_print_fields_with_no_presence=True,
)
transcoded_request = path_template.transcode(http_options, **request_kwargs)

# Jsonify the request body
body_request = operations_pb2.CancelOperationRequest()
json_format.ParseDict(transcoded_request["body"], body_request)
body = json_format.MessageToDict(
body_request,
including_default_value_fields=False,
preserving_proto_field_name=False,
use_integers_for_enums=False,
)
Expand All @@ -435,7 +477,6 @@ def _cancel_operation(
json_format.ParseDict(transcoded_request["query_params"], query_params_request)
query_params = json_format.MessageToDict(
query_params_request,
including_default_value_fields=False,
preserving_proto_field_name=False,
use_integers_for_enums=False,
)
Expand Down
Loading

0 comments on commit a650198

Please sign in to comment.