Ejemplo n.º 1
0
    def rest(self, line, cell=''):
        """Run given HTTP query."""
        args = self.get_args(
            magic_arguments.parse_argstring(self.rest, line)
        )
        try:
            rest_request = parse_rest_request('\n'.join((
                ' '.join(args.query),
                expand_variables(cell, self.get_user_namespace()).rstrip('\n')
            )))
        except ParseError as ex:
            display_usage_example(magic='rest', error_text=str(ex),
                                  is_cell_magic=(cell != ''))
            return None

        sender = self.sender or RequestSender()
        root = self.root or RESTRequest()

        try:
            response = sender.send(
                RESTRequest('GET', 'https://') + root + rest_request,
                proxy=args.proxy,
                verify=not args.insecure,
                max_redirects=args.max_redirects,
                timeout=args.timeout,
            )
        except SSLError:
            self.showtraceback('Use `%rest --insecure` option to disable '
                               'SSL certificate verification.')
            return None
        except Exception:
            self.showtraceback('Request was not completed.')
            return None

        if args.verbose and not args.quiet:
            print(sender.dump())
        elif not args.quiet:
            try:
                if args.parser_expression:
                    display_dict(
                        ResponseParser(response=response,
                                       expression=remove_argument_quotes(args.parser_expression),
                                       content_subtype=args.parser).parse()
                    )
                else:
                    display_response(response)
            except UnknownSubtype:
                self.showtraceback("Use `%rest --parser` to specify which parser to use.")
            except Exception:
                self.showtraceback("Can't display the response.")
        return response
Ejemplo n.º 2
0
 def rest_root(self, line, cell=''):
     """Set default HTTP query values, to be used by all subsequent queries.
     """
     args = self.get_args(
         magic_arguments.parse_argstring(self.rest_root, line)
     )
     if line or cell:
         try:
             self.root = parse_rest_request('\n'.join((
                 ' '.join(args.query),
                 expand_variables(cell, self.get_user_namespace())
             )))
         except ParseError as ex:
             display_usage_example(magic='rest_root', error_text=str(ex),
                                   is_cell_magic=(cell != ''))
             return
         else:
             print('Requests defaults are set.')
             self.root_args = args
     else:
         self.root = None
         self.root_args = argparse.Namespace()
         print('Requests defaults are canceled.')
Ejemplo n.º 3
0
def test_request_body_parsed(value, expected):
    assert parse_rest_request(value).body == expected.body
Ejemplo n.º 4
0
def test_request_headers_parsed(value, expected):
    assert parse_rest_request(value).headers == expected.headers
Ejemplo n.º 5
0
def test_request_url_parsed(value, expected):
    assert parse_rest_request(value).url == expected.url
Ejemplo n.º 6
0
def test_request_method_parsed(value, expected):
    assert parse_rest_request(value).method == expected.method
Ejemplo n.º 7
0
def test_exception_on_inalid_request(value):
    with pytest.raises(ParseError):
        parse_rest_request(value)