Пример #1
0
def generate_method_output(operation, method, output_preference):
    """
  Helper method for generating code for handling the output/response of
  an API call. If the operation does not return an output (i.e. no output
  type is defined, this method does nothing. Otherwise it generates the
  necessary code to obtain the output data and deserialize them by invoking
  the appropriate object deserializer. It will also generate the required
  deserializer functions as it goes along.

  Args:
    operation Operation to which code is being generated for
    method    Python method that's being code generated
    output_preference The preferred output media type (Useful if the operation
                      has multiple output media types)
  """
    output_type = operation.output.type
    if output_type is None:
        return

    content_types = operation.output.contentType
    if isinstance(output_type.type, TypeDef):
        temp_name = resource.name + "_" + operation.name + "_OutputType"
        data_type = create_named_type(temp_name, output_type.type)
        generate_data_types(data_type)
        method.return_type = "An instance of the " + data_type.name + " class"
    else:
        data_type = api.get_type_by_name(output_type.type.get_reference_name())
        if isinstance(output_type.type, ContainerTypeRef):
            method.return_type = (
                "A " + output_type.type.container + " of " + output_type.type.type.get_reference_name() + " objects"
            )
        elif data_type.name == "_API_":
            method.return_type = "An instance of the API class"
        else:
            method.return_type = "An instance of the " + data_type.name + " class"
    generate_deserializers(api, data_type, content_types)
    media_type = select_media_type(content_types, output_preference)
    function_name = get_function_name("deserialize", data_type, media_type, api)
    deserializer_name = function_name + "(payload)"
    method.add_line("return " + deserializer_name)
Пример #2
0
def generate_method_output(operation, method, output_preference):
    """
  Helper method for generating code for handling the output/response of
  an API call. If the operation does not return an output (i.e. no output
  type is defined, this method does nothing. Otherwise it generates the
  necessary code to obtain the output data and deserialize them by invoking
  the appropriate object deserializer. It will also generate the required
  deserializer functions as it goes along.

  Args:
    operation Operation to which code is being generated for
    method    Python method that's being code generated
    output_preference The preferred output media type (Useful if the operation
                      has multiple output media types)
  """
    output_type = operation.output.type
    if output_type is None:
        return

    content_types = operation.output.contentType
    if isinstance(output_type.type, TypeDef):
        temp_name = resource.name + '_' + operation.name + '_OutputType'
        data_type = create_named_type(temp_name, output_type.type)
        generate_data_types(data_type)
        method.return_type = 'An instance of the ' + data_type.name + ' class'
    else:
        data_type = api.get_type_by_name(output_type.type.get_reference_name())
        if isinstance(output_type.type, ContainerTypeRef):
            method.return_type = 'A ' + output_type.type.container + ' of ' +\
                                 output_type.type.type.get_reference_name() + ' objects'
        elif data_type.name == '_API_':
            method.return_type = 'An instance of the API class'
        else:
            method.return_type = 'An instance of the ' + data_type.name + ' class'
    generate_deserializers(api, data_type, content_types)
    media_type = select_media_type(content_types, output_preference)
    function_name = get_function_name('deserialize', data_type, media_type,
                                      api)
    deserializer_name = function_name + '(payload)'
    method.add_line('return ' + deserializer_name)
Пример #3
0
def generate_rest_invocation(operation, method, request_path):
    """
  Helper method to generate Python code for making a RESTful invocation of
  a given API operation.

  Args:
    operation The API operation being invoked
    method  Python Method that's being code generated. All the code generated
            by this method will be added to this Method object.
    request_path  Request URL path for the HTTP call
  """

    # Start by obtaining a connection
    method.add_line("conn = self.get_connection()")

    if operation.method == "POST" or operation.method == "PUT":
        # If the request is an entity enclosing request, we need to deal with
        # parameter/payload serialization.
        content_type = operation.input.contentType[0]
        input_type = operation.input.type.type
        if isinstance(input_type, TypeDef):
            param_name = method.get_mapping("request")
        elif isinstance(input_type, ContainerTypeRef):
            arg_name = input_type.type.get_reference_name() + "_" + input_type.container
            param_name = method.get_mapping(arg_name)
        else:
            data_type = api.get_type_by_name(input_type.get_reference_name())
            param_name = method.get_mapping(data_type.name)

        # Generate serializer methods for the selected input content type
        generate_serializers(api, input_type, operation.input.contentType)
        media_type = find_media_type(content_type)

        # Generate the code to call the serializer
        function_name = get_function_name("serialize", input_type, media_type, api)
        payload = "payload = serialize_final_" + media_type + "(" + function_name + "(" + param_name + "))"

        # Generate code to set the HTTP Content-type header
        headers = "headers = { 'Content-type' : '" + content_type + "' }"
        method.add_line(headers)
        method.add_line(payload)

        # Make the HTTP POST/PUT call
        method.add_line(
            "conn.request('" + operation.method + "', self.get_path() + " + request_path + " + query, payload, headers)"
        )
    else:
        # Not an entity enclosing request - Simply make the URL request
        method.add_line("conn.request('" + operation.method + "', self.get_path() + " + request_path + " + query)")

    # Generate code to obtain the response
    method.add_line("response = conn.getresponse()")

    output = operation.output
    status = output.status
    errors = {}

    # Generate error handling code
    if operation.errors:
        errors = operation.errors
    method.add_line("expected_status = " + str(status))
    method.add_line("errors = " + str(errors))
    method.add_line("payload = self.get_output(response, expected_status, errors)")
    method.add_line("conn.close()")
    method.add_line("if self.debug:")
    method.indent()
    method.add_line("print 'payload:', payload")
    method.dedent()
Пример #4
0
def generate_rest_invocation(operation, method, request_path):
    """
  Helper method to generate Python code for making a RESTful invocation of
  a given API operation.

  Args:
    operation The API operation being invoked
    method  Python Method that's being code generated. All the code generated
            by this method will be added to this Method object.
    request_path  Request URL path for the HTTP call
  """

    # Start by obtaining a connection
    method.add_line('conn = self.get_connection()')

    if operation.method == 'POST' or operation.method == 'PUT':
        # If the request is an entity enclosing request, we need to deal with
        # parameter/payload serialization.
        content_type = operation.input.contentType[0]
        input_type = operation.input.type.type
        if isinstance(input_type, TypeDef):
            param_name = method.get_mapping('request')
        elif isinstance(input_type, ContainerTypeRef):
            arg_name = input_type.type.get_reference_name(
            ) + '_' + input_type.container
            param_name = method.get_mapping(arg_name)
        else:
            data_type = api.get_type_by_name(input_type.get_reference_name())
            param_name = method.get_mapping(data_type.name)

        # Generate serializer methods for the selected input content type
        generate_serializers(api, input_type, operation.input.contentType)
        media_type = find_media_type(content_type)

        # Generate the code to call the serializer
        function_name = get_function_name('serialize', input_type, media_type,
                                          api)
        payload = 'payload = serialize_final_' + media_type + '(' + \
                  function_name + '(' + param_name + '))'

        # Generate code to set the HTTP Content-type header
        headers = 'headers = { \'Content-type\' : \'' + content_type + '\' }'
        method.add_line(headers)
        method.add_line(payload)

        # Make the HTTP POST/PUT call
        method.add_line('conn.request(\'' + operation.method +
                        '\', self.get_path() + ' + request_path +
                        ' + query, payload, headers)')
    else:
        # Not an entity enclosing request - Simply make the URL request
        method.add_line('conn.request(\'' + operation.method +
                        '\', self.get_path() + ' + request_path + ' + query)')

    # Generate code to obtain the response
    method.add_line('response = conn.getresponse()')

    output = operation.output
    status = output.status
    errors = {}

    # Generate error handling code
    if operation.errors:
        errors = operation.errors
    method.add_line('expected_status = ' + str(status))
    method.add_line('errors = ' + str(errors))
    method.add_line(
        'payload = self.get_output(response, expected_status, errors)')
    method.add_line('conn.close()')
    method.add_line('if self.debug:')
    method.indent()
    method.add_line('print \'payload:\', payload')
    method.dedent()