-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Support millisecond timestamp precision for bedrock-agentcore #9791
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
Open
SamRemis
wants to merge
2
commits into
aws:v2
Choose a base branch
from
SamRemis:port-3568
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,10 +63,32 @@ | |
# Same as ISO8601, but with microsecond precision. | ||
ISO8601_MICRO = '%Y-%m-%dT%H:%M:%S.%fZ' | ||
|
||
TIMESTAMP_PRECISION_DEFAULT = 'default' | ||
TIMESTAMP_PRECISION_MILLISECOND = 'millisecond' | ||
TIMESTAMP_PRECISION_OPTIONS = ( | ||
TIMESTAMP_PRECISION_DEFAULT, | ||
TIMESTAMP_PRECISION_MILLISECOND, | ||
) | ||
|
||
def create_serializer(protocol_name, include_validation=True): | ||
# TODO: Unknown protocols. | ||
serializer = SERIALIZERS[protocol_name]() | ||
def create_serializer( | ||
protocol_name, | ||
include_validation=True, | ||
timestamp_precision=TIMESTAMP_PRECISION_DEFAULT, | ||
): | ||
"""Create a serializer for the given protocol. | ||
:param protocol_name: The protocol name to create a serializer for. | ||
:type protocol_name: str | ||
:param include_validation: Whether to include parameter validation. | ||
:type include_validation: bool | ||
:param timestamp_precision: Timestamp precision level. | ||
- 'default': Microseconds for ISO timestamps, seconds for Unix and RFC | ||
- 'millisecond': Millisecond precision (ISO/Unix), seconds for RFC | ||
:type timestamp_precision: str | ||
:return: A serializer instance for the given protocol. | ||
""" | ||
serializer = SERIALIZERS[protocol_name]( | ||
timestamp_precision=timestamp_precision | ||
) | ||
if include_validation: | ||
validator = validate.ParamValidator() | ||
serializer = validate.ParamValidationDecorator(validator, serializer) | ||
|
@@ -82,6 +104,14 @@ class Serializer: | |
MAP_TYPE = dict | ||
DEFAULT_ENCODING = 'utf-8' | ||
|
||
def __init__(self, timestamp_precision=TIMESTAMP_PRECISION_DEFAULT): | ||
if timestamp_precision not in TIMESTAMP_PRECISION_OPTIONS: | ||
raise ValueError( | ||
f"Invalid timestamp precision found while creating serializer: {timestamp_precision}" | ||
) | ||
|
||
self._timestamp_precision = timestamp_precision | ||
|
||
def serialize_to_request(self, parameters, operation_model): | ||
"""Serialize parameters into an HTTP request. | ||
|
||
|
@@ -136,16 +166,33 @@ def _create_default_request(self): | |
# Some extra utility methods subclasses can use. | ||
|
||
def _timestamp_iso8601(self, value): | ||
if value.microsecond > 0: | ||
timestamp_format = ISO8601_MICRO | ||
"""Return ISO8601 timestamp with precision based on timestamp_precision.""" | ||
# Smithy's standard is milliseconds, so we truncate the timestamp if the millisecond flag is set to true | ||
if self._timestamp_precision == TIMESTAMP_PRECISION_MILLISECOND: | ||
milliseconds = value.microsecond // 1000 | ||
return ( | ||
value.strftime('%Y-%m-%dT%H:%M:%S') + f'.{milliseconds:03d}Z' | ||
) | ||
else: | ||
timestamp_format = ISO8601 | ||
return value.strftime(timestamp_format) | ||
# Otherwise we continue supporting microseconds in iso8601 for legacy reasons | ||
if value.microsecond > 0: | ||
timestamp_format = ISO8601_MICRO | ||
else: | ||
timestamp_format = ISO8601 | ||
return value.strftime(timestamp_format) | ||
|
||
def _timestamp_unixtimestamp(self, value): | ||
return int(calendar.timegm(value.timetuple())) | ||
"""Return unix timestamp with precision based on timestamp_precision.""" | ||
# As of the addition of the precision flag, we support millisecond precision here as well | ||
if self._timestamp_precision == TIMESTAMP_PRECISION_MILLISECOND: | ||
base_timestamp = calendar.timegm(value.timetuple()) | ||
milliseconds = (value.microsecond // 1000) / 1000.0 | ||
return base_timestamp + milliseconds | ||
else: | ||
return int(calendar.timegm(value.timetuple())) | ||
|
||
def _timestamp_rfc822(self, value): | ||
"""Return RFC822 timestamp (always second precision - RFC doesn't support sub-second).""" | ||
if isinstance(value, datetime.datetime): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: missing one-line comment, to align with the botocore PR |
||
value = self._timestamp_unixtimestamp(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this line should be updated to match the botocore PR |
||
return formatdate(value, usegmt=True) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we remove this extra newline to match the formatting in botocore?