Skip to content

Commit

Permalink
use async swift in get_manifest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-crkn committed Oct 31, 2024
1 parent e5502fe commit c0a0c28
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 48 deletions.
19 changes: 3 additions & 16 deletions utils/get_manifest_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import logging
import os
import json
from swiftclient import ClientException
import pickle
from dotenv import load_dotenv
from fastapi.responses import JSONResponse

# Load .env file
load_dotenv()
container_name = os.getenv("CONTAINER_NAME")
# Connect to Swift
conn = get_swift_connection()

#config logger
logging.basicConfig(level=logging.INFO,handlers=[logging.StreamHandler()])
Expand All @@ -21,18 +18,15 @@

async def get_manifest_conn(slug:str,request: Request):
"""
#Retrieve file content from the specified Swift container by filename, and format JSON data.
# Access swift_session, swift_token, and swift_storage_url,redis from app state
Retrieve file content from the specified Swift container by filename, and format JSON data.
"""
#Access swift_session, swift_token, and swift_storage_url,redis from app state
swift_session = request.app.state.swift_session
swift_token = request.app.state.swift_token
swift_storage_url = request.app.state.swift_storage_url

if not swift_token or not swift_storage_url:
raise HTTPException(status_code=401, detail="Authentication is not complete or failed.")
"""

try:
#Access Swift and Redis objects from the app's state
redis = request.app.state.redis
Expand All @@ -41,12 +35,6 @@ async def get_manifest_conn(slug:str,request: Request):
#Check Redis cache
if (cached_profile := await redis.get(f"manifest_{slug}")) is not None:
return pickle.loads(cached_profile)

#Fetch from Swift storage
_,manifest = conn.get_object(container_name, manifest_name)
manifest_data = json.loads(manifest)

"""
file_url = f"{swift_storage_url}/{container_name}/{manifest_name}"
headers = {
"X-Auth-Token": swift_token
Expand All @@ -62,7 +50,6 @@ async def get_manifest_conn(slug:str,request: Request):
# If not valid JSON, raise an error
logger.error("Retrieved content is not valid JSON format.")
raise HTTPException(status_code=400, detail="The file content is not valid JSON format.")
"""
# Cache the manifest in Redis
logger.info(f"Caching manifest_{slug} in Redis.")
await redis.set(f"manifest_{slug}", pickle.dumps(manifest_data))
Expand Down
36 changes: 4 additions & 32 deletions utils/upload_manifest.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import os
from dotenv import load_dotenv
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import Request, UploadFile, HTTPException,File
from fastapi import Request, UploadFile, HTTPException
from utils.validator import Validator
import json
from utils import back_task
from swiftclient.exceptions import ClientException
from redis.asyncio import Redis
from contextlib import asynccontextmanager
import logging
from swift_config.swift_config import get_swift_connection
import io
from urllib.parse import urlparse,urlunparse
from urllib.parse import urlparse
import botocore

# Load .env file
load_dotenv()
container_name = os.getenv("CONTAINER_NAME")
# Connect to Swift
conn = get_swift_connection()

#config logger
logging.basicConfig(level=logging.INFO,handlers=[logging.StreamHandler()])
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -117,7 +111,7 @@ async def upload_manifest_backend(
for canvas_item in canvas_content:

canvas_id = "/".join(canvas_item['id'].split("/")[-2:])
canvas_name = f'{slug}/{canvas_id}/canvas.json'
#canvas_name = f'{slug}/{canvas_id}/canvas.json'
canvas_parsed_url = urlparse(canvas_item['id'])
target_parsed_url = urlparse(canvas_item['items'][0]['items'][0]['target'])
annotation_page_parsed_url = urlparse(canvas_item['items'][0]['id'])
Expand All @@ -131,32 +125,10 @@ async def upload_manifest_backend(
canvas_item['items'][0]['id'] = annotation_page_id
canvas_item['items'][0]['items'][0]['id'] = annotation_id
new_manifest_items.append(canvas_item)
updated_canvas_content = json.dumps(canvas_item)
"""
#upload canvas to swift
conn.put_object(
container_name,
canvas_name,
contents=updated_canvas_content,
content_type='application/json'
)
"""
#updated_canvas_content = json.dumps(canvas_item)
# Upload manifest to Swift
manifest['items'] = new_manifest_items
updated_manifest = json.dumps(manifest)

"""
conn.put_object(
container_name,
manifest_name,
contents=updated_manifest,
content_type='application/json'
)
"""
upload_url = f"{swift_storage_url}/{container_name}/{manifest_name}"
headers = {
"X-Auth-Token": swift_token,
Expand Down

0 comments on commit c0a0c28

Please sign in to comment.