# Path: cloudevents/http/http_methods.py
# def from_http(
#     headers: typing.Dict[str, str],
#     data: typing.Union[str, bytes, None],
#     data_unmarshaller: types.UnmarshallerType = None,
# ):
#     """
#     Unwrap a CloudEvent (binary or structured) from an HTTP request.
#     :param headers: the HTTP headers
#     :type headers: typing.Dict[str, str]
#     :param data: the HTTP request body. If set to None, "" or b'', the returned
#         event's data field will be set to None
#     :type data: typing.IO
#     :param data_unmarshaller: Callable function to map data to a python object
#         e.g. lambda x: x or lambda x: json.loads(x)
#     :type data_unmarshaller: types.UnmarshallerType
#     """
#     if data is None or data == b"":
#         # Empty string will cause data to be marshalled into None
#         data = ""
# 
#     if not isinstance(data, (str, bytes, bytearray)):
#         raise cloud_exceptions.InvalidStructuredJSON(
#             "Expected json of type (str, bytes, bytearray), "
#             f"but instead found type {type(data)}"
#         )
# 
#     headers = {key.lower(): value for key, value in headers.items()}
#     if data_unmarshaller is None:
#         data_unmarshaller = _json_or_string
# 
#     marshall = marshaller.NewDefaultHTTPMarshaller()
# 
#     if is_binary(headers):
#         specversion = headers.get("ce-specversion", None)
#     else:
#         try:
#             raw_ce = json.loads(data)
#         except json.decoder.JSONDecodeError:
#             raise cloud_exceptions.MissingRequiredFields(
#                 "Failed to read specversion from both headers and data. "
#                 f"The following can not be parsed as json: {data}"
#             )
#         if hasattr(raw_ce, "get"):
#             specversion = raw_ce.get("specversion", None)
#         else:
#             raise cloud_exceptions.MissingRequiredFields(
#                 "Failed to read specversion from both headers and data. "
#                 f"The following deserialized data has no 'get' method: {raw_ce}"
#             )
# 
#     if specversion is None:
#         raise cloud_exceptions.MissingRequiredFields(
#             "Failed to find specversion in HTTP request"
#         )
# 
#     event_handler = _obj_by_version.get(specversion, None)
# 
#     if event_handler is None:
#         raise cloud_exceptions.InvalidRequiredFields(
#             f"Found invalid specversion {specversion}"
#         )
# 
#     event = marshall.FromRequest(
#         event_handler(), headers, data, data_unmarshaller=data_unmarshaller
#     )
#     attrs = event.Properties()
#     attrs.pop("data", None)
#     attrs.pop("extensions", None)
#     attrs.update(**event.extensions)
# 
#     if event.data == "" or event.data == b"":
#         # TODO: Check binary unmarshallers to debug why setting data to ""
#         # returns an event with data set to None, but structured will return ""
#         data = None
#     else:
#         data = event.data
#     return CloudEvent(attrs, data)


import io
from flask import Flask, request
from PIL import Image
from cloudevents.http import from_http
# All Rights Reserved.
#
#    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.



app = Flask(__name__)


@app.route("/", methods=["POST"])
def home():
    # Create a CloudEvent.
    # data_unmarshaller will cast event.data into an io.BytesIO object

# Path: cloudevents/http/http_methods.py
# def from_http(
#     headers: typing.Dict[str, str],
#     data: typing.Union[str, bytes, None],
#     data_unmarshaller: types.UnmarshallerType = None,
# ):
#     """
#     Unwrap a CloudEvent (binary or structured) from an HTTP request.
#     :param headers: the HTTP headers
#     :type headers: typing.Dict[str, str]
#     :param data: the HTTP request body. If set to None, "" or b'', the returned
#         event's data field will be set to None
#     :type data: typing.IO
#     :param data_unmarshaller: Callable function to map data to a python object
#         e.g. lambda x: x or lambda x: json.loads(x)
#     :type data_unmarshaller: types.UnmarshallerType
#     """
#     if data is None or data == b"":
#         # Empty string will cause data to be marshalled into None
#         data = ""
# 
#     if not isinstance(data, (str, bytes, bytearray)):
#         raise cloud_exceptions.InvalidStructuredJSON(
#             "Expected json of type (str, bytes, bytearray), "
#             f"but instead found type {type(data)}"
#         )
# 
#     headers = {key.lower(): value for key, value in headers.items()}
#     if data_unmarshaller is None:
#         data_unmarshaller = _json_or_string
# 
#     marshall = marshaller.NewDefaultHTTPMarshaller()
# 
#     if is_binary(headers):
#         specversion = headers.get("ce-specversion", None)
#     else:
#         try:
#             raw_ce = json.loads(data)
#         except json.decoder.JSONDecodeError:
#             raise cloud_exceptions.MissingRequiredFields(
#                 "Failed to read specversion from both headers and data. "
#                 f"The following can not be parsed as json: {data}"
#             )
#         if hasattr(raw_ce, "get"):
#             specversion = raw_ce.get("specversion", None)
#         else:
#             raise cloud_exceptions.MissingRequiredFields(
#                 "Failed to read specversion from both headers and data. "
#                 f"The following deserialized data has no 'get' method: {raw_ce}"
#             )
# 
#     if specversion is None:
#         raise cloud_exceptions.MissingRequiredFields(
#             "Failed to find specversion in HTTP request"
#         )
# 
#     event_handler = _obj_by_version.get(specversion, None)
# 
#     if event_handler is None:
#         raise cloud_exceptions.InvalidRequiredFields(
#             f"Found invalid specversion {specversion}"
#         )
# 
#     event = marshall.FromRequest(
#         event_handler(), headers, data, data_unmarshaller=data_unmarshaller
#     )
#     attrs = event.Properties()
#     attrs.pop("data", None)
#     attrs.pop("extensions", None)
#     attrs.update(**event.extensions)
# 
#     if event.data == "" or event.data == b"":
#         # TODO: Check binary unmarshallers to debug why setting data to ""
#         # returns an event with data set to None, but structured will return ""
#         data = None
#     else:
#         data = event.data
#     return CloudEvent(attrs, data)


# Path: samples/http-image-cloudevents/image_sample_server.py
import io
from flask import Flask, request
from PIL import Image
from cloudevents.http import from_http

# All Rights Reserved.
#
#    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.



app = Flask(__name__)


@app.route("/", methods=["POST"])
def home():
    # Create a CloudEvent.
    # data_unmarshaller will cast event.data into an io.BytesIO object

    event = from_http(