def ParseMessageBody(message_body): """Parse "--message-body" flag as an argparse type. The flag is given as a string: --message-body 'some data' Args: message_body: str, the value of the --message-body flag. Returns: AdditionalProperty, a cloudscheduler additional property object with 'data' as a key, and a JSON object (with a base64-encoded string value) as the value. """ pubsub_messages = _GetPubsubMessages() scheduler_messages = _GetSchedulerMessages() # First, put into a PubsubMessage to make sure we've got the general format # right. pubsub_message = pubsub_messages.PubsubMessage( data=http_encoding.Encode(message_body)) pubsub_message_type = scheduler_messages.PubsubTarget.PubsubMessageValue encoded_data = base64.urlsafe_b64encode(pubsub_message.data) # Apitools will convert these messages to JSON values before issuing the # request. Since extra_types is used here, apitools does not handle converting # string_value to unicode before converting to JSON using json.dumps. That # means we have to convert to unicode here before the request goes into # apitools. Since the data is base64 encoded (which is all ascii), encoding it # here will not change it's value. encoded_data = http_encoding.Decode(encoded_data) return pubsub_message_type.AdditionalProperty( key='data', value=extra_types.JsonValue(string_value=encoded_data))
def ParseMessageBody(message_body): """Parse "--message-body" flag as an argparse type. The flag is given as a string: --message-body 'some data' Args: message_body: str, the value of the --message-body flag. Returns: AdditionalProperty, a cloudscheduler additional property object with 'data' as a key, and a JSON object (with a base64-encoded string value) as the value. """ pubsub_messages = _GetPubsubMessages() # First, put into a PubsubMessage to make sure we've got the general format # right. pubsub_message = pubsub_messages.PubsubMessage( data=http_encoding.Encode(message_body)) encoded_data = base64.urlsafe_b64encode(pubsub_message.data) encoded_data = http_encoding.Decode(encoded_data) return encoded_data
def ReadNoProxy(uri): """Opens a URI with metadata headers, without a proxy, and reads all data..""" request = urllib.request.Request(uri, headers=GOOGLE_GCE_METADATA_HEADERS) # pytype: disable=wrong-arg-types timeout_property = ( properties.VALUES.compute.gce_metadata_read_timeout_sec.GetInt()) result = urllib.request.build_opener(urllib.request.ProxyHandler({})).open( request, timeout=timeout_property).read() return http_encoding.Decode(result)
def ReadNoProxy(uri, timeout): """Opens a URI with metadata headers, without a proxy, and reads all data..""" request = urllib.request.Request(uri, headers=GOOGLE_GCE_METADATA_HEADERS) result = urllib.request.build_opener(urllib.request.ProxyHandler({})).open( request, timeout=timeout).read() return http_encoding.Decode(result)
def _Decode(value): return (http_encoding.Decode(value) if isinstance(value, six.binary_type) else value)
def Default(value): if isinstance(value, datetime.datetime): return {'_datetime': value.strftime(_BOTOCORE_DATE_FORMAT)} if isinstance(value, botocore.response.StreamingBody): return {'_streamingbody': http_encoding.Decode(value.read())} return json.JSONEncoder.default(json.JSONEncoder, value)