Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [EventarcPublishing] Add publishing methods for channel resources #5226

Merged
merged 2 commits into from
Apr 21, 2022
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
15 changes: 10 additions & 5 deletions EventarcPublishing/metadata/V1/Publisher.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions EventarcPublishing/src/V1/Gapic/PublisherGapicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
use Google\Auth\FetchAuthTokenInterface;
use Google\Cloud\Eventarc\Publishing\V1\PublishChannelConnectionEventsRequest;
use Google\Cloud\Eventarc\Publishing\V1\PublishChannelConnectionEventsResponse;
use Google\Cloud\Eventarc\Publishing\V1\PublishEventsRequest;
use Google\Cloud\Eventarc\Publishing\V1\PublishEventsResponse;
use Google\Protobuf\Any;

/**
Expand Down Expand Up @@ -253,4 +255,63 @@ public function publishChannelConnectionEvents(array $optionalArgs = [])
$request
)->wait();
}

/**
* Publish events to a subscriber's channel.
*
* Sample code:
* ```
* $publisherClient = new PublisherClient();
* try {
* $response = $publisherClient->publishEvents();
* } finally {
* $publisherClient->close();
* }
* ```
*
* @param array $optionalArgs {
* Optional.
*
* @type string $channel
* The full name of the channel to publish to. For example:
* `projects/{project}/locations/{location}/channels/{channel-id}`.
* @type Any[] $events
* The CloudEvents v1.0 events to publish. No other types are allowed.
* @type RetrySettings|array $retrySettings
* Retry settings to use for this call. Can be a
* {@see Google\ApiCore\RetrySettings} object, or an associative array of retry
* settings parameters. See the documentation on
* {@see Google\ApiCore\RetrySettings} for example usage.
* }
*
* @return \Google\Cloud\Eventarc\Publishing\V1\PublishEventsResponse
*
* @throws ApiException if the remote call fails
*/
public function publishEvents(array $optionalArgs = [])
{
$request = new PublishEventsRequest();
$requestParamHeaders = [];
if (isset($optionalArgs['channel'])) {
$request->setChannel($optionalArgs['channel']);
$requestParamHeaders['channel'] = $optionalArgs['channel'];
}

if (isset($optionalArgs['events'])) {
$request->setEvents($optionalArgs['events']);
}

$requestParams = new RequestParamsHeaderDescriptor(
$requestParamHeaders
);
$optionalArgs['headers'] = isset($optionalArgs['headers'])
? array_merge($requestParams->getHeader(), $optionalArgs['headers'])
: $requestParams->getHeader();
return $this->startCall(
'PublishEvents',
PublishEventsResponse::class,
$optionalArgs,
$request
)->wait();
}
}
105 changes: 105 additions & 0 deletions EventarcPublishing/src/V1/PublishEventsRequest.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions EventarcPublishing/src/V1/PublishEventsResponse.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions EventarcPublishing/src/V1/PublisherGrpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,19 @@ public function PublishChannelConnectionEvents(\Google\Cloud\Eventarc\Publishing
$metadata, $options);
}

/**
* Publish events to a subscriber's channel.
* @param \Google\Cloud\Eventarc\Publishing\V1\PublishEventsRequest $argument input argument
* @param array $metadata metadata
* @param array $options call options
* @return \Grpc\UnaryCall
*/
public function PublishEvents(\Google\Cloud\Eventarc\Publishing\V1\PublishEventsRequest $argument,
$metadata = [], $options = []) {
return $this->_simpleRequest('/google.cloud.eventarc.publishing.v1.Publisher/PublishEvents',
$argument,
['\Google\Cloud\Eventarc\Publishing\V1\PublishEventsResponse', 'decode'],
$metadata, $options);
}

}
5 changes: 5 additions & 0 deletions EventarcPublishing/src/V1/gapic_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"methods": [
"publishChannelConnectionEvents"
]
},
"PublishEvents": {
"methods": [
"publishEvents"
]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
"timeout_millis": 60000,
"retry_codes_name": "no_retry_1_codes",
"retry_params_name": "no_retry_1_params"
},
"PublishEvents": {
"timeout_millis": 60000,
"retry_codes_name": "no_retry_codes",
"retry_params_name": "no_retry_params"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
],
],
],
'PublishEvents' => [
'method' => 'post',
'uriTemplate' => '/v1/{channel=projects/*/locations/*/channels/*}:publishEvents',
'body' => '*',
'placeholders' => [
'channel' => [
'getters' => [
'getChannel',
],
],
],
],
],
],
];
57 changes: 57 additions & 0 deletions EventarcPublishing/tests/Unit/V1/PublisherClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Google\ApiCore\Testing\MockTransport;
use Google\Cloud\Eventarc\Publishing\V1\PublishChannelConnectionEventsResponse;
use Google\Cloud\Eventarc\Publishing\V1\PublisherClient;
use Google\Cloud\Eventarc\Publishing\V1\PublishEventsResponse;
use Google\Rpc\Code;
use stdClass;

Expand Down Expand Up @@ -121,4 +122,60 @@ public function publishChannelConnectionEventsExceptionTest()
$transport->popReceivedCalls();
$this->assertTrue($transport->isExhausted());
}

/**
* @test
*/
public function publishEventsTest()
{
$transport = $this->createTransport();
$client = $this->createClient([
'transport' => $transport,
]);
$this->assertTrue($transport->isExhausted());
// Mock response
$expectedResponse = new PublishEventsResponse();
$transport->addResponse($expectedResponse);
$response = $client->publishEvents();
$this->assertEquals($expectedResponse, $response);
$actualRequests = $transport->popReceivedCalls();
$this->assertSame(1, count($actualRequests));
$actualFuncCall = $actualRequests[0]->getFuncCall();
$actualRequestObject = $actualRequests[0]->getRequestObject();
$this->assertSame('/google.cloud.eventarc.publishing.v1.Publisher/PublishEvents', $actualFuncCall);
$this->assertTrue($transport->isExhausted());
}

/**
* @test
*/
public function publishEventsExceptionTest()
{
$transport = $this->createTransport();
$client = $this->createClient([
'transport' => $transport,
]);
$this->assertTrue($transport->isExhausted());
$status = new stdClass();
$status->code = Code::DATA_LOSS;
$status->details = 'internal error';
$expectedExceptionMessage = json_encode([
'message' => 'internal error',
'code' => Code::DATA_LOSS,
'status' => 'DATA_LOSS',
'details' => [],
], JSON_PRETTY_PRINT);
$transport->addResponse(null, $status);
try {
$client->publishEvents();
// If the $client method call did not throw, fail the test
$this->fail('Expected an ApiException, but no exception was thrown.');
} catch (ApiException $ex) {
$this->assertEquals($status->code, $ex->getCode());
$this->assertEquals($expectedExceptionMessage, $ex->getMessage());
}
// Call popReceivedCalls to ensure the stub is exhausted
$transport->popReceivedCalls();
$this->assertTrue($transport->isExhausted());
}
}