Skip to content

Commit

Permalink
feat: add support for official cloudevent sdk
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrik Heil <[email protected]>
  • Loading branch information
hendrikheil committed Apr 14, 2022
1 parent d17e37e commit 0ea3176
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/lib/PubSub/CloudEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
use Exception;
use InvalidArgumentException;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Deprecated;
use LogicException;

/**
* Class CloudEvent
* @package Dapr\PubSub
* @deprecated 1.3.0 Replaced by cloudevents/sdk-php
*/
#[Deprecated(since: '1.3.0', replacement: \CloudEvents\V1\CloudEvent::class)]
class CloudEvent implements IDeserialize
{
/**
Expand Down
12 changes: 9 additions & 3 deletions src/lib/PubSub/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Dapr\exceptions\DaprException;
use JetBrains\PhpStorm\Deprecated;
use Psr\Log\LoggerInterface;
use CloudEvents\V1\CloudEventInterface;
use CloudEvents\Serializers\JsonSerializer;

/**
* Class Topic
Expand All @@ -25,7 +27,7 @@ public function __construct(
/**
* Publish an event to the topic
*
* @param CloudEvent|mixed $event The event to publish
* @param CloudEventInterface|CloudEvent|mixed $event The event to publish
* @param array|null $metadata Additional metadata to pass to the component
* @param string $content_type The header to include in the publish request. Ignored when $event is a CloudEvent
*
Expand All @@ -38,13 +40,17 @@ public function publish(mixed $event, ?array $metadata = null, string $content_t
} elseif ($this->client instanceof NewClient) {
$this->client->logger->debug('Sending {event} to {topic}', ['event' => $event, 'topic' => $this->topic]);
}
if ($event instanceof CloudEvent) {
if ($event instanceof CloudEvent || $event instanceof CloudEventInterface) {
$content_type = 'application/cloudevents+json';
$this->client->extra_headers = [
'Content-Type: application/cloudevents+json',
];

$event = $event->to_array();
if ($event instanceof CloudEvent) {
$event = $event->to_array();
} elseif ($event instanceof CloudEventInterface) {
$event = json_decode(JsonSerializer::create()->serializeStructured($event), true);
}
}

if ($this->client instanceof DaprClient) {
Expand Down

0 comments on commit 0ea3176

Please sign in to comment.