示例#1
0
    def request_method(self, operation):
        http = operation['http']

        output_type = ParserBase.get_output_type(operation)
        self.generate_documentation(operation, "\t")

        # This feels so hacky to get around scoping of these in the else block:
        input_name = ''
        input_type = ''

        if not ('input' in operation):
            self.append("\tpub fn " + ParserBase.c_to_s(operation['name']) +
                        "(&mut self"
                        ") -> Result<" + output_type + ", AWSError> {")
        else:
            input_name = operation['input']['shape']
            input_type = self.shape(input_name)
            self.append("\tpub fn " + ParserBase.c_to_s(operation['name']) +
                        "(&mut self, input: &" + input_name + ") -> Result<" +
                        output_type + ", AWSError> {")

        self.append('\t\tlet mut request = SignedRequest::new("' +
                    http['method'] + '", "' + self.metadata('endpointPrefix') +
                    '", &self.region, "' + http['requestUri'] + '");')
        self.append("\t\tlet mut params = Params::new();")
        self.append('\t\tparams.put("Action", "' + operation['name'] + '");')

        if ('input' in operation):
            self.append('\t\t' + input_name +
                        'Writer::write_params(&mut params, \"\", &input);')

        self.append('\t\trequest.set_params(params);')
        self.append(
            '\t\tlet mut result = request.sign_and_execute(try!(self.creds.get_credentials()));'
        )
        self.append('\t\tlet status = result.status.to_u16();')
        #	self.append('\t\tprintln!("{}", output);'
        self.append('\t\tlet mut reader = EventReader::new(result);')
        self.append(
            '\t\tlet mut stack = XmlResponseFromAws::new(reader.events().peekable());'
        )
        self.append('\t\tstack.next(); // xml start tag')
        self.append('\t\tstack.next();')
        self.append('\t\tmatch status {')

        self.append('\t\t\t200 => { ')

        if output_type == '()':
            self.append('\t\t\t\tOk(())')
        else:
            self.append('\t\t\t\tOk(try!(' + output_type +
                        'Parser::parse_xml("' + output_type +
                        '", &mut stack)))')

        self.append('\t\t\t}')
        self.append('\t\t\t_ => { Err(AWSError::new("error")) }')
        self.append('\t\t}')
        self.append("\t}")
示例#2
0
    def request_method(self, operation):
        http = operation['http']

        output_type = ParserBase.get_output_type(operation)
        self.generate_documentation(operation, "\t")

        if not ('input' in operation):
            input_name = ''
            input_type = ''
            self.append("\tpub fn " + ParserBase.c_to_s(operation['name']) +
                        "(&mut self"
                        ") -> Result<" + output_type + "> {")
        else:
            input_name = operation['input']['shape']
            input_type = self.shape(input_name)
            self.append("\tpub fn " + ParserBase.c_to_s(operation['name']) +
                        "(&mut self, input: &" + input_name + ") -> Result<" +
                        output_type + "> {")

        self.append(
            '\t\tlet encoded = to_string(&input).expect("failed to convert input to JSON");'
        )
        self.append('\t\tlet mut request = SignedRequest::new("' +
                    http['method'] + '", "' + self.metadata('endpointPrefix') +
                    '", &self.region, "' + http['requestUri'] + '");')
        self.append(
            '\t\trequest.set_content_type("application/x-amz-json-1.1".to_string());'
        )
        self.append('\t\trequest.add_header("x-amz-target", "' +
                    self.metadata('targetPrefix') + '.' + operation['name'] +
                    '");')
        self.append('\t\trequest.set_payload(Some(encoded.as_bytes()));')
        self.append(
            '\t\tlet mut result = request.sign_and_execute(try!(self.creds.get_credentials()));'
        )
        self.append('\t\tlet status = result.status.to_u16();')
        self.append('\t\tlet mut body = String::new();')
        self.append('\t\tresult.read_to_string(&mut body).unwrap();')

        self.append('\t\tmatch status {')
        self.append('\t\t\t200 => { ')

        if output_type == '()':
            self.append('\t\t\t\tOk(())')
        else:
            self.append(
                '\t\t\t\tlet decoded: ' + output_type +
                ' = from_str(&body).expect("failed to convert JSON into Rust type");'
            )
            self.append('\t\t\t\tOk(decoded)')

        self.append('\t\t\t}')
        self.append('\t\t\t_ => {')
        self.append('\t\t\t\tErr(parse_error(&body))')
        self.append('\t\t\t}')
        self.append('\t\t}')
        self.append("\t}")
示例#3
0
    def request_method(self, operation):
        http = operation['http']

        output_type = ParserBase.get_output_type(operation)
        self.generate_documentation(operation, "\t")

        # This feels so hacky to get around scoping of these in the else block:
        input_name = ''
        input_type = ''

        if not ('input' in operation):
            self.append("\tpub fn " + ParserBase.c_to_s(
                    operation['name']) + "(&mut self"") -> Result<" + output_type + ", AWSError> {")
        else:
            input_name = operation['input']['shape']
            input_type = self.shape(input_name)
            self.append("\tpub fn " + ParserBase.c_to_s(operation[
                                                            'name']) + "(&mut self, input: &" + input_name + ") -> Result<" + output_type + ", AWSError> {")

        self.append(
            '\t\tlet mut request = SignedRequest::new("' + http['method'] + '", "' + self.metadata('endpointPrefix')
            + '", &self.region, "' + http['requestUri'] + '");')
        self.append("\t\tlet mut params = Params::new();")
        self.append('\t\tparams.put("Action", "' + operation['name'] + '");')

        if ('input' in operation):
            self.append('\t\t' + input_name + 'Writer::write_params(&mut params, \"\", &input);')

        self.append('\t\trequest.set_params(params);')
        self.append('\t\tlet mut result = request.sign_and_execute(try!(self.creds.get_credentials()));')
        self.append('\t\tlet status = result.status.to_u16();')
        #	self.append('\t\tprintln!("{}", output);'
        self.append('\t\tlet mut reader = EventReader::new(result);')
        self.append('\t\tlet mut stack = XmlResponseFromAws::new(reader.events().peekable());')
        self.append('\t\tstack.next(); // xml start tag')
        self.append('\t\tstack.next();')
        self.append('\t\tmatch status {')

        self.append('\t\t\t200 => { ')

        if output_type == '()':
            self.append('\t\t\t\tOk(())')
        else:
            self.append('\t\t\t\tOk(try!(' + output_type + 'Parser::parse_xml("' + output_type + '", &mut stack)))')

        self.append('\t\t\t}')
        self.append('\t\t\t_ => { Err(AWSError::new("error")) }')
        self.append('\t\t}')
        self.append("\t}")
示例#4
0
    def request_method(self, operation):
        http = operation['http']

        output_type = ParserBase.get_output_type(operation)
        self.generate_documentation(operation, "\t")

        if not ('input' in operation):
            input_name = ''
            input_type = ''
            self.append(
                "\tpub fn " + ParserBase.c_to_s(operation['name']) + "(&mut self"") -> Result<" + output_type + "> {")
        else:
            input_name = operation['input']['shape']
            input_type = self.shape(input_name)
            self.append("\tpub fn " + ParserBase.c_to_s(
                    operation['name']) + "(&mut self, input: &" + input_name + ") -> Result<" + output_type + "> {")

        self.append('\t\tlet encoded = to_string(&input).expect("failed to convert input to JSON");')
        self.append('\t\tlet mut request = SignedRequest::new("' + http['method'] + '", "' + self.metadata(
            'endpointPrefix') + '", &self.region, "' + http['requestUri'] + '");')
        self.append('\t\trequest.set_content_type("application/x-amz-json-1.1".to_string());')
        self.append('\t\trequest.add_header("x-amz-target", "' + self.metadata('targetPrefix') + '.' + operation[
            'name'] + '");')
        self.append('\t\trequest.set_payload(Some(encoded.as_bytes()));')
        self.append('\t\tlet mut result = request.sign_and_execute(try!(self.creds.get_credentials()));')
        self.append('\t\tlet status = result.status.to_u16();')
        self.append('\t\tlet mut body = String::new();')
        self.append('\t\tresult.read_to_string(&mut body).unwrap();')

        self.append('\t\tmatch status {')
        self.append('\t\t\t200 => { ')

        if output_type == '()':
            self.append('\t\t\t\tOk(())')
        else:
            self.append(
                '\t\t\t\tlet decoded: ' + output_type + ' = from_str(&body).expect("failed to convert JSON into Rust type");')
            self.append('\t\t\t\tOk(decoded)')

        self.append('\t\t\t}')
        self.append('\t\t\t_ => {')
        self.append('\t\t\t\tErr(parse_error(&body))')
        self.append('\t\t\t}')
        self.append('\t\t}')
        self.append("\t}")