Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ async def submit_uri(
self,
request: Optional[Union[webrisk.SubmitUriRequest, dict]] = None,
*,
parent: Optional[str] = None,
submission: Optional[webrisk.Submission] = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = (),
Expand Down Expand Up @@ -867,6 +869,21 @@ async def sample_submit_uri():
request (Optional[Union[google.cloud.webrisk_v1.types.SubmitUriRequest, dict]]):
The request object. Request to send a potentially
malicious URI to WebRisk.
parent (:class:`str`):
Required. The name of the project that is making the
submission. This string is in the format
"projects/{project_number}".

This corresponds to the ``parent`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
submission (:class:`google.cloud.webrisk_v1.types.Submission`):
Required. The submission that
contains the URI to be scanned.

This corresponds to the ``submission`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
Expand All @@ -885,11 +902,30 @@ async def sample_submit_uri():

"""
# Create or coerce a protobuf request object.
# - Quick check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
flattened_params = [parent, submission]
has_flattened_params = (
len([param for param in flattened_params if param is not None]) > 0
)
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
)

# - Use the request object if provided (there's no risk of modifying the input as
# there are no flattened fields), or create one.
if not isinstance(request, webrisk.SubmitUriRequest):
request = webrisk.SubmitUriRequest(request)

# If we have keyword arguments corresponding to fields on the
# request, apply these.
if parent is not None:
request.parent = parent
if submission is not None:
request.submission = submission

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = self._client._transport._wrapped_methods[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,8 @@ def submit_uri(
self,
request: Optional[Union[webrisk.SubmitUriRequest, dict]] = None,
*,
parent: Optional[str] = None,
submission: Optional[webrisk.Submission] = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, Union[str, bytes]]] = (),
Expand Down Expand Up @@ -1258,6 +1260,21 @@ def sample_submit_uri():
request (Union[google.cloud.webrisk_v1.types.SubmitUriRequest, dict]):
The request object. Request to send a potentially
malicious URI to WebRisk.
parent (str):
Required. The name of the project that is making the
submission. This string is in the format
"projects/{project_number}".

This corresponds to the ``parent`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
submission (google.cloud.webrisk_v1.types.Submission):
Required. The submission that
contains the URI to be scanned.

This corresponds to the ``submission`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
Expand All @@ -1276,10 +1293,28 @@ def sample_submit_uri():

"""
# Create or coerce a protobuf request object.
# - Quick check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
flattened_params = [parent, submission]
has_flattened_params = (
len([param for param in flattened_params if param is not None]) > 0
)
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
)

# - Use the request object if provided (there's no risk of modifying the input as
# there are no flattened fields), or create one.
if not isinstance(request, webrisk.SubmitUriRequest):
request = webrisk.SubmitUriRequest(request)
# If we have keyword arguments corresponding to fields on the
# request, apply these.
if parent is not None:
request.parent = parent
if submission is not None:
request.submission = submission

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,8 @@ class Submission(proto.Message):


class ThreatInfo(proto.Message):
r"""Context about the submission including the type of abuse found on
the URI and supporting details. option
(google.api.message_visibility).restriction = "TRUSTED_TESTER";
r"""Context about the submission including the type of abuse
found on the URI and supporting details.

Attributes:
abuse_type (google.cloud.webrisk_v1.types.ThreatInfo.AbuseType):
Expand Down Expand Up @@ -835,8 +834,7 @@ class SubmitUriRequest(proto.Message):


class SubmitUriMetadata(proto.Message):
r"""Metadata for the Submit URI long-running operation. option
(google.api.message_visibility).restriction = "TRUSTED_TESTER";
r"""Metadata for the Submit URI long-running operation.

Attributes:
state (google.cloud.webrisk_v1.types.SubmitUriMetadata.State):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,14 @@
"name": "request",
"type": "google.cloud.webrisk_v1.types.SubmitUriRequest"
},
{
"name": "parent",
"type": "str"
},
{
"name": "submission",
"type": "google.cloud.webrisk_v1.types.Submission"
},
{
"name": "retry",
"type": "google.api_core.retry.Retry"
Expand Down Expand Up @@ -793,6 +801,14 @@
"name": "request",
"type": "google.cloud.webrisk_v1.types.SubmitUriRequest"
},
{
"name": "parent",
"type": "str"
},
{
"name": "submission",
"type": "google.cloud.webrisk_v1.types.Submission"
},
{
"name": "retry",
"type": "google.api_core.retry.Retry"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,98 @@ async def test_submit_uri_field_headers_async():
) in kw["metadata"]


def test_submit_uri_flattened():
client = WebRiskServiceClient(
credentials=ga_credentials.AnonymousCredentials(),
)

# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client.transport.submit_uri), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = operations_pb2.Operation(name="operations/op")
# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
client.submit_uri(
parent="parent_value",
submission=webrisk.Submission(uri="uri_value"),
)

# Establish that the underlying call was made with the expected
# request object values.
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
arg = args[0].parent
mock_val = "parent_value"
assert arg == mock_val
arg = args[0].submission
mock_val = webrisk.Submission(uri="uri_value")
assert arg == mock_val


def test_submit_uri_flattened_error():
client = WebRiskServiceClient(
credentials=ga_credentials.AnonymousCredentials(),
)

# Attempting to call a method with both a request object and flattened
# fields is an error.
with pytest.raises(ValueError):
client.submit_uri(
webrisk.SubmitUriRequest(),
parent="parent_value",
submission=webrisk.Submission(uri="uri_value"),
)


@pytest.mark.asyncio
async def test_submit_uri_flattened_async():
client = WebRiskServiceAsyncClient(
credentials=async_anonymous_credentials(),
)

# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(type(client.transport.submit_uri), "__call__") as call:
# Designate an appropriate return value for the call.
call.return_value = operations_pb2.Operation(name="operations/op")

call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(
operations_pb2.Operation(name="operations/spam")
)
# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
response = await client.submit_uri(
parent="parent_value",
submission=webrisk.Submission(uri="uri_value"),
)

# Establish that the underlying call was made with the expected
# request object values.
assert len(call.mock_calls)
_, args, _ = call.mock_calls[0]
arg = args[0].parent
mock_val = "parent_value"
assert arg == mock_val
arg = args[0].submission
mock_val = webrisk.Submission(uri="uri_value")
assert arg == mock_val


@pytest.mark.asyncio
async def test_submit_uri_flattened_error_async():
client = WebRiskServiceAsyncClient(
credentials=async_anonymous_credentials(),
)

# Attempting to call a method with both a request object and flattened
# fields is an error.
with pytest.raises(ValueError):
await client.submit_uri(
webrisk.SubmitUriRequest(),
parent="parent_value",
submission=webrisk.Submission(uri="uri_value"),
)


def test_compute_threat_list_diff_rest_use_cached_wrapped_rpc():
# Clients should use _prep_wrapped_messages to create cached wrapped rpcs,
# instead of constructing them on each call
Expand Down Expand Up @@ -3473,6 +3565,62 @@ def test_submit_uri_rest_unset_required_fields():
)


def test_submit_uri_rest_flattened():
client = WebRiskServiceClient(
credentials=ga_credentials.AnonymousCredentials(),
transport="rest",
)

# Mock the http request call within the method and fake a response.
with mock.patch.object(type(client.transport._session), "request") as req:
# Designate an appropriate value for the returned response.
return_value = operations_pb2.Operation(name="operations/spam")

# get arguments that satisfy an http rule for this method
sample_request = {"parent": "projects/sample1"}

# get truthy value for each flattened field
mock_args = dict(
parent="parent_value",
submission=webrisk.Submission(uri="uri_value"),
)
mock_args.update(sample_request)

# Wrap the value into a proper Response obj
response_value = Response()
response_value.status_code = 200
json_return_value = json_format.MessageToJson(return_value)
response_value._content = json_return_value.encode("UTF-8")
req.return_value = response_value
req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"}

client.submit_uri(**mock_args)

# Establish that the underlying call was made with the expected
# request object values.
assert len(req.mock_calls) == 1
_, args, _ = req.mock_calls[0]
assert path_template.validate(
"%s/v1/{parent=projects/*}/uris:submit" % client.transport._host, args[1]
)


def test_submit_uri_rest_flattened_error(transport: str = "rest"):
client = WebRiskServiceClient(
credentials=ga_credentials.AnonymousCredentials(),
transport=transport,
)

# Attempting to call a method with both a request object and flattened
# fields is an error.
with pytest.raises(ValueError):
client.submit_uri(
webrisk.SubmitUriRequest(),
parent="parent_value",
submission=webrisk.Submission(uri="uri_value"),
)


def test_credentials_transport_error():
# It is an error to provide credentials and a transport instance.
transport = transports.WebRiskServiceGrpcTransport(
Expand Down