Skip to content

Commit

Permalink
Merge pull request #79 from TheJacksonLaboratory/G3-75-all-geneweaver…
Browse files Browse the repository at this point in the history
…-packages-need-to-upgrade-pydantic-2-0

G3 75 all geneweaver packages need to upgrade pydantic 2 0
  • Loading branch information
bergsalex authored Jul 15, 2024
2 parents 4ee37b7 + 6e7f18b commit da0f30e
Show file tree
Hide file tree
Showing 7 changed files with 413 additions and 181 deletions.
503 changes: 367 additions & 136 deletions poetry.lock

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "geneweaver-api"
version = "0.7.0a7"
version = "0.7.0a8"
description = "The Geneweaver API"
authors = [
"Alexander Berger <[email protected]>",
Expand All @@ -17,14 +17,15 @@ packages = [

[tool.poetry.dependencies]
python = "^3.9"
geneweaver-core = "^0.10.0a2"
fastapi = {extras = ["all"], version = "^0.99.1"}
uvicorn = {extras = ["standard"], version = "^0.24.0"}
geneweaver-db = "0.5.0a11"
geneweaver-core = "^0.10.0a3"
fastapi = {extras = ["all"], version = "^0.111.0"}
uvicorn = {extras = ["standard"], version = "^0.30.0"}
geneweaver-db = "0.5.0a12"
psycopg-pool = "^3.1.7"
requests = "^2.31.0"
requests = "^2.32.3"
python-jose = {extras = ["cryptography"], version = "^3.3.0"}
psycopg-binary = "3.1.18"
pydantic-settings = "^2.3.4"

[tool.poetry.group.dev.dependencies]
geneweaver-testing = "^0.1.2"
Expand Down
21 changes: 12 additions & 9 deletions src/geneweaver/api/controller/genesets.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""Endpoints related to genesets."""

import json
import os
import time
from tempfile import TemporaryDirectory
from typing import Optional, Set

from fastapi import APIRouter, Depends, HTTPException, Path, Query, Security
from fastapi.responses import FileResponse
from fastapi.responses import FileResponse, StreamingResponse
from geneweaver.api import dependencies as deps
from geneweaver.api.schemas.apimodels import GeneValueReturn
from geneweaver.api.schemas.auth import UserInternal
Expand Down Expand Up @@ -189,7 +188,7 @@ def get_export_geneset_by_id_type(
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
temp_dir: TemporaryDirectory = Depends(deps.get_temp_dir),
gene_id_type: Optional[GeneIdentifier] = None,
) -> FileResponse:
) -> StreamingResponse:
"""Export geneset into JSON file. Search by ID and optional gene identifier type."""
timestr = time.strftime("%Y%m%d-%H%M%S")

Expand All @@ -214,15 +213,19 @@ def get_export_geneset_by_id_type(
geneset_filename = f"geneset_{geneset_id}_{timestr}.json"

# Write the data to temp file
temp_file_path = os.path.join(temp_dir, geneset_filename)
with open(temp_file_path, "w") as f:
json.dump(response, f, default=str)
from io import StringIO

buffer = StringIO()

json.dump(response, buffer, default=str)

buffer.seek(0)

# Return as a download
return FileResponse(
path=temp_file_path,
return StreamingResponse(
buffer,
media_type="application/octet-stream",
filename=geneset_filename,
headers={"Content-Disposition": f"attachment; filename={geneset_filename}"},
)


Expand Down
42 changes: 21 additions & 21 deletions src/geneweaver/api/core/config_class.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Namespace for the config class for the Geneweaver API."""

from typing import Any, Dict, List, Optional
from typing import List, Optional

from geneweaver.db.core.settings_class import Settings as DBSettings
from pydantic import BaseSettings, validator
from pydantic import model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
from typing_extensions import Self


class GeneweaverAPIConfig(BaseSettings):
Expand All @@ -20,33 +22,31 @@ class GeneweaverAPIConfig(BaseSettings):
DB_PORT: int = 5432
DB: Optional[DBSettings] = None

@validator("DB", pre=True)
def assemble_db_settings(
cls, v: Optional[DBSettings], values: Dict[str, Any] # noqa: N805
) -> DBSettings:
@model_validator(mode="after")
def assemble_db_settings(self) -> Self:
"""Build the database settings."""
if isinstance(v, DBSettings):
return v
return DBSettings(
SERVER=values.get("DB_HOST"),
NAME=values.get("DB_NAME"),
USERNAME=values.get("DB_USERNAME"),
PASSWORD=values.get("DB_PASSWORD"),
PORT=values.get("DB_PORT"),
)
if not isinstance(self.DB, DBSettings):
self.DB = DBSettings(
SERVER=self.DB_HOST,
NAME=self.DB_NAME,
USERNAME=self.DB_USERNAME,
PASSWORD=self.DB_PASSWORD,
PORT=self.DB_PORT,
)
return self

AUTH_DOMAIN: str = "geneweaver.auth0.com"
AUTH_AUDIENCE: str = "https://api.geneweaver.org"
AUTH_ALGORITHMS: List[str] = ["RS256"]
AUTH_EMAIL_NAMESPACE: str = AUTH_AUDIENCE
AUTH_SCOPES = {
AUTH_SCOPES: dict = {
"openid profile email": "read",
}
JWT_PERMISSION_PREFIX: str = "approle"
AUTH_CLIENT_ID: str = "T7bj6wlmtVcAN2O6kzDRwPVFyIj4UQNs"

class Config:
"""Configuration for the BaseSettings class."""

env_file = ".env"
case_sensitive = True
model_config = SettingsConfigDict(
env_file=".env",
case_sensitive=False,
extra="ignore",
)
13 changes: 5 additions & 8 deletions src/geneweaver/api/schemas/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from enum import Enum
from typing import List, Optional

from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field


class AppRoles(str, Enum):
Expand All @@ -17,8 +17,8 @@ class AppRoles(str, Enum):
class User(BaseModel):
"""User model."""

email: Optional[str]
name: Optional[str]
email: Optional[str] = None
name: Optional[str] = None
sso_id: str = Field(None, alias="sub")
id: int = Field(None, alias="gw_id") # noqa: A003
role: Optional[AppRoles] = AppRoles.user
Expand All @@ -29,9 +29,6 @@ class UserInternal(User):

auth_header: dict = {}
token: str
permissions: Optional[List[str]]
permissions: Optional[List[str]] = None

class Config:
"""Pydantic config."""

allow_population_by_field_name = True
model_config = ConfigDict(populate_by_name=True)
1 change: 1 addition & 0 deletions tests/controllers/test_api_standards.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def get_openapi_json():
from unittest.mock import patch

config = GeneweaverAPIConfig(
_env_file=None,
DB_HOST="localhost",
DB_USERNAME="postgres",
DB_PASSWORD="postgres",
Expand Down
1 change: 0 additions & 1 deletion tests/controllers/test_genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def test_export_geneset_w_gene_id_type(mock_service_get_geneset_w_gene_id_type,
response = client.get("/api/genesets/1234/file?gene_id_type=2")

assert response.headers.get("content-type") == "application/octet-stream"
assert int(response.headers.get("content-length")) > 0
assert response.status_code == 200


Expand Down

0 comments on commit da0f30e

Please sign in to comment.