Skip to content

Commit

Permalink
safeguard contact info (#1835) (#1862)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis authored Dec 9, 2024
1 parent 2262696 commit 627be58
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 30 deletions.
91 changes: 67 additions & 24 deletions pygeoapi/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,38 +144,81 @@ def gen_contact(cfg: dict) -> dict:
:returns: `dict` of OpenAPI contact object
"""

has_addresses = False
has_phones = False

contact = {
'name': cfg['metadata']['provider']['name'],
'url': cfg['metadata']['provider']['url'],
'email': cfg['metadata']['contact']['email']
'name': cfg['metadata']['provider']['name']
}

for key in ['url', 'email']:
if key in cfg['metadata']['provider']:
contact[key] = cfg['metadata']['provider'][key]

contact['x-ogc-serviceContact'] = {
'name': cfg['metadata']['contact']['name'],
'position': cfg['metadata']['contact']['position'],
'addresses': [{
'deliveryPoint': [cfg['metadata']['contact']['address']],
'city': cfg['metadata']['contact']['city'],
'administrativeArea': cfg['metadata']['contact']['stateorprovince'], # noqa
'postalCode': cfg['metadata']['contact']['postalcode'],
'country': cfg['metadata']['contact']['country']
}],
'phones': [{
'type': 'main', 'value': cfg['metadata']['contact']['phone']
}, {
'type': 'fax', 'value': cfg['metadata']['contact']['fax']
}],
'emails': [{
'addresses': []
}

if 'position' in cfg['metadata']['contact']:
contact['x-ogc-serviceContact']['position'] = cfg['metadata']['contact']['position'] # noqa

if any(address in ['address', 'city', 'stateorprovince', 'postalcode', 'country'] for address in cfg['metadata']['contact']): # noqa
has_addresses = True

if has_addresses:
address = {}
if 'address' in cfg['metadata']['contact']:
address['deliveryPoint'] = [cfg['metadata']['contact']['address']]

if 'city' in cfg['metadata']['contact']:
address['city'] = cfg['metadata']['contact']['city']

if 'stateorprovince' in cfg['metadata']['contact']:
address['administrativeArea'] = cfg['metadata']['contact']['stateorprovince'] # noqa

if 'postalCode' in cfg['metadata']['contact']:
address['administrativeArea'] = cfg['metadata']['contact']['postalCode'] # noqa

if 'country' in cfg['metadata']['contact']:
address['administrativeArea'] = cfg['metadata']['contact']['country'] # noqa

contact['x-ogc-serviceContact']['addresses'].append(address)

if any(phone in ['phone', 'fax'] for phone in cfg['metadata']['contact']):
has_phones = True
contact['x-ogc-serviceContact']['phones'] = []

if has_phones:
if 'phone' in cfg['metadata']['contact']:
contact['x-ogc-serviceContact']['phones'].append({
'type': 'main', 'value': cfg['metadata']['contact']['phone']
})

if 'fax' in cfg['metadata']['contact']:
contact['x-ogc-serviceContact']['phones'].append({
'type': 'fax', 'value': cfg['metadata']['contact']['fax']
})

if 'email' in cfg['metadata']['contact']:
contact['x-ogc-serviceContact']['emails'] = [{
'value': cfg['metadata']['contact']['email']
}],
'contactInstructions': cfg['metadata']['contact']['instructions'],
'links': [{
}]

if 'url' in cfg['metadata']['contact']:
contact['x-ogc-serviceContact']['links'] = [{
'type': 'text/html',
'href': cfg['metadata']['contact']['url']
}],
'hoursOfService': cfg['metadata']['contact']['hours'],
'roles': [cfg['metadata']['contact']['role']]
}
}]

if 'instructions' in cfg['metadata']['contact']:
contact['x-ogc-serviceContact']['contactInstructions'] = cfg['metadata']['contact']['instructions'] # noqa

if 'hours' in cfg['metadata']['contact']:
contact['x-ogc-serviceContact']['hoursOfService'] = cfg['metadata']['contact']['hours'] # noqa

if 'role' in cfg['metadata']['contact']:
contact['x-ogc-serviceContact']['hoursOfService'] = cfg['metadata']['contact']['role'] # noqa

return contact

Expand Down
11 changes: 6 additions & 5 deletions pygeoapi/templates/landing_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ <h2>{% trans %}Tile Matrix Sets{% endtrans %}</h2>
<div class="card-body">
<b>{% trans %}Address{% endtrans %}</b><br/>
<div class="section">
<span>{{ config['metadata']['contact']['address'] }}</span><br/>
<span>{{ config['metadata']['contact']['city'] }}</span>,
<span>{{ config['metadata']['contact']['stateorprovince'] }}</span><br/>
<span>{{ config['metadata']['contact']['postalcode'] }}</span><br/>
<span>{{ config['metadata']['contact']['country'] }}</span>
<span>{{ config['metadata']['contact']['name'] }}</span><br/>
{% for key in ['address', 'city', 'stateorprovince', 'postalcode', 'country'] %}
{% if key in config['metadata']['contact'] %}
<span>{{ config['metadata']['contact'][key] }}</span><br/>
{% endif %}
{% endfor %}
</div>
<div>
<b>{% trans %}Email{% endtrans %}</b><br/>
Expand Down
21 changes: 21 additions & 0 deletions tests/pygeoapi-test-openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ info:
email: you@example.org
name: Organization Name
url: https://pygeoapi.io
x-ogc-serviceContact:
addresses:
- administrativeArea: Country
city:
- City
deliveryPoint:
- Mailing Address
contactInstructions: During hours of service. Off on weekends.
emails:
- value: you@example.org
hoursOfService: pointOfContact
links:
- href: Contact URL
type: text/html
name: Lastname, Firstname
phones:
- type: main
value: +xx-xxx-xxx-xxxx
- type: fax
value: +xx-xxx-xxx-xxxx
position: Position Title
description: pygeoapi provides an API to geospatial data
license:
name: CC-BY 4.0 license
Expand Down
37 changes: 36 additions & 1 deletion tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_str2bool():
osl = get_ogc_schemas_location(default)


def test_get_oas(config, openapi):
def test_get_oas(config):
openapi_doc = get_oas(config)

assert isinstance(openapi_doc, dict)
Expand All @@ -84,6 +84,41 @@ def test_get_oas(config, openapi):
assert is_valid


def test_get_oas_ogc_service_contact(config):

ogc_service_contact = {
'addresses': [{
'administrativeArea': 'Country',
'city': 'City',
'deliveryPoint': ['Mailing Address']
}],
'contactInstructions': 'During hours of service. Off on weekends.',
'emails': [{
'value': 'you@example.org'
}],
'hoursOfService': 'pointOfContact',
'links': [{
'href': 'Contact URL',
'type': 'text/html'
}],
'name': 'Lastname, Firstname',
'phones': [{
'type': 'main',
'value': '+xx-xxx-xxx-xxxx'
}, {
'type': 'fax',
'value': '+xx-xxx-xxx-xxxx'
}],
'position': 'Position Title'
}

openapi_doc = get_oas(config)

assert isinstance(openapi_doc, dict)

assert openapi_doc['info']['contact']['x-ogc-serviceContact'] == ogc_service_contact # noqa


def test_validate_openapi_document(openapi):
is_valid = validate_openapi_document(openapi)
assert is_valid
Expand Down

0 comments on commit 627be58

Please sign in to comment.