Skip to content

Commit

Permalink
read dynamically slug in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-crkn committed Nov 6, 2024
1 parent 35c8eee commit 02bbac0
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Azure_auth/jwt_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import jwt
# load .env file
load_dotenv()
jwt_secret = os.getenv("EDITOR_SECRET_KEY")
jwt_secret = os.getenv("JWT_SECRET")
#define a HTTPBearer to handle the token from client
client_token_scheme = HTTPBearer()
# JWT authentication function
Expand Down
5 changes: 3 additions & 2 deletions swift_config/swift_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
logger = logging.getLogger(__name__)

def get_swift_connection():
"""
Initialize the swift connection
"""
try:

return swiftclient.Connection(
user=SWIFT_USER,
key=SWIFT_KEY,
authurl=SWIFT_AUTH_URL,
preauthurl=SWIFT_PREAUTH_URL
)

except (ClientException) as conn_error:
logger.error(f"Failed to connect to Swift: {conn_error}")
raise HTTPException(status_code=500, detail=f"Failed to connect to Swift: {conn_error}")
1 change: 0 additions & 1 deletion utils/get_manifest_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ async def get_manifest_conn(slug:str,request: Request):
#Access Swift and Redis objects from the app's state
redis = request.app.state.redis
manifest_name = f'{slug}/manifest.json'

#Check Redis cache,if exists return from redis
if (cached_profile := await redis.get(f"manifest_{slug}")) is not None:
return pickle.loads(cached_profile)
Expand Down
7 changes: 1 addition & 6 deletions utils/lifespan_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,8 @@ async def initialize_swift():
"X-Auth-Key":swift_key
}
try:
logger.info(f"Attempting pre-authentication with SWIFT_PREAUTH_URL: {swift_preauth_url}")
# Step1: Get a preauth token with preauth_url
async with swift_session.get(swift_preauth_url, headers=headers) as preauth_resp:
logger.info(f"Received preauth response with status {preauth_resp.status}")
logger.info(f"Pre-auth response headers: {preauth_resp.headers}")
if preauth_resp.status in (200, 204):
preauth_token = preauth_resp.headers.get("X-Auth-Token")
if not preauth_token:
Expand Down
9 changes: 9 additions & 0 deletions utils/metadata_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def get_slug_in_metadata(metadata:list) ->list:
"""
Retrieves the value corresponding to the label 'Slug' from a list of metadata items.
"""
for item in metadata:
for key,value in item["label"].items():
if value[0].lower() == "slug":
return list(item["value"].values())[0]
return None
12 changes: 10 additions & 2 deletions utils/slug.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
from fastapi import HTTPException
import logging
from utils.metadata_slug import get_slug_in_metadata

#config logger
logging.basicConfig(level=logging.INFO,handlers=[logging.StreamHandler()])
logger = logging.getLogger(__name__)

async def get_slug(repo,id):
try:
_metadata = await repo.get_slug(id)
if _metadata is None or "value" not in _metadata[0] or "none" not in _metadata[0]["value"]:
if _metadata is None :
raise HTTPException(status_code=404, detail=f"Slug not found for the associated ID: {id}")
slug = _metadata[0]["value"]["none"][0]
slug = get_slug_in_metadata(_metadata)[0]
return slug
except Exception as e:
logger.error(f"Failed to get the slug: {e}")
raise HTTPException(status_code=404, detail=f"Slug not found for the associated ID: {id}")
5 changes: 3 additions & 2 deletions utils/upload_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from urllib.parse import urlparse
import botocore
from swift_config.swift_config import get_swift_connection
from utils.metadata_slug import get_slug_in_metadata

# Load .env file
load_dotenv()
Expand Down Expand Up @@ -60,8 +61,8 @@ async def upload_manifest_backend(
raise HTTPException(status_code=500, detail="Failed to read the uploaded file.")
manifest = json.loads(content)
try:
slug_value_dict = manifest['metadata'][0]['value']
slug = list(slug_value_dict.values())[0][0]
slug_value_dict = get_slug_in_metadata(manifest['metadata'])[0]
slug = slug_value_dict[0]
except Exception as e:
logger.error(f"Error extracting slug from manifest: {e}")
raise HTTPException(status_code=400, detail="Invalid manifest structure: missing slug.")
Expand Down

0 comments on commit 02bbac0

Please sign in to comment.