当前位置: 首页>>代码示例>>Python>>正文


Python RestApiClient.get_headers方法代码示例

本文整理汇总了Python中RestApiClient.RestApiClient.get_headers方法的典型用法代码示例。如果您正苦于以下问题:Python RestApiClient.get_headers方法的具体用法?Python RestApiClient.get_headers怎么用?Python RestApiClient.get_headers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RestApiClient.RestApiClient的用法示例。


在下文中一共展示了RestApiClient.get_headers方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from RestApiClient import RestApiClient [as 别名]
# 或者: from RestApiClient.RestApiClient import get_headers [as 别名]
def main():
    
    # Create our client and set up some sample data.
    client = RestApiClient()
    setup_data(client)
    
    # Some endpoints accept body parameters. An example of this is the
    # /referencedata/sets/bulkLoad endpoint.
    # Body parameters may appear with path parameters, as in this case, but will
    # never appear with query parameters.
    
    # You must make sure that you set the content type correctly to a type
    # accepted by the endpoint.
    headers = client.get_headers().copy()
    headers['Content-type'] = 'application/json'
    
    body = b'["abc", "def", "123"]'
    
    # Send the request.
    SampleUtilities.pretty_print_request(client, 'referencedata/sets/bulkLoad/rest_api_samples_testset', 'POST', headers=headers)
    response = client.call_api('referencedata/sets/bulkLoad/rest_api_samples_testset', 'POST', headers=headers, data=body)
    SampleUtilities.pretty_print_response(response)
    
    # The response from the previous command only shows information about the
    # set, not the contents of the set. We can view the contents of the set with
    # this command:
    response = client.call_api('referencedata/sets/rest_api_samples_testset', 'GET')
    SampleUtilities.pretty_print_response(response)
开发者ID:MatticusCaesar,项目名称:api-samples,代码行数:30,代码来源:04_BodyParameters.py

示例2: main

# 需要导入模块: from RestApiClient import RestApiClient [as 别名]
# 或者: from RestApiClient.RestApiClient import get_headers [as 别名]
def main():
	# First we have to create our client
	client = RestApiClient()


	#----------------------------------------------------------------------------#
	#Basic 'GET'
	# In this example we'll be using the GET endpoint of siem/offenses without
	# any parameters. This will print absolutely everything it can find, every
	# parameter of every offense.

	# Send in the request
	SampleUtilities.pretty_print_request(client, 'siem/offenses', 'GET')
	response = client.call_api('siem/offenses', 'GET')

	# Check if the success code was returned to ensure the call to the API was
	# successful.
	if (response.code != 200):
		print('Failed to retrieve the list of offenses')
		SampleUtilities.pretty_print_response(response)
		sys.exit(1)

    # Since the previous call had no parameters and response has a lot of text,
	# we'll just print out the number of offenses 
	response_body = json.loads(response.read().decode('utf-8'))
	print('Number of offenses retrived: ' + str(len(response_body)))
	

	#----------------------------------------------------------------------------#
	#Using the fields parameter with 'GET'	
	# If you just print out the result of a call to the siem/offenses GET endpoint
	# there will be a lot of fields displayed which you have no interest in.
	# Here, the fields parameter will make sure the only the fields you want
	# are displayed for each offense.

	# Setting a variable for all the fields that are to be displayed
	fields = '''id,status,description,offense_type,offense_source,magnitude,\
source_network,destination_networks,assigned_to'''

	# Send in the request
	SampleUtilities.pretty_print_request(client, 'siem/offenses?fields=' + fields, 'GET')
	response = client.call_api('siem/offenses?fields=' + fields, 'GET')

	# Once again, check the response code
	if (response.code != 200):
		print('Failed to retrieve list of offenses')
		SampleUtilities.pretty_print_response(response)
		sys.exit(1)

	# This time we will print out the data itself
	SampleUtilities.pretty_print_response(response)


	#----------------------------------------------------------------------------#
	#Using the filter parameter with 'GET'
	# Sometimes you'll want to narrow down your search to just a few offenses.
	# You can use the filter parameter to carefully select what is returned
	# after the call by the value of the fields.
	# Here we're only looking for OPEN offenses, as shown by the value of 'status'
	# being 'OPEN' 
	
	# Send in the request
	SampleUtilities.pretty_print_request(client, 'siem/offenses?fields=' + fields + 
			'&filter=status=OPEN', 'GET')
	response = client.call_api('siem/offenses?fields=' + fields + '&filter=status=OPEN', 'GET')

	# Always check the response code
	if (response.code != 200):
		print('Failed to retrieve list of offenses')
		SampleUtilities.pretty_print_response(response)
		sys.exit(1)

	# And output the data
	SampleUtilities.pretty_print_response(response)


	#----------------------------------------------------------------------------#
	#Paging the 'GET' data using 'Range'
	# If you have a lot of offenses, then you may want to browse through them
	# just a few at a time. In that case, you can use the Range header to 
	# limit the number of offenses shown in a single call. 

	# In this example only OPEN offenses will be used.

	# Call the endpoint so that we can find how many OPEN offenses there are.
	response = client.call_api('siem/offenses?filter=status=OPEN', 'GET')
	num_of_open_offenses = len(json.loads(response.read().decode('utf-8')))

	# Copy the headers into our own variable
	range_header = client.get_headers().copy()

	# Set the starting point (indexing starts at 0)
	page_position = 0
	# and choose how many offenses you want to display at a time.
	offenses_per_page = 5

	# Looping here in order to repeatedly show 5 offenses at a time until we've
	# seen all of the OPEN offenses
	while True:

#.........这里部分代码省略.........
开发者ID:RuanMuller,项目名称:api-samples,代码行数:103,代码来源:01_GetOffenses.py

示例3: make_request

# 需要导入模块: from RestApiClient import RestApiClient [as 别名]
# 或者: from RestApiClient.RestApiClient import get_headers [as 别名]
def make_request(args):

    # Create an API for the version specified by the user. If args.version is
    # None the latest version will be used.
    api_client = RestApiClient(version=args.version)

    # Make a copy of the headers so we are able to set some custom headers.
    headers = api_client.get_headers()

    # Gets endpoint from --api ENDPOINT argument
    endpoint = args.api

    # Strips endpoint of first forward slash, if it has one. Allows user to
    # supply or omit forward slash from beginning of endpoint.
    if str.startswith(endpoint, '/'):
        endpoint = endpoint[1:]

    # Changes 'Accept' header to --response_format RESPONSE_FORMAT argument.
    headers['Accept'] = args.response_format

    # This code snippet adds any extra headers you wish to send with your api
    # call. Must be in name1=value1+name2=value2 form.
    if args.add_headers:
        try:
            header_pairs = args.add_headers.split("+")
            for header_pair in header_pairs:
                header_pair = header_pair.split("=", 1)
                headers[header_pair[0]] = header_pair[1]
        except IndexError as ex:
            raise ParseError("Error: Parsing headers failed. Make sure " +
                             "headers are in format \"<name1>=<value1>+" +
                             "<name2>=<value2>\"", ex)

    if args.range:
        headers['Range'] = 'items='+args.range

    # This adds any query/body params to the list of query/body params.

    params = parse_params(args.params)

    # Checks content_type to see if it should send params as body param, or
    # query param.
    content_type = None

    # Gets Content-type from --request_format REQUEST_FORMAT argument.
    if args.request_format:
        headers['Content-type'] = args.request_format
        content_type = args.request_format

    try:
        # If content_type is application/json, then it is sending a JSON object
        # as a body parameter.
        if content_type == 'application/json':
            data = params['data'].encode('utf-8')
            return api_client.call_api(endpoint, 'POST', data=data,
                                       headers=headers)
        # Else it sends all params as query parameters.
        else:
            for key, value in params.items():
                params[key] = urlparse.quote(value)
            return api_client.call_api(endpoint, args.method, params=params,
                                       headers=headers)

    except IndexError:
        raise ParseError('Error: Parameter parsing failed. Make sure any ' +
                         'parameters follow the syntax ' +
                         '<paramname>="<paramvalue>"')
开发者ID:amico1234,项目名称:api-samples,代码行数:69,代码来源:apiclient.py


注:本文中的RestApiClient.RestApiClient.get_headers方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。