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

move cloud events to azure core #16661

Closed
wants to merge 21 commits into from
Closed

Conversation

rakshith91
Copy link
Contributor

@rakshith91 rakshith91 commented Feb 10, 2021

Fixes #16585

Adding CloudEvent to azure-core as follows:

from azure.core.messaging import CloudEvent


def _decode(content):
try:
return base64.b64decode(content.encode('utf-8'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ascii is enough, per base64 spec.
  • I'm not even sure we need to encode, could you describe the scenario where we do?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this method is confusing to me. Is the only reason for it to exist to normalize the exception type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - thinking about it, i think we dont need this method at all - removing it

self.type = type
self.specversion = kwargs.pop("specversion", "1.0")
self.id = kwargs.pop("id", str(uuid.uuid4()))
self.time = kwargs.pop("time", dt.datetime.utcnow().isoformat())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow
Warning Because naive datetime objects are treated by many datetime methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing the current time in UTC is by calling datetime.now(timezone.utc).

Problem is, Python 2 doesn't have timezone.utc. @johanste usually I tell people to use msrest.serialization.utc since they use msrest as a dependency anyway, but here it won't work. Though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

source=event.pop("source", None),
type=event.pop("type", None),
specversion=event.pop("specversion", None),
data=event.pop("data", None) or _decode(event.pop("data_base64", None)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect an error if both data and data_base64 are present for clarity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with it, but if we ever decide to add data_base64 in the future, this error would cause a breaking change - thoughts?

import json

from azure.core.messaging import CloudEvent
from devtools_testutils import AzureMgmtTestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad copying ;) - removed it



def test_cloud_custom_dict_with_extensions():
event = CloudEvent.from_dict(cloud_custom_dict_with_extensions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually find it confusing to have the body in another file here. Could we get rid of the _mock.py file and insert the dict here?

@rakshith91 rakshith91 mentioned this pull request Feb 11, 2021
@@ -2,6 +2,9 @@

## 1.11.1 (Unreleased)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.12.0

Copy link
Member

@xiangyan99 xiangyan99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate core changes and event hub changes into different PRs.

:type subject: str
:keyword specversion: Optional. The version of the CloudEvent spec. Defaults to "1.0"
:type specversion: str
:keyword id: Optional. An identifier for the event. The combination of id and source must be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need explicit "Optional." for keyword arguments. Right? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, not sure if it's a good practice - but there can certainly be "required" keyword only params (hopefully not in our libraries, but still possible) - thought no harm in mentioning it's optional :)

:rtype: CloudEvent
"""
return cls(
id=event.pop("id", None),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we already pass **kwargs into cls, why we need id=event.pop("id", None), etc.?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, intentionally popping them since we want to have the remainder of the dict as "extensions"

@@ -0,0 +1,42 @@
# storage cloud event
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give the file a more specific name.

@xiangyan99
Copy link
Member

And you also need to bump the core dependency version to 1.12.0 for event grid. :)

@rakshith91
Copy link
Contributor Author

Please review core related chages here
Add cloud event to core by rakshith91 · Pull Request #16800 · Azure/azure-sdk-for-python (github.com)

@iscai-msft
Copy link
Contributor

/azp run python - autorest - pr

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rakshith91 rakshith91 closed this Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move cloudevent to azure core
5 participants