|
| 1 | +import os |
| 2 | +import redis |
| 3 | +import logging |
| 4 | +from typing import Optional, Dict |
| 5 | + |
| 6 | +# Configure logging |
| 7 | +logging.basicConfig(level=logging.INFO) |
| 8 | + |
| 9 | +def get_redis_connection() -> redis.Redis: |
| 10 | + """ |
| 11 | + Establishes a connection to Redis using environment variables. |
| 12 | +
|
| 13 | + Returns: |
| 14 | + redis.Redis: A Redis connection object. |
| 15 | + """ |
| 16 | + try: |
| 17 | + return redis.Redis( |
| 18 | + host = os.getenv('FALKORDB_HOST'), |
| 19 | + port = os.getenv('FALKORDB_PORT'), |
| 20 | + username = os.getenv('FALKORDB_USERNAME'), |
| 21 | + password = os.getenv('FALKORDB_PASSWORD'), |
| 22 | + decode_responses = True # To ensure string responses |
| 23 | + ) |
| 24 | + except Exception as e: |
| 25 | + logging.error(f"Error connecting to Redis: {e}") |
| 26 | + raise |
| 27 | + |
| 28 | + |
| 29 | +def save_repo_info(repo_name: str, repo_url: str) -> None: |
| 30 | + """ |
| 31 | + Saves repository information (URL) to Redis under a hash named {repo_name}_info. |
| 32 | +
|
| 33 | + Args: |
| 34 | + repo_name (str): The name of the repository. |
| 35 | + repo_url (str): The URL of the repository. |
| 36 | + """ |
| 37 | + |
| 38 | + try: |
| 39 | + r = get_redis_connection() |
| 40 | + key = f"{{ {repo_name} }}_info" # Safely format the key |
| 41 | + |
| 42 | + # Save the repository URL |
| 43 | + r.hset(key, 'repo_url', repo_url) |
| 44 | + logging.info(f"Repository info saved for {repo_name}") |
| 45 | + |
| 46 | + except Exception as e: |
| 47 | + logging.error(f"Error saving repo info for '{repo_name}': {e}") |
| 48 | + raise |
| 49 | + |
| 50 | +def get_repo_info(repo_name: str) -> Optional[Dict[str, str]]: |
| 51 | + """ |
| 52 | + Retrieves repository information from Redis. |
| 53 | +
|
| 54 | + Args: |
| 55 | + repo_name (str): The name of the repository. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + Optional[Dict[str, str]]: A dictionary of repository information, or None if not found. |
| 59 | + """ |
| 60 | + try: |
| 61 | + r = get_redis_connection() |
| 62 | + key = f"{{{repo_name}}}_info" |
| 63 | + |
| 64 | + # Retrieve all information about the repository |
| 65 | + repo_info = r.hgetall(key) |
| 66 | + if not repo_info: |
| 67 | + logging.warning(f"No repository info found for {repo_name}") |
| 68 | + return None |
| 69 | + |
| 70 | + logging.info(f"Repository info retrieved for {repo_name}") |
| 71 | + return repo_info |
| 72 | + |
| 73 | + except Exception as e: |
| 74 | + logging.error(f"Error retrieving repo info for '{repo_name}': {e}") |
| 75 | + raise |
| 76 | + |
0 commit comments