Skip to content

Commit

Permalink
Add a geocoding endpoint to find the names iso3166 uses for a country #…
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Mar 14, 2022
1 parent e15c5f7 commit 2a5d1e9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
33 changes: 32 additions & 1 deletion geocoding/location-service/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ info:
tags:
- name: Geocode
description: Geolocation of a case
- name: Convert
description: Converting geocoding-related values
paths:
/geocode/seed:
post:
Expand Down Expand Up @@ -68,7 +70,7 @@ paths:
$ref: '#/components/responses/500'
/geocode/convertUTM:
get:
tags: [Convert, Geocode]
tags: [Convert]
summary: Convert UTM coordinates to latitude/longitude.
operationId: convert
parameters:
Expand Down Expand Up @@ -97,6 +99,29 @@ paths:
$ref: '#/components/responses/400'
'500':
$ref: '#/components/responses/500'
/geocode/countryName:
get:
tags: [Convert]
summary: Convert ISO-3166-1 2-letter country code to the country's name.
operationId: countryName
parameters:
- name: c
in: query
required: true
description: The ISO-3166-1 country code
example: CR
schema:
type: string
responses:
'200':
$ref: '#/components/responses/200String'
'400':
$ref: '#/components/responses/400'
'404':
$ref: '#/components/responses/404'
'500':
$ref: '#/components/responses/500'

components:
schemas:
Location:
Expand Down Expand Up @@ -156,6 +181,12 @@ components:
application/json:
schema:
$ref: '#components/schemas/Position'
'200String':
description: OK
content:
application/json:
schema:
type: string
'200':
description: OK
'400':
Expand Down
15 changes: 15 additions & 0 deletions geocoding/location-service/src/app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import json
import iso3166
import logging
import pymongo
from flask import Blueprint, Flask, jsonify, request
Expand Down Expand Up @@ -125,6 +126,20 @@ def convert_geocode():
}
return jsonify(position)

@app.route("/geocode/countryName")
def country_name():
code = request.args.get('c', type=str)
if not code:
logger.warning(f"No country code in request {request}")
return "No country code", 400
if len(code) != 2:
logger.warning(f"Country code {code} is not two characters long in request {request}")
return "Bad ISO-3166-1 country code", 400
country = iso3166.countries_by_alpha2.get(code)
if country is None:
return "Unknown country code", 404
return country.name, 200


if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)

0 comments on commit 2a5d1e9

Please sign in to comment.