Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release to main #63

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/v1/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def get_node(node_id: int):
if aed is None:
raise HTTPException(404, f'Node {node_id} not found')

x, y = get_coordinates(aed.position)[0]
x, y = get_coordinates(aed.position)[0].tolist()

photo_dict = await _get_image_data(aed.tags)

Expand Down
6 changes: 4 additions & 2 deletions api/v1/photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from urllib.parse import unquote_plus

import magic
import orjson
from bs4 import BeautifulSoup
from fastapi import APIRouter, File, Form, HTTPException, Request, Response, UploadFile
from fastapi.responses import FileResponse
from feedgen.feed import FeedGenerator
from msgspec.json import Decoder

from config import IMAGE_CONTENT_TYPES, REMOTE_IMAGE_MAX_FILE_SIZE
from middlewares.cache_middleware import configure_cache
Expand All @@ -19,6 +19,8 @@
from states.photo_state import PhotoState
from utils import get_http_client, get_wikimedia_commons_url

_json_decode = Decoder().decode

router = APIRouter(prefix='/photos')


Expand Down Expand Up @@ -108,7 +110,7 @@ async def upload(
raise HTTPException(400, f'Unsupported file type {content_type!r}, must be one of {IMAGE_CONTENT_TYPES}')

try:
oauth2_credentials_ = orjson.loads(oauth2_credentials)
oauth2_credentials_: dict = _json_decode(oauth2_credentials)
except Exception as e:
raise HTTPException(400, 'OAuth2 credentials must be a JSON object') from e

Expand Down
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sentry_sdk.integrations.pymongo import PyMongoIntegration

NAME = 'openaedmap-backend'
VERSION = '2.7.6'
VERSION = '2.8.0'
CREATED_BY = f'{NAME} {VERSION}'
WEBSITE = 'https://openaedmap.org'

Expand All @@ -37,7 +37,7 @@
OVERPASS_API_URL = 'https://overpass-api.de/api/interpreter'
OPENSTREETMAP_API_URL = os.getenv('OPENSTREETMAP_API_URL', 'https://api.openstreetmap.org/api/0.6/')
REPLICATION_URL = 'https://planet.openstreetmap.org/replication/minute/'
COUNTRIES_GEOJSON_URL = 'https://osm-countries-geojson.monicz.dev/osm-countries-0-01.geojson.br'
COUNTRIES_GEOJSON_URL = 'https://osm-countries-geojson.monicz.dev/osm-countries-0-01.geojson.zst'

DEFAULT_CACHE_MAX_AGE = timedelta(minutes=1)
DEFAULT_CACHE_STALE = timedelta(minutes=5)
Expand Down
14 changes: 14 additions & 0 deletions json_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import override

from fastapi.responses import JSONResponse
from msgspec.json import Encoder

_encode = Encoder(decimal_format='number').encode


class CustomJSONResponse(JSONResponse):
media_type = 'application/json; charset=utf-8'

@override
def render(self, content) -> bytes:
return _encode(content)
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from fastapi.middleware.cors import CORSMiddleware

from config import DEFAULT_CACHE_MAX_AGE, DEFAULT_CACHE_STALE, startup_setup
from json_response import CustomJSONResponse
from middlewares.cache_middleware import CacheMiddleware
from middlewares.profiler_middleware import ProfilerMiddleware
from middlewares.version_middleware import VersionMiddleware
from orjson_response import CustomORJSONResponse
from states.aed_state import AEDState
from states.country_state import CountryState
from states.worker_state import WorkerState, WorkerStateEnum
Expand All @@ -39,7 +39,7 @@ async def lifespan(_):
yield


app = FastAPI(lifespan=lifespan, default_response_class=CustomORJSONResponse)
app = FastAPI(lifespan=lifespan, default_response_class=CustomJSONResponse)
app.add_middleware(ProfilerMiddleware)
app.add_middleware(VersionMiddleware)
app.add_middleware(
Expand Down
4 changes: 2 additions & 2 deletions middlewares/skip_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from fastapi import Response

from orjson_response import CustomORJSONResponse
from json_response import CustomJSONResponse


def skip_serialization(headers: Mapping[str, str] | None = None):
Expand All @@ -13,7 +13,7 @@ async def wrapper(*args, **kwargs):
raw_response = await func(*args, **kwargs)
if isinstance(raw_response, Response):
return raw_response
return CustomORJSONResponse(raw_response, headers=headers)
return CustomJSONResponse(raw_response, headers=headers)

return wrapper

Expand Down
5 changes: 0 additions & 5 deletions orjson_response.py

This file was deleted.

11 changes: 7 additions & 4 deletions osm_countries.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from collections.abc import Sequence

import brotlicffi as brotli
import orjson
from msgspec.json import Decoder
from sentry_sdk import trace
from shapely.geometry import shape
from zstandard import ZstdDecompressor

from config import COUNTRIES_GEOJSON_URL
from models.osm_country import OSMCountry
from utils import get_http_client

_zstd_decompress = ZstdDecompressor().decompress
_json_decode = Decoder().decode


@trace
async def get_osm_countries() -> Sequence[OSMCountry]:
Expand All @@ -17,8 +20,8 @@ async def get_osm_countries() -> Sequence[OSMCountry]:
r.raise_for_status()

buffer = r.read()
buffer = brotli.decompress(buffer)
data = orjson.loads(buffer)
buffer = _zstd_decompress(buffer)
data: dict = _json_decode(buffer)

result = []

Expand Down
Loading