-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy path__init__.py
More file actions
70 lines (58 loc) · 1.8 KB
/
__init__.py
File metadata and controls
70 lines (58 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import logging
from typing import Dict
import azure.functions as func
from funclib.errors import BBoxTooLargeError
from pydantic import ValidationError
from .models import StatisticsRequest
from .settings import StatisticsSettings
from .statistics import PcMosaicImage
async def main(req: func.HttpRequest) -> func.HttpResponse:
try:
body = req.get_json()
except ValueError:
return func.HttpResponse(
status_code=400,
mimetype="application/text",
body="Error: Invalid JSON",
)
try:
parsed_request = StatisticsRequest(**body)
except ValidationError as e:
return func.HttpResponse(
status_code=400,
mimetype="application/json",
body=e.json(),
)
try:
response = await handle_request(parsed_request)
return func.HttpResponse(
status_code=200,
mimetype="application/json",
body=json.dumps(response),
)
except BBoxTooLargeError as e:
logging.exception(e)
return func.HttpResponse(
status_code=400,
mimetype="application/json",
body=json.dumps({"error": str(e)}),
)
except Exception as e:
logging.exception(e)
return func.HttpResponse(
status_code=500,
mimetype="application/json",
)
async def handle_request(req: StatisticsRequest) -> Dict:
settings = StatisticsSettings.get()
mosaic_image = PcMosaicImage(
bbox=req.bbox,
zoom=req.zoom,
cql=req.cql,
render_options=req.get_render_options(),
settings=settings,
data_api_url_override=req.data_api_url,
)
img = await mosaic_image.get()
return {k: v.dict() for k, v in img.statistics().items()}