diff --git a/cloudevents/proto/__init__.py b/cloudevents/proto/__init__.py new file mode 100644 index 00000000..8053ba9b --- /dev/null +++ b/cloudevents/proto/__init__.py @@ -0,0 +1,16 @@ +# Copyright 2018-Present The CloudEvents Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +from cloudevents.proto.cloudevents_pb2 import CloudEvent, CloudEventBatch + +__all__ = [CloudEvent, CloudEventBatch] diff --git a/cloudevents/proto/cloudevents.proto b/cloudevents/proto/cloudevents.proto new file mode 100644 index 00000000..7f6684ae --- /dev/null +++ b/cloudevents/proto/cloudevents.proto @@ -0,0 +1,69 @@ +/** + * CloudEvent Protobuf Format + * + * - Required context attributes are explicitly represented. + * - Optional and Extension context attributes are carried in a map structure. + * - Data may be represented as binary, text, or protobuf messages. + */ + +syntax = "proto3"; + +package io.cloudevents.v1; + +import "google/protobuf/any.proto"; +import "google/protobuf/timestamp.proto"; + +option csharp_namespace = "CloudNative.CloudEvents.V1"; +option go_package = "cloudevents.io/genproto/v1"; +option java_package = "io.cloudevents.v1.proto"; +option java_multiple_files = true; +option php_namespace = "Io\\CloudEvents\\V1\\Proto"; +option ruby_package = "Io::CloudEvents::V1::Proto"; + +message CloudEvent { + + // -- CloudEvent Context Attributes + + // Required Attributes + string id = 1; + string source = 2; // URI-reference + string spec_version = 3; + string type = 4; + + // Optional & Extension Attributes + map attributes = 5; + + // -- CloudEvent Data (Bytes, Text, or Proto) + oneof data { + bytes binary_data = 6; + string text_data = 7; + google.protobuf.Any proto_data = 8; + } + + /** + * The CloudEvent specification defines + * seven attribute value types... + */ + + message CloudEventAttributeValue { + + oneof attr { + bool ce_boolean = 1; + int32 ce_integer = 2; + string ce_string = 3; + bytes ce_bytes = 4; + string ce_uri = 5; + string ce_uri_ref = 6; + google.protobuf.Timestamp ce_timestamp = 7; + } + } +} + +/** + * CloudEvent Protobuf Batch Format + * + */ + +message CloudEventBatch { + repeated CloudEvent events = 1; +} diff --git a/cloudevents/proto/cloudevents_pb2.py b/cloudevents/proto/cloudevents_pb2.py new file mode 100644 index 00000000..b93eef5b --- /dev/null +++ b/cloudevents/proto/cloudevents_pb2.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: cloudevents.proto +"""Generated protocol buffer code.""" +from google.protobuf.internal import builder as _builder +from google.protobuf import descriptor as _descriptor +from google.protobuf import descriptor_pool as _descriptor_pool +from google.protobuf import symbol_database as _symbol_database +# @@protoc_insertion_point(imports) + +_sym_db = _symbol_database.Default() + + +from google.protobuf import any_pb2 as google_dot_protobuf_dot_any__pb2 +from google.protobuf import timestamp_pb2 as google_dot_protobuf_dot_timestamp__pb2 + + +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x11\x63loudevents.proto\x12\x11io.cloudevents.v1\x1a\x19google/protobuf/any.proto\x1a\x1fgoogle/protobuf/timestamp.proto\"\xb0\x04\n\nCloudEvent\x12\n\n\x02id\x18\x01 \x01(\t\x12\x0e\n\x06source\x18\x02 \x01(\t\x12\x14\n\x0cspec_version\x18\x03 \x01(\t\x12\x0c\n\x04type\x18\x04 \x01(\t\x12\x41\n\nattributes\x18\x05 \x03(\x0b\x32-.io.cloudevents.v1.CloudEvent.AttributesEntry\x12\x15\n\x0b\x62inary_data\x18\x06 \x01(\x0cH\x00\x12\x13\n\ttext_data\x18\x07 \x01(\tH\x00\x12*\n\nproto_data\x18\x08 \x01(\x0b\x32\x14.google.protobuf.AnyH\x00\x1ai\n\x0f\x41ttributesEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\x45\n\x05value\x18\x02 \x01(\x0b\x32\x36.io.cloudevents.v1.CloudEvent.CloudEventAttributeValue:\x02\x38\x01\x1a\xd3\x01\n\x18\x43loudEventAttributeValue\x12\x14\n\nce_boolean\x18\x01 \x01(\x08H\x00\x12\x14\n\nce_integer\x18\x02 \x01(\x05H\x00\x12\x13\n\tce_string\x18\x03 \x01(\tH\x00\x12\x12\n\x08\x63\x65_bytes\x18\x04 \x01(\x0cH\x00\x12\x10\n\x06\x63\x65_uri\x18\x05 \x01(\tH\x00\x12\x14\n\nce_uri_ref\x18\x06 \x01(\tH\x00\x12\x32\n\x0c\x63\x65_timestamp\x18\x07 \x01(\x0b\x32\x1a.google.protobuf.TimestampH\x00\x42\x06\n\x04\x61ttrB\x06\n\x04\x64\x61ta\"@\n\x0f\x43loudEventBatch\x12-\n\x06\x65vents\x18\x01 \x03(\x0b\x32\x1d.io.cloudevents.v1.CloudEventB\x8b\x01\n\x17io.cloudevents.v1.protoP\x01Z\x1a\x63loudevents.io/genproto/v1\xaa\x02\x1a\x43loudNative.CloudEvents.V1\xca\x02\x17Io\\CloudEvents\\V1\\Proto\xea\x02\x1aIo::CloudEvents::V1::Protob\x06proto3') + +_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) +_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'cloudevents_pb2', globals()) +if _descriptor._USE_C_DESCRIPTORS == False: + + DESCRIPTOR._options = None + DESCRIPTOR._serialized_options = b'\n\027io.cloudevents.v1.protoP\001Z\032cloudevents.io/genproto/v1\252\002\032CloudNative.CloudEvents.V1\312\002\027Io\\CloudEvents\\V1\\Proto\352\002\032Io::CloudEvents::V1::Proto' + _CLOUDEVENT_ATTRIBUTESENTRY._options = None + _CLOUDEVENT_ATTRIBUTESENTRY._serialized_options = b'8\001' + _CLOUDEVENT._serialized_start=101 + _CLOUDEVENT._serialized_end=661 + _CLOUDEVENT_ATTRIBUTESENTRY._serialized_start=334 + _CLOUDEVENT_ATTRIBUTESENTRY._serialized_end=439 + _CLOUDEVENT_CLOUDEVENTATTRIBUTEVALUE._serialized_start=442 + _CLOUDEVENT_CLOUDEVENTATTRIBUTEVALUE._serialized_end=653 + _CLOUDEVENTBATCH._serialized_start=663 + _CLOUDEVENTBATCH._serialized_end=727 +# @@protoc_insertion_point(module_scope) diff --git a/cloudevents/proto/cloudevents_pb2.pyi b/cloudevents/proto/cloudevents_pb2.pyi new file mode 100644 index 00000000..f390f342 --- /dev/null +++ b/cloudevents/proto/cloudevents_pb2.pyi @@ -0,0 +1,58 @@ +from google.protobuf import any_pb2 as _any_pb2 +from google.protobuf import timestamp_pb2 as _timestamp_pb2 +from google.protobuf.internal import containers as _containers +from google.protobuf import descriptor as _descriptor +from google.protobuf import message as _message +from typing import ClassVar as _ClassVar, Iterable as _Iterable, Mapping as _Mapping, Optional as _Optional, Union as _Union + +DESCRIPTOR: _descriptor.FileDescriptor + +class CloudEvent(_message.Message): + __slots__ = ["attributes", "binary_data", "id", "proto_data", "source", "spec_version", "text_data", "type"] + class AttributesEntry(_message.Message): + __slots__ = ["key", "value"] + KEY_FIELD_NUMBER: _ClassVar[int] + VALUE_FIELD_NUMBER: _ClassVar[int] + key: str + value: CloudEvent.CloudEventAttributeValue + def __init__(self, key: _Optional[str] = ..., value: _Optional[_Union[CloudEvent.CloudEventAttributeValue, _Mapping]] = ...) -> None: ... + class CloudEventAttributeValue(_message.Message): + __slots__ = ["ce_boolean", "ce_bytes", "ce_integer", "ce_string", "ce_timestamp", "ce_uri", "ce_uri_ref"] + CE_BOOLEAN_FIELD_NUMBER: _ClassVar[int] + CE_BYTES_FIELD_NUMBER: _ClassVar[int] + CE_INTEGER_FIELD_NUMBER: _ClassVar[int] + CE_STRING_FIELD_NUMBER: _ClassVar[int] + CE_TIMESTAMP_FIELD_NUMBER: _ClassVar[int] + CE_URI_FIELD_NUMBER: _ClassVar[int] + CE_URI_REF_FIELD_NUMBER: _ClassVar[int] + ce_boolean: bool + ce_bytes: bytes + ce_integer: int + ce_string: str + ce_timestamp: _timestamp_pb2.Timestamp + ce_uri: str + ce_uri_ref: str + def __init__(self, ce_boolean: bool = ..., ce_integer: _Optional[int] = ..., ce_string: _Optional[str] = ..., ce_bytes: _Optional[bytes] = ..., ce_uri: _Optional[str] = ..., ce_uri_ref: _Optional[str] = ..., ce_timestamp: _Optional[_Union[_timestamp_pb2.Timestamp, _Mapping]] = ...) -> None: ... + ATTRIBUTES_FIELD_NUMBER: _ClassVar[int] + BINARY_DATA_FIELD_NUMBER: _ClassVar[int] + ID_FIELD_NUMBER: _ClassVar[int] + PROTO_DATA_FIELD_NUMBER: _ClassVar[int] + SOURCE_FIELD_NUMBER: _ClassVar[int] + SPEC_VERSION_FIELD_NUMBER: _ClassVar[int] + TEXT_DATA_FIELD_NUMBER: _ClassVar[int] + TYPE_FIELD_NUMBER: _ClassVar[int] + attributes: _containers.MessageMap[str, CloudEvent.CloudEventAttributeValue] + binary_data: bytes + id: str + proto_data: _any_pb2.Any + source: str + spec_version: str + text_data: str + type: str + def __init__(self, id: _Optional[str] = ..., source: _Optional[str] = ..., spec_version: _Optional[str] = ..., type: _Optional[str] = ..., attributes: _Optional[_Mapping[str, CloudEvent.CloudEventAttributeValue]] = ..., binary_data: _Optional[bytes] = ..., text_data: _Optional[str] = ..., proto_data: _Optional[_Union[_any_pb2.Any, _Mapping]] = ...) -> None: ... + +class CloudEventBatch(_message.Message): + __slots__ = ["events"] + EVENTS_FIELD_NUMBER: _ClassVar[int] + events: _containers.RepeatedCompositeFieldContainer[CloudEvent] + def __init__(self, events: _Optional[_Iterable[_Union[CloudEvent, _Mapping]]] = ...) -> None: ... diff --git a/requirements/dev.txt b/requirements/dev.txt index 63872949..2c9d0cae 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -5,3 +5,5 @@ pep8-naming flake8-print tox pre-commit +protobuf +grpcio-tools diff --git a/requirements/test.txt b/requirements/test.txt index 3f6e2d89..99750389 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -13,3 +13,5 @@ requests flask pydantic>=1.0.0<1.9.0; python_version <= '3.6' pydantic>=1.0.0<2.0; python_version > '3.6' +protobuf >= 3.19.0; python_version <= '3.6' +protobuf >= 3.20.0; python_version > '3.6' diff --git a/setup.py b/setup.py index 8a4ca870..5dcc671d 100644 --- a/setup.py +++ b/setup.py @@ -74,5 +74,9 @@ def get_version(rel_path): "pydantic>=1.0.0<1.9.0; python_version <= '3.6'", "pydantic>=1.0.0<2.0; python_version > '3.6'", ], + "protobuf": [ + "protobuf >= 3.19.0; python_version <= '3.6'", + "protobuf >= 3.20.0; python_version > '3.6'", + ] }, )