Skip to content

Commit

Permalink
Merge pull request #150 from openforcefield/user-async-batch
Browse files Browse the repository at this point in the history
AlchemiscalClient async+bulk for results, other methods; add request, response compression for large objects
  • Loading branch information
dotsdl authored Jun 29, 2023
2 parents f701cb2 + f70a6c9 commit dab275b
Show file tree
Hide file tree
Showing 13 changed files with 889 additions and 233 deletions.
27 changes: 25 additions & 2 deletions alchemiscale/base/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@


from functools import lru_cache
from typing import Any, Union, Dict, List
from typing import Any, Union, Dict, List, Callable
import os
import json
import gzip

from starlette.responses import JSONResponse
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi import APIRouter, Depends, HTTPException, status, Request, Response
from fastapi.routing import APIRoute
from fastapi.security import OAuth2PasswordRequestForm
from py2neo import Graph
from gufe import AlchemicalNetwork, ChemicalSystem, Transformation
Expand Down Expand Up @@ -146,6 +148,27 @@ def render(self, content: Any) -> bytes:
return json.dumps(content, cls=JSON_HANDLER.encoder).encode("utf-8")


class GzipRequest(Request):
async def body(self) -> bytes:
if not hasattr(self, "_body"):
body = await super().body()
if "gzip" in self.headers.getlist("Content-Encoding"):
body = gzip.decompress(body)
self._body = body
return self._body


class GzipRoute(APIRoute):
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()

async def custom_route_handler(request: Request) -> Response:
request = GzipRequest(request.scope, request.receive)
return await original_route_handler(request)

return custom_route_handler


def scope_params(org: str = None, campaign: str = None, project: str = None):
try:
return Scope(org=org, campaign=campaign, project=project)
Expand Down
Loading

0 comments on commit dab275b

Please sign in to comment.