def test_cloud_custom_dict_ms_precision_is_gt_six(): time ="2021-02-18T20:18:10.539861122+00:00" date_obj = _convert_to_isoformat(time) assert date_obj.month == 2 assert date_obj.day == 18 assert date_obj.hour == 20 assert date_obj.microsecond == 539861
def test_cloud_custom_dict_ms_precision_is_eq_six_z_not(): time ="2021-02-18T20:18:10.123456Z" date_obj = _convert_to_isoformat(time) assert date_obj.month == 2 assert date_obj.day == 18 assert date_obj.hour == 20 assert date_obj.microsecond == 123456
def from_dict(cls, event): # type: (Dict) -> CloudEvent """ Returns the deserialized CloudEvent object when a dict is provided. :param event: The dict representation of the event which needs to be deserialized. :type event: dict :rtype: CloudEvent """ kwargs = {} # type: Dict[Any, Any] reserved_attr = [ "data", "data_base64", "id", "source", "type", "specversion", "time", "dataschema", "datacontenttype", "subject", ] if "data" in event and "data_base64" in event: raise ValueError( "Invalid input. Only one of data and data_base64 must be present." ) if "data" in event: data = event.get("data") kwargs["data"] = data if data is not None else NULL elif "data_base64" in event: kwargs["data"] = b64decode( cast(Union[str, bytes], event.get("data_base64")) ) for item in ["datacontenttype", "dataschema", "subject"]: if item in event: val = event.get(item) kwargs[item] = val if val is not None else NULL extensions = {k: v for k, v in event.items() if k not in reserved_attr} if extensions: kwargs["extensions"] = extensions return cls( id=event.get("id"), source=event["source"], type=event["type"], specversion=event.get("specversion"), time=_convert_to_isoformat(event.get("time")), **kwargs )
def from_dict(cls, event): # type: (Dict) -> CloudEvent """ Returns the deserialized CloudEvent object when a dict is provided. :param event: The dict representation of the event which needs to be deserialized. :type event: dict :rtype: CloudEvent """ kwargs = {} # type: Dict[Any, Any] reserved_attr = [ "data", "data_base64", "id", "source", "type", "specversion", "time", "dataschema", "datacontenttype", "subject", ] if "data" in event and "data_base64" in event: raise ValueError( "Invalid input. Only one of data and data_base64 must be present." ) if "data" in event: data = event.get("data") kwargs["data"] = data if data is not None else NULL elif "data_base64" in event: kwargs["data"] = b64decode( cast(Union[str, bytes], event.get("data_base64")) ) for item in ["datacontenttype", "dataschema", "subject"]: if item in event: val = event.get(item) kwargs[item] = val if val is not None else NULL extensions = {k: v for k, v in event.items() if k not in reserved_attr} if extensions: kwargs["extensions"] = extensions try: event_obj = cls( id=event.get("id"), source=event["source"], type=event["type"], specversion=event.get("specversion"), time=_convert_to_isoformat(event.get("time")), **kwargs ) except KeyError: # https://github.com/cloudevents/spec Cloud event spec requires source, type, # specversion. We autopopulate everything other than source, type. if not all([_ in event for _ in ("source", "type")]): if all([_ in event for _ in ("subject", "eventType", "data", "dataVersion", "id", "eventTime")]): raise ValueError( "The event you are trying to parse follows the Eventgrid Schema. You can parse" + " EventGrid events using EventGridEvent.from_dict method in the azure-eventgrid library." ) raise ValueError( "The event does not conform to the cloud event spec https://github.com/cloudevents/spec." + " The `source` and `type` params are required." ) return event_obj