-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from openearthplatforminitiative/feat/add-code…
…-examples feat: add code examples
- Loading branch information
Showing
14 changed files
with
177 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
curl -i -X GET $endpoint_url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Get yearly forest cover loss for the river basin at the given point | ||
curl -i -X GET $endpoint_url?lon=30.0619&lat=-1.9441 | ||
|
||
# Get yearly forest cover loss for all river basins within the given bounding box | ||
curl -i -X GET $endpoint_url?min_lon=28.850951&min_lat=30.909622&max_lon=-2.840114&max_lat=-1.041395 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
curl -i -X GET $endpoint_url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const response = await fetch( | ||
"$endpoint_url" | ||
); | ||
const json = await response.json(); | ||
const message = json.message; | ||
|
||
console.log(`Message: ${message}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Get yearly forest cover loss for the river basin at the given point | ||
const response_point = await fetch( | ||
"$endpoint_url?" + new URLSearchParams({ | ||
lat: -1.9441, | ||
lon: 30.0619 | ||
}) | ||
); | ||
const data_point = await response_point.json(); | ||
|
||
// Print the total forest cover loss within the returned river basin | ||
console.log(data_point.features[0].properties.daterange_tot_treeloss); | ||
|
||
|
||
// Get yearly forest cover loss for all river basins within the given bounding box | ||
const response_bbox = await fetch( | ||
"$endpoint_url?" + new URLSearchParams({ | ||
min_lon: 28.850951, | ||
min_lat: -2.840114, | ||
max_lon: 30.909622, | ||
max_lat: -1.041395 | ||
}) | ||
); | ||
const data_bbox = await response_bbox.json(); | ||
|
||
// Print the total forest cover loss within the first returned river basin | ||
console.log(data_bbox.features[0].properties.daterange_tot_treeloss); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const response = await fetch( | ||
"$endpoint_url" | ||
); | ||
const json = await response.json(); | ||
const status = json.status; | ||
|
||
console.log(`Status: ${status}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from httpx import Client | ||
|
||
with Client() as client: | ||
response = client.get(url="$endpoint_url") | ||
print(response.json()["message"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from httpx import Client | ||
|
||
with Client() as client: | ||
# Get yearly forest cover loss for the river basin at the given point | ||
response_point = client.get( | ||
url="$endpoint_url", | ||
params={"lon": 30.0619, "lat": -1.9441}, | ||
) | ||
|
||
data_point = response_point.json() | ||
|
||
# Print the total forest cover loss within the returned river basin | ||
print(data_point["features"][0]["properties"]["daterange_tot_treeloss"]) | ||
|
||
# Get yearly forest cover loss for all river basins within the given bounding box | ||
response_bbox = client.get( | ||
url="$endpoint_url", | ||
params={ | ||
"min_lon": 28.850951, | ||
"min_lat": -2.840114, | ||
"max_lon": 30.909622, | ||
"max_lat": -1.041395, | ||
}, | ||
) | ||
|
||
data_bbox = response_bbox.json() | ||
|
||
# Print the total forest cover loss within the first returned river basin | ||
print(data_bbox["features"][0]["properties"]["daterange_tot_treeloss"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from httpx import Client | ||
|
||
with Client() as client: | ||
response = client.get(url="$endpoint_url") | ||
print(response.json()["status"]) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import logging | ||
import os | ||
from pathlib import Path | ||
from string import Template | ||
|
||
from fastapi import FastAPI | ||
from fastapi.openapi.utils import get_openapi | ||
from fastapi.routing import APIRoute | ||
|
||
from deforestation_api.settings import settings | ||
|
||
supported_languages = {"cURL": "sh", "JavaScript": "js", "Python": "py"} | ||
|
||
|
||
def custom_openapi(app: FastAPI, example_code_dir: Path): | ||
if app.openapi_schema: | ||
return app.openapi_schema | ||
|
||
openapi_schema = get_openapi( | ||
title="Deforestation API", | ||
version=settings.version, | ||
description=settings.api_description, | ||
routes=app.routes, | ||
) | ||
|
||
openapi_schema["info"]["x-logo"] = { | ||
"url": f"https://{settings.api_domain}/assets/icons/open-epi-logo.svg" | ||
} | ||
|
||
routes_that_need_doc = [ | ||
route for route in app.routes if isinstance(route, APIRoute) | ||
] | ||
for route in routes_that_need_doc: | ||
code_samples = [] | ||
for lang in supported_languages: | ||
file_with_code_sample = ( | ||
example_code_dir | ||
/ lang.lower() | ||
/ f"{route.name}.{supported_languages[lang]}" | ||
) | ||
if os.path.isfile(file_with_code_sample): | ||
with open(file_with_code_sample) as f: | ||
code_template = Template(f.read()) | ||
code_samples.append( | ||
{ | ||
"lang": lang, | ||
"source": code_template.safe_substitute( | ||
endpoint_url=f"{settings.api_url}{route.path}", | ||
), | ||
} | ||
) | ||
else: | ||
logging.warning( | ||
"No code sample found for route %s and language %s", | ||
route.path, | ||
lang, | ||
) | ||
|
||
if code_samples: | ||
openapi_schema["paths"][route.path]["get"]["x-codeSamples"] = code_samples | ||
|
||
return openapi_schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters