def request(ctx, operation, params, path, format, encoding, verbose): options = {"schema": {"path": path, "format": format, "encoding": encoding}} config = _load_config(options, verbose=verbose) path = config["schema"]["path"] format = config["schema"]["format"] encoding = config["schema"]["encoding"] with open(path, "rb") as schema_file: schema = schema_file.read() params = [param.partition("=") for param in params] params = dict([(key, value) for key, sep, value in params]) session = ctx.obj if verbose: session = DebugSession(session) try: client = Client(schema, format=format, encoding=encoding, session=session) except (typesystem.ParseError, typesystem.ValidationError) as exc: if isinstance(exc, typesystem.ParseError): summary = { "json": "Invalid JSON.", "yaml": "Invalid YAML.", None: "Parse error.", }[encoding] else: summary = { "config": "Invalid APIStar config.", "jsonschema": "Invalid JSONSchema document.", "openapi": "Invalid OpenAPI schema.", "swagger": "Invalid Swagger schema.", None: "Invalid schema.", }[format] _echo_error(exc, schema, summary=summary, verbose=verbose) sys.exit(1) try: result = client.request(operation, **params) except ClientError as exc: for message in exc.messages: if message.code == "invalid_property": text = '* Invalid parameter "%s".' % message.index[0] elif message.code == "required": text = '* Missing required parameter "%s".' % message.index[0] else: text = "* %s" % message.text click.echo(text) click.echo(click.style("✘ ", fg="red") + "Client error") sys.exit(1) except ErrorResponse as exc: click.echo(json.dumps(exc.content, indent=4)) click.echo(click.style("✘ ", fg="red") + exc.title) sys.exit(1) click.echo(json.dumps(result, indent=4))
def test_unique_filename(tmpdir): client = Client(schema, session=TestClient(app), decoders=[decoders.DownloadDecoder(tmpdir)]) data = client.request('file-response') assert os.path.basename(data.name) == 'filename.png' assert data.read() == b'<somedata>' data = client.request('file-response') assert os.path.basename(data.name) == 'filename (1).png' assert data.read() == b'<somedata>'
def request(ctx, operation, params, path, format, encoding, verbose): options = { 'schema': { 'path': path, 'format': format, 'encoding': encoding } } config = _load_config(options, verbose=verbose) path = config['schema']['path'] format = config['schema']['format'] encoding = config['schema']['encoding'] with open(path, 'rb') as schema_file: schema = schema_file.read() params = [param.partition('=') for param in params] params = dict([(key, value) for key, sep, value in params]) session = ctx.obj if verbose: session = DebugSession(session) try: client = Client(schema, format=format, encoding=encoding, session=session) except (ParseError, ValidationError) as exc: _echo_error(exc, schema, verbose=verbose) sys.exit(1) try: result = client.request(operation, **params) except ClientError as exc: for message in exc.messages: if message.code == 'invalid_property': text = '* Invalid parameter "%s".' % message.index[0] elif message.code == 'required': text = '* Missing required parameter "%s".' % message.index[0] else: text = '* %s' % message.text click.echo(text) click.echo(click.style('✘ ', fg='red') + 'Client error') sys.exit(1) except ErrorResponse as exc: click.echo(json.dumps(exc.content, indent=4)) click.echo(click.style('✘ ', fg='red') + exc.title) sys.exit(1) click.echo(json.dumps(result, indent=4))
def test_extra_param(): client = Client(schema, session=TestClient(app)) with pytest.raises(exceptions.ClientError): client.request("body-param", value={"example": 123}, extra=456)
def test_missing_param(): client = Client(schema, session=TestClient(app)) with pytest.raises(exceptions.ClientError): client.request("body-param")
def test_body_param(): client = Client(schema, session=TestClient(app)) data = client.request("body-param", value={"example": 123}) assert data == {"body": {"example": 123}}
def test_file_response_url_filename(): client = Client(schema, session=TestClient(app)) data = client.request('file-response-url-filename') assert os.path.basename(data.name) == 'name.png' assert data.read() == b'<somedata>'
def test_text_response(): client = Client(schema, session=TestClient(app)) data = client.request('text-response') assert data == 'hello, world'
def test_path_param(): client = Client(schema, session=TestClient(app)) data = client.request('path-param', value=123) assert data == {'value': '123'}
def test_file_response_no_name(): client = Client(schema, session=TestClient(app)) data = client.request('file-response-no-name') assert os.path.basename(data.name) == 'download.png' assert data.read() == b'<somedata>'
def test_token_auth(): session = TestClient(app) auth = TokenAuthentication("xxx") client = Client(schema, session=session, auth=auth) data = client.request("token-auth") assert data == {"authorization": "Bearer xxx"}
def test_file_response(): client = Client(schema, session=TestClient(app)) data = client.request("file-response") assert os.path.basename(data.name) == "filename.png" assert data.read() == b"<somedata>"
def test_token_auth(): session = TestClient(app) auth = TokenAuthentication('xxx') client = Client(schema, session=session, auth=auth) data = client.request('token-auth') assert data == {'authorization': 'Bearer xxx'}
def test_query_params(): client = Client(schema, session=TestClient(app)) data = client.request('query-params', a=123, b=456) assert data == {'query': {'a': '123', 'b': '456'}}
def test_path_param(): client = Client(schema, session=TestClient(app)) data = client.request("path-param", value=123) assert data == {"value": "123"}
def test_query_params(): client = Client(schema, session=TestClient(app)) data = client.request("query-params", a=123, b=456) assert data == {"query": {"a": "123", "b": "456"}}
def test_body_param(): client = Client(schema, session=TestClient(app)) data = client.request('body-param', value={'example': 123}) assert data == {'body': {'example': 123}}