def GetHeaders(document_client, default_headers, verb, path, resource_id, resource_type, options, partition_key_range_id=None): """Gets HTTP request headers. :param document_client.DocumentClient document_client: :param dict default_headers: :param str verb: :param str path: :param str resource_id: :param str resource_type: :param dict options: :param str partition_key_range_id: :return: The HTTP request headers. :rtype: dict """ headers = dict(default_headers) options = options or {} if options.get('continuation'): headers[http_constants.HttpHeaders.Continuation] = ( options['continuation']) pre_trigger_include = options.get('preTriggerInclude') if pre_trigger_include: headers[http_constants.HttpHeaders.PreTriggerInclude] = ( pre_trigger_include if isinstance(pre_trigger_include, str) else (',').join(pre_trigger_include)) post_trigger_include = options.get('postTriggerInclude') if post_trigger_include: headers[http_constants.HttpHeaders.PostTriggerInclude] = ( post_trigger_include if isinstance(post_trigger_include, str) else (',').join(post_trigger_include)) if options.get('maxItemCount'): headers[http_constants.HttpHeaders.PageSize] = options['maxItemCount'] access_condition = options.get('accessCondition') if access_condition: if access_condition['type'] == 'IfMatch': headers[http_constants.HttpHeaders. IfMatch] = access_condition['condition'] else: headers[http_constants.HttpHeaders. IfNoneMatch] = access_condition['condition'] if options.get('indexingDirective'): headers[http_constants.HttpHeaders.IndexingDirective] = ( options['indexingDirective']) consistency_level = None ''' get default client consistency level''' default_client_consistency_level = headers.get( http_constants.HttpHeaders.ConsistencyLevel) ''' set consistency level. check if set via options, this will override the default ''' if options.get('consistencyLevel'): consistency_level = options['consistencyLevel'] headers[ http_constants.HttpHeaders.ConsistencyLevel] = consistency_level elif default_client_consistency_level is not None: consistency_level = default_client_consistency_level headers[ http_constants.HttpHeaders.ConsistencyLevel] = consistency_level # figure out if consistency level for this request is session is_session_consistency = ( consistency_level == documents.ConsistencyLevel.Session) # set session token if required if is_session_consistency is True: # if there is a token set via option, then use it to override default if options.get('sessionToken'): headers[http_constants.HttpHeaders. SessionToken] = options['sessionToken'] else: # check if the client's default consistency is session (and request consistency level is same), # then update from session container if default_client_consistency_level == documents.ConsistencyLevel.Session: # populate session token from the client's session container headers[http_constants.HttpHeaders.SessionToken] = ( document_client.session.get_session_token(path)) if options.get('enableScanInQuery'): headers[http_constants.HttpHeaders.EnableScanInQuery] = ( options['enableScanInQuery']) if options.get('resourceTokenExpirySeconds'): headers[http_constants.HttpHeaders.ResourceTokenExpiry] = ( options['resourceTokenExpirySeconds']) if options.get('offerType'): headers[http_constants.HttpHeaders.OfferType] = options['offerType'] if options.get('offerThroughput'): headers[http_constants.HttpHeaders. OfferThroughput] = options['offerThroughput'] if 'partitionKey' in options: # if partitionKey value is Undefined, serialize it as {} to be consistent with other SDKs if options.get('partitionKey') is documents.Undefined: headers[http_constants.HttpHeaders.PartitionKey] = [{}] # else serialize using json dumps method which apart from regular values will serialize None into null else: headers[http_constants.HttpHeaders.PartitionKey] = json.dumps( [options['partitionKey']]) if options.get('enableCrossPartitionQuery'): headers[ http_constants.HttpHeaders. EnableCrossPartitionQuery] = options['enableCrossPartitionQuery'] if options.get('populateQueryMetrics'): headers[http_constants.HttpHeaders. PopulateQueryMetrics] = options['populateQueryMetrics'] if document_client.master_key: headers[http_constants.HttpHeaders.XDate] = ( datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')) if document_client.master_key or document_client.resource_tokens: authorization = auth.GetAuthorizationHeader(document_client, verb, path, resource_id, resource_type, headers) # urllib.quote throws when the input parameter is None if authorization: # -_.!~*'() are valid characters in url, and shouldn't be quoted. authorization = urllib_quote(authorization, '-_.!~*\'()') headers[http_constants.HttpHeaders.Authorization] = authorization if verb == 'post' or verb == 'put': if not headers.get(http_constants.HttpHeaders.ContentType): headers[http_constants.HttpHeaders. ContentType] = runtime_constants.MediaTypes.Json if not headers.get(http_constants.HttpHeaders.Accept): headers[http_constants.HttpHeaders. Accept] = runtime_constants.MediaTypes.Json if partition_key_range_id is not None: headers[http_constants.HttpHeaders. PartitionKeyRangeID] = partition_key_range_id if options.get('enableScriptLogging'): headers[http_constants.HttpHeaders. EnableScriptLogging] = options['enableScriptLogging'] if options.get('offerEnableRUPerMinuteThroughput'): headers[http_constants.HttpHeaders. OfferIsRUPerMinuteThroughputEnabled] = options[ 'offerEnableRUPerMinuteThroughput'] if options.get('disableRUPerMinuteUsage'): headers[http_constants.HttpHeaders. DisableRUPerMinuteUsage] = options['disableRUPerMinuteUsage'] return headers
def GetHeaders(document_client, default_headers, verb, path, resource_id, resource_type, options): """Gets HTTP request headers. :Parameters: - `document_client`: document_client.DocumentClient - `default_headers`: dict - `verb`: str - `path`: str - `resource_id`: str - `resource_type`: str - `options`: dict :Returns: dict, the HTTP request headers. """ headers = dict(default_headers) options = options or {} if options.get('continuation'): headers[http_constants.HttpHeaders.Continuation] = ( options['continuation']) pre_trigger_include = options.get('preTriggerInclude') if pre_trigger_include: headers[http_constants.HttpHeaders.PreTriggerInclude] = ( pre_trigger_include if isinstance(pre_trigger_include, str) else (',').join(pre_trigger_include)) post_trigger_include = options.get('postTriggerInclude') if post_trigger_include: headers[http_constants.HttpHeaders.PostTriggerInclude] = ( post_trigger_include if isinstance(post_trigger_include, str) else (',').join(post_trigger_include)) if options.get('maxItemCount'): headers[http_constants.HttpHeaders.PageSize] = options['maxItemCount'] access_condition = options.get('accessCondition') if access_condition: if access_condition['type'] == 'IfMatch': headers[http_constants.HttpHeaders. IfMatch] = access_condition['condition'] else: headers[http_constants.HttpHeaders. IfNoneMatch] = access_condition['condition'] if options.get('indexingDirective'): headers[http_constants.HttpHeaders.IndexingDirective] = ( options['indexingDirective']) # TODO: add consistency level validation. if options.get('consistencyLevel'): headers[http_constants.HttpHeaders.ConsistencyLevel] = ( options['consistencyLevel']) # TODO: add session token automatic handling in case of session consistency. if options.get('sessionToken'): headers[http_constants.HttpHeaders.SessionToken] = ( options['sessionToken']) if options.get('enableScanInQuery'): headers[http_constants.HttpHeaders.EnableScanInQuery] = ( options['enableScanInQuery']) if options.get('resourceTokenExpirySeconds'): headers[http_constants.HttpHeaders.ResourceTokenExpiry] = ( options['resourceTokenExpirySeconds']) if options.get('offerType'): headers[http_constants.HttpHeaders.OfferType] = options['offerType'] if document_client.master_key: headers[http_constants.HttpHeaders.XDate] = ( datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')) if document_client.master_key or document_client.resource_tokens: # -_.!~*'() are valid characters in url, and shouldn't be quoted. headers[http_constants.HttpHeaders.Authorization] = urllib.quote( auth.GetAuthorizationHeader(document_client, verb, path, resource_id, resource_type, headers), '-_.!~*\'()') if verb == 'post' or verb == 'put': if not headers.get(http_constants.HttpHeaders.ContentType): headers[http_constants.HttpHeaders. ContentType] = runtime_constants.MediaTypes.Json if not headers.get(http_constants.HttpHeaders.Accept): headers[http_constants.HttpHeaders. Accept] = runtime_constants.MediaTypes.Json return headers