Exemplo n.º 1
0
def test_yaml_load():
    with open(get_test_file_path('pygeoapi-test-config.yml')) as fh:
        d = util.yaml_load(fh)
        assert isinstance(d, dict)
    with pytest.raises(FileNotFoundError):
        with open(get_test_file_path('404.yml')) as fh:
            d = util.yaml_load(fh)
Exemplo n.º 2
0
    def _from_env(self):
        """
        Private function: Load environment data into
        provider attributes

        """
        import os
        with open(os.getenv('PYGEOAPI_CONFIG'), encoding='utf8') as fh:
            CONFIG = yaml_load(fh)
            self._rel_link = CONFIG['server']['url']

            # Validate intra-links
            for (name, rs) in CONFIG['resources'].items():
                if not rs.get('providers') or \
                   rs['providers'][0]['name'] != 'SensorThings':
                    continue

                _entity = rs['providers'][0].get('entity')
                uri = rs['providers'][0].get('uri_field', '')

                for p in rs['providers']:
                    # Validate linkable provider
                    if (p['name'] != 'SensorThings'
                            or not p.get('intralink', False)):
                        continue

                    if p.get('default', False) is True:
                        _entity = p['entity']
                        uri = p['uri_field']

                    self._linkables[_entity] = {}
                    self._linkables[_entity].update({
                        'n': name, 'u': uri
                    })
Exemplo n.º 3
0
def generate_openapi_document(ctx, config_file):
    """Generate OpenAPI Document"""

    if config_file is None:
        raise click.ClickException('--config/-c required')
    with open(config_file) as ff:
        s = yaml_load(ff)
        click.echo(yaml.safe_dump(get_oas(s), default_flow_style=False))
Exemplo n.º 4
0
def test_config_envvars():
    os.environ['PYGEOAPI_PORT'] = '5001'
    os.environ['PYGEOAPI_TITLE'] = 'my title'

    with open(get_test_file_path('pygeoapi-test-config-envvars.yml')) as fh:
        config = yaml_load(fh)

    assert isinstance(config, dict)
    assert config['server']['bind']['port'] == 5001
    assert config['metadata']['identification']['title'] == \
        'pygeoapi default instance my title'

    os.environ.pop('PYGEOAPI_PORT')

    with pytest.raises(EnvironmentError):
        with open(get_test_file_path(
                'pygeoapi-test-config-envvars.yml')) as fh:  # noqa
            config = yaml_load(fh)
Exemplo n.º 5
0
def validate(ctx, openapi_file):
    """Validate OpenAPI Document"""

    if openapi_file is None:
        raise click.ClickException('--openapi/-o required')

    click.echo('Validating {}'.format(openapi_file))
    instance = yaml_load(openapi_file)
    validate_openapi_document(instance)
    click.echo('Valid OpenAPI document')
Exemplo n.º 6
0
def test_get_provider_default():
    with open(get_test_file_path('pygeoapi-test-config.yml')) as fh:
        d = util.yaml_load(fh)

    pd = util.get_provider_default(d['resources']['obs']['providers'])

    assert pd['type'] == 'feature'
    assert pd['name'] == 'CSV'

    pd = util.get_provider_default(d['resources']['obs']['providers'])
Exemplo n.º 7
0
def validate(ctx, config_file):
    """Validate configuration"""

    if config_file is None:
        raise click.ClickException('--config/-c required')

    with open(config_file) as ff:
        click.echo('Validating {}'.format(config_file))
        instance = yaml_load(ff)
        validate_config(instance)
        click.echo('Valid configuration')
Exemplo n.º 8
0
def test_filter_dict_by_key_value():
    with open(get_test_file_path('pygeoapi-test-config.yml')) as fh:
        d = util.yaml_load(fh)

    collections = util.filter_dict_by_key_value(d['resources'], 'type',
                                                'collection')
    assert len(collections) == 1

    notfound = util.filter_dict_by_key_value(d['resources'], 'type', 'foo')

    assert len(notfound) == 0
Exemplo n.º 9
0
def generate_openapi_document(ctx, config_file, format_='yaml'):
    """Generate OpenAPI Document"""

    if config_file is None:
        raise click.ClickException('--config/-c required')
    with open(config_file) as ff:
        s = yaml_load(ff)
        pretty_print = s['server'].get('pretty_print', False)
        if format_ == 'yaml':
            click.echo(yaml.safe_dump(get_oas(s), default_flow_style=False))
        else:
            click.echo(to_json(get_oas(s), pretty=pretty_print))
Exemplo n.º 10
0
def api():
    with open(os.environ.get('PYGEOAPI_OPENAPI')) as ff:
        openapi = yaml_load(ff)

    headers, status_code, content = api_.api(request.headers, request.args,
                                             openapi)

    response = make_response(content, status_code)
    if headers:
        response.headers = headers

    return response
Exemplo n.º 11
0
def openapi():
    """
    OpenAPI endpoint

    :returns: HTTP response
    """
    with open(os.environ.get('PYGEOAPI_OPENAPI'), encoding='utf8') as ff:
        if os.environ.get('PYGEOAPI_OPENAPI').endswith(('.yaml', '.yml')):
            openapi_ = yaml_load(ff)
        else:  # JSON file, do not transform
            openapi_ = ff

    return get_response(api_.openapi(request, openapi_))
Exemplo n.º 12
0
def test_get_provider_by_type():
    with open(get_test_file_path('pygeoapi-test-config.yml')) as fh:
        d = util.yaml_load(fh)

    p = util.get_provider_by_type(d['resources']['obs']['providers'],
                                  'feature')

    assert isinstance(p, dict)
    assert p['type'] == 'feature'
    assert p['name'] == 'CSV'

    with pytest.raises(ProviderTypeError):
        p = util.get_provider_by_type(d['resources']['obs']['providers'],
                                      'something-else')
Exemplo n.º 13
0
async def openapi(request: Request):
    """
    OpenAPI endpoint

    :param request: Starlette Request instance

    :returns: Starlette HTTP Response
    """
    with open(os.environ.get('PYGEOAPI_OPENAPI'), encoding='utf8') as ff:
        if os.environ.get('PYGEOAPI_OPENAPI').endswith(('.yaml', '.yml')):
            openapi_ = yaml_load(ff)
        else:  # JSON file, do not transform
            openapi_ = ff

    return get_response(api_.openapi(request, openapi_))
Exemplo n.º 14
0
async def api(request: Request):
    """
    OpenAPI access point

    :returns: Starlette HTTP Response
    """
    with open(os.environ.get('PYGEOAPI_OPENAPI')) as ff:
        openapi = yaml_load(ff)

    headers, status_code, content = api_.api(request.headers,
                                             request.query_params, openapi)

    response = Response(content=content, status_code=status_code)
    if headers:
        response.headers.update(headers)

    return response
Exemplo n.º 15
0
def openapi():
    """
    OpenAPI access point

    :returns: HTTP response
    """
    with open(os.environ.get('PYGEOAPI_OPENAPI'), encoding='utf8') as ff:
        openapi = yaml_load(ff)

    headers, status_code, content = api_.openapi(request.headers, request.args,
                                                 openapi)

    response = make_response(content, status_code)
    if headers:
        response.headers = headers

    return response
Exemplo n.º 16
0
def validate_config(instance_dict):
    """
    Validate pygeoapi configuration against pygeoapi schema

    :param instance_dict: dict of configuration

    :returns: `bool` of validation
    """

    schema_file = os.path.join(THISDIR, 'schemas', 'config',
                               'pygeoapi-config-0.x.yml')

    with open(schema_file) as fh2:
        schema_dict = yaml_load(fh2)
        jsonschema_validate(json.loads(to_json(instance_dict)), schema_dict)

        return True
Exemplo n.º 17
0
def test_validate_datetime():
    config = yaml_load('''
        temporal:
            begin: 2000-10-30T18:24:39Z
            end: 2007-10-30T08:57:29Z
    ''')

    # test time instant
    assert validate_datetime(config, '2004') == '2004'
    assert validate_datetime(config, '2004-10') == '2004-10'
    assert validate_datetime(config, '2001-10-30') == '2001-10-30'

    with pytest.raises(ValueError):
        _ = validate_datetime(config, '2009-10-30')
    with pytest.raises(ValueError):
        _ = validate_datetime(config, '2000-09-09')
    with pytest.raises(ValueError):
        _ = validate_datetime(config, '2000-10-30T17:24:39Z')
    with pytest.raises(ValueError):
        _ = validate_datetime(config, '2007-10-30T08:58:29Z')

    # test time envelope
    assert validate_datetime(config, '2004/2005') == '2004/2005'
    assert validate_datetime(config, '2004-10/2005-10') == '2004-10/2005-10'
    assert (validate_datetime(config, '2001-10-30/2002-10-30') ==
            '2001-10-30/2002-10-30')
    assert validate_datetime(config, '2004/..') == '2004/..'
    assert validate_datetime(config, '../2005') == '../2005'
    assert validate_datetime(config, '2004/') == '2004/..'
    assert validate_datetime(config, '/2005') == '../2005'
    assert validate_datetime(config, '2004-10/2005-10') == '2004-10/2005-10'
    assert (validate_datetime(config, '2001-10-30/2002-10-30') ==
            '2001-10-30/2002-10-30')

    with pytest.raises(ValueError):
        _ = validate_datetime(config, '1999/..')
    with pytest.raises(ValueError):
        _ = validate_datetime(config, '2000/..')
    with pytest.raises(ValueError):
        _ = validate_datetime(config, '../2007')
    with pytest.raises(ValueError):
        _ = validate_datetime(config, '../2010')
Exemplo n.º 18
0
def openapi():
    """
    OpenAPI endpoint

    :returns: HTTP response
    """

    with open(os.environ.get('PYGEOAPI_OPENAPI'), encoding='utf8') as ff:
        if os.environ.get('PYGEOAPI_OPENAPI').endswith(('.yaml', '.yml')):
            openapi = yaml_load(ff)
        else:  # JSON file, do not transform
            openapi = ff

        headers, status_code, content = api_.openapi(
            request.headers, request.args, openapi)

        response = make_response(content, status_code)

        if headers:
            response.headers = headers

        return response
Exemplo n.º 19
0
async def openapi(request: Request):
    """
    OpenAPI endpoint

    :returns: Starlette HTTP Response
    """

    with open(os.environ.get('PYGEOAPI_OPENAPI'), encoding='utf8') as ff:
        if os.environ.get('PYGEOAPI_OPENAPI').endswith(('.yaml', '.yml')):
            openapi = yaml_load(ff)
        else:  # JSON file, do not transform
            openapi = ff

        headers, status_code, content = api_.openapi(request.headers,
                                                     request.query_params,
                                                     openapi)

        response = Response(content=content, status_code=status_code)

        if headers:
            response.headers.update(headers)

        return response
Exemplo n.º 20
0
def config():
    with open(get_test_file_path('pygeoapi-test-config.yml')) as fh:
        return yaml_load(fh)
Exemplo n.º 21
0
def openapi():
    with open(get_test_file_path('pygeoapi-test-openapi.yml')) as fh:
        return yaml_load(fh)
Exemplo n.º 22
0
from starlette.staticfiles import StaticFiles
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import Response
import uvicorn

from pygeoapi.api import API
from pygeoapi.util import yaml_load

CONFIG = None

if 'PYGEOAPI_CONFIG' not in os.environ:
    raise RuntimeError('PYGEOAPI_CONFIG environment variable not set')

with open(os.environ.get('PYGEOAPI_CONFIG'), encoding='utf8') as fh:
    CONFIG = yaml_load(fh)

STATIC_DIR = '{}{}static'.format(os.path.dirname(os.path.realpath(__file__)),
                                 os.sep)
if 'templates' in CONFIG['server']:
    STATIC_DIR = CONFIG['server']['templates'].get('static', STATIC_DIR)

app = Starlette()
app.mount('/static', StaticFiles(directory=STATIC_DIR))

# CORS: optionally enable from config.
if CONFIG['server'].get('cors', False):
    from starlette.middleware.cors import CORSMiddleware
    app.add_middleware(CORSMiddleware, allow_origins=['*'])

OGC_SCHEMAS_LOCATION = CONFIG['server'].get('ogc_schemas_location', None)