Skip to content

Commit 627be58

Browse files
authored
safeguard contact info (geopython#1835) (geopython#1862)
1 parent 2262696 commit 627be58

File tree

4 files changed

+130
-30
lines changed

4 files changed

+130
-30
lines changed

pygeoapi/openapi.py

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -144,38 +144,81 @@ def gen_contact(cfg: dict) -> dict:
144144
:returns: `dict` of OpenAPI contact object
145145
"""
146146

147+
has_addresses = False
148+
has_phones = False
149+
147150
contact = {
148-
'name': cfg['metadata']['provider']['name'],
149-
'url': cfg['metadata']['provider']['url'],
150-
'email': cfg['metadata']['contact']['email']
151+
'name': cfg['metadata']['provider']['name']
151152
}
152153

154+
for key in ['url', 'email']:
155+
if key in cfg['metadata']['provider']:
156+
contact[key] = cfg['metadata']['provider'][key]
157+
153158
contact['x-ogc-serviceContact'] = {
154159
'name': cfg['metadata']['contact']['name'],
155-
'position': cfg['metadata']['contact']['position'],
156-
'addresses': [{
157-
'deliveryPoint': [cfg['metadata']['contact']['address']],
158-
'city': cfg['metadata']['contact']['city'],
159-
'administrativeArea': cfg['metadata']['contact']['stateorprovince'], # noqa
160-
'postalCode': cfg['metadata']['contact']['postalcode'],
161-
'country': cfg['metadata']['contact']['country']
162-
}],
163-
'phones': [{
164-
'type': 'main', 'value': cfg['metadata']['contact']['phone']
165-
}, {
166-
'type': 'fax', 'value': cfg['metadata']['contact']['fax']
167-
}],
168-
'emails': [{
160+
'addresses': []
161+
}
162+
163+
if 'position' in cfg['metadata']['contact']:
164+
contact['x-ogc-serviceContact']['position'] = cfg['metadata']['contact']['position'] # noqa
165+
166+
if any(address in ['address', 'city', 'stateorprovince', 'postalcode', 'country'] for address in cfg['metadata']['contact']): # noqa
167+
has_addresses = True
168+
169+
if has_addresses:
170+
address = {}
171+
if 'address' in cfg['metadata']['contact']:
172+
address['deliveryPoint'] = [cfg['metadata']['contact']['address']]
173+
174+
if 'city' in cfg['metadata']['contact']:
175+
address['city'] = cfg['metadata']['contact']['city']
176+
177+
if 'stateorprovince' in cfg['metadata']['contact']:
178+
address['administrativeArea'] = cfg['metadata']['contact']['stateorprovince'] # noqa
179+
180+
if 'postalCode' in cfg['metadata']['contact']:
181+
address['administrativeArea'] = cfg['metadata']['contact']['postalCode'] # noqa
182+
183+
if 'country' in cfg['metadata']['contact']:
184+
address['administrativeArea'] = cfg['metadata']['contact']['country'] # noqa
185+
186+
contact['x-ogc-serviceContact']['addresses'].append(address)
187+
188+
if any(phone in ['phone', 'fax'] for phone in cfg['metadata']['contact']):
189+
has_phones = True
190+
contact['x-ogc-serviceContact']['phones'] = []
191+
192+
if has_phones:
193+
if 'phone' in cfg['metadata']['contact']:
194+
contact['x-ogc-serviceContact']['phones'].append({
195+
'type': 'main', 'value': cfg['metadata']['contact']['phone']
196+
})
197+
198+
if 'fax' in cfg['metadata']['contact']:
199+
contact['x-ogc-serviceContact']['phones'].append({
200+
'type': 'fax', 'value': cfg['metadata']['contact']['fax']
201+
})
202+
203+
if 'email' in cfg['metadata']['contact']:
204+
contact['x-ogc-serviceContact']['emails'] = [{
169205
'value': cfg['metadata']['contact']['email']
170-
}],
171-
'contactInstructions': cfg['metadata']['contact']['instructions'],
172-
'links': [{
206+
}]
207+
208+
if 'url' in cfg['metadata']['contact']:
209+
contact['x-ogc-serviceContact']['links'] = [{
173210
'type': 'text/html',
174211
'href': cfg['metadata']['contact']['url']
175-
}],
176-
'hoursOfService': cfg['metadata']['contact']['hours'],
177-
'roles': [cfg['metadata']['contact']['role']]
178-
}
212+
}]
213+
214+
if 'instructions' in cfg['metadata']['contact']:
215+
contact['x-ogc-serviceContact']['contactInstructions'] = cfg['metadata']['contact']['instructions'] # noqa
216+
217+
if 'hours' in cfg['metadata']['contact']:
218+
contact['x-ogc-serviceContact']['hoursOfService'] = cfg['metadata']['contact']['hours'] # noqa
219+
220+
if 'role' in cfg['metadata']['contact']:
221+
contact['x-ogc-serviceContact']['hoursOfService'] = cfg['metadata']['contact']['role'] # noqa
179222

180223
return contact
181224

pygeoapi/templates/landing_page.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,12 @@ <h2>{% trans %}Tile Matrix Sets{% endtrans %}</h2>
121121
<div class="card-body">
122122
<b>{% trans %}Address{% endtrans %}</b><br/>
123123
<div class="section">
124-
<span>{{ config['metadata']['contact']['address'] }}</span><br/>
125-
<span>{{ config['metadata']['contact']['city'] }}</span>,
126-
<span>{{ config['metadata']['contact']['stateorprovince'] }}</span><br/>
127-
<span>{{ config['metadata']['contact']['postalcode'] }}</span><br/>
128-
<span>{{ config['metadata']['contact']['country'] }}</span>
124+
<span>{{ config['metadata']['contact']['name'] }}</span><br/>
125+
{% for key in ['address', 'city', 'stateorprovince', 'postalcode', 'country'] %}
126+
{% if key in config['metadata']['contact'] %}
127+
<span>{{ config['metadata']['contact'][key] }}</span><br/>
128+
{% endif %}
129+
{% endfor %}
129130
</div>
130131
<div>
131132
<b>{% trans %}Email{% endtrans %}</b><br/>

tests/pygeoapi-test-openapi.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ info:
8383
8484
name: Organization Name
8585
url: https://pygeoapi.io
86+
x-ogc-serviceContact:
87+
addresses:
88+
- administrativeArea: Country
89+
city:
90+
- City
91+
deliveryPoint:
92+
- Mailing Address
93+
contactInstructions: During hours of service. Off on weekends.
94+
emails:
95+
96+
hoursOfService: pointOfContact
97+
links:
98+
- href: Contact URL
99+
type: text/html
100+
name: Lastname, Firstname
101+
phones:
102+
- type: main
103+
value: +xx-xxx-xxx-xxxx
104+
- type: fax
105+
value: +xx-xxx-xxx-xxxx
106+
position: Position Title
86107
description: pygeoapi provides an API to geospatial data
87108
license:
88109
name: CC-BY 4.0 license

tests/test_openapi.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_str2bool():
7474
osl = get_ogc_schemas_location(default)
7575

7676

77-
def test_get_oas(config, openapi):
77+
def test_get_oas(config):
7878
openapi_doc = get_oas(config)
7979

8080
assert isinstance(openapi_doc, dict)
@@ -84,6 +84,41 @@ def test_get_oas(config, openapi):
8484
assert is_valid
8585

8686

87+
def test_get_oas_ogc_service_contact(config):
88+
89+
ogc_service_contact = {
90+
'addresses': [{
91+
'administrativeArea': 'Country',
92+
'city': 'City',
93+
'deliveryPoint': ['Mailing Address']
94+
}],
95+
'contactInstructions': 'During hours of service. Off on weekends.',
96+
'emails': [{
97+
'value': '[email protected]'
98+
}],
99+
'hoursOfService': 'pointOfContact',
100+
'links': [{
101+
'href': 'Contact URL',
102+
'type': 'text/html'
103+
}],
104+
'name': 'Lastname, Firstname',
105+
'phones': [{
106+
'type': 'main',
107+
'value': '+xx-xxx-xxx-xxxx'
108+
}, {
109+
'type': 'fax',
110+
'value': '+xx-xxx-xxx-xxxx'
111+
}],
112+
'position': 'Position Title'
113+
}
114+
115+
openapi_doc = get_oas(config)
116+
117+
assert isinstance(openapi_doc, dict)
118+
119+
assert openapi_doc['info']['contact']['x-ogc-serviceContact'] == ogc_service_contact # noqa
120+
121+
87122
def test_validate_openapi_document(openapi):
88123
is_valid = validate_openapi_document(openapi)
89124
assert is_valid

0 commit comments

Comments
 (0)