|
| 1 | +from dataclasses import dataclass, replace |
| 2 | +from typing import Any, Dict, Protocol, List |
| 3 | +import httpx |
| 4 | +from fastapi import HTTPException |
| 5 | + |
| 6 | + |
| 7 | +class ContentClient(Protocol): |
| 8 | + async def fetch( |
| 9 | + self, collection_type: str, filters: Dict[str, str] |
| 10 | + ) -> List[Dict[str, Any]]: |
| 11 | + ... |
| 12 | + |
| 13 | + |
| 14 | +class PayloadClient: |
| 15 | + url: str |
| 16 | + api_key: str |
| 17 | + |
| 18 | + def __init__(self, url: str, api_key: str): |
| 19 | + self.url = url |
| 20 | + self.api_key = api_key |
| 21 | + |
| 22 | + async def fetch( |
| 23 | + self, collection_type: str, filters: Dict[str, str] |
| 24 | + ) -> List[Dict[str, Any]]: |
| 25 | + """ |
| 26 | + Fetch documents from Payload CMS with optional filtering. |
| 27 | +
|
| 28 | + Args: |
| 29 | + collection_type: The Payload collection name |
| 30 | + filters: Dictionary of property filters |
| 31 | +
|
| 32 | + Returns: |
| 33 | + Response from Payload CMS API |
| 34 | + """ |
| 35 | + url = f"{self.url}/api/{collection_type}" |
| 36 | + |
| 37 | + # Build query parameters for Payload's where clause |
| 38 | + where_conditions = {} |
| 39 | + for prop, value in filters.items(): |
| 40 | + where_conditions[prop] = {"equals": value} |
| 41 | + |
| 42 | + params = {} |
| 43 | + if where_conditions: |
| 44 | + # Payload uses a 'where' parameter with JSON |
| 45 | + import json |
| 46 | + |
| 47 | + params["where"] = json.dumps(where_conditions) |
| 48 | + |
| 49 | + headers = {} |
| 50 | + if self.api_key: |
| 51 | + headers["Authorization"] = f"Bearer {self.api_key}" |
| 52 | + |
| 53 | + async with httpx.AsyncClient() as client: |
| 54 | + try: |
| 55 | + response = await client.get( |
| 56 | + url, params=params, headers=headers, timeout=30.0 |
| 57 | + ) |
| 58 | + response.raise_for_status() |
| 59 | + return response.json() |
| 60 | + except httpx.HTTPError as e: |
| 61 | + raise HTTPException( |
| 62 | + status_code=500, detail=f"Error fetching from Payload CMS: {str(e)}" |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +class CacheClient: |
| 67 | + cache_dir: str |
| 68 | + |
| 69 | + def __init__(self, cache_dir: str): |
| 70 | + self.cache_dir = cache_dir |
| 71 | + |
| 72 | + async def fetch( |
| 73 | + self, collection_type: str, filters: Dict[str, str] |
| 74 | + ) -> List[Dict[str, Any]]: |
| 75 | + raise NotImplementedError() |
| 76 | + |
| 77 | + def update( |
| 78 | + self, |
| 79 | + collection_type: str, |
| 80 | + documents: List[Dict[str, Any]], |
| 81 | + replace_all: bool = False, |
| 82 | + ): |
| 83 | + # used for populating the cache |
| 84 | + raise NotImplementedError() |
| 85 | + |
| 86 | + |
| 87 | +# Turns out none of this is needed. Will delete... |
| 88 | +# class WrongNumberOfMatches(Exception): |
| 89 | +# pass |
| 90 | +# |
| 91 | +# class TooManyMatches(WrongNumberOfMatches): |
| 92 | +# pass |
| 93 | +# |
| 94 | +# class NoMatches(WrongNumberOfMatches): |
| 95 | +# pass |
| 96 | +# |
| 97 | +# @dataclass(frozen=True) |
| 98 | +# class Query: |
| 99 | +# client : ContentClient |
| 100 | +# content_type: str |
| 101 | +# filters : Dict[str, str] |
| 102 | +# |
| 103 | +# def filter_by(self, **filters): |
| 104 | +# new_filters = dict(self.filters, **filters) |
| 105 | +# return replace(self, filters=new_filters) |
| 106 | +# |
| 107 | +# async def one(self): |
| 108 | +# result = await self.client.fetch(self.content_type, self.filters) |
| 109 | +# if len(result) == 1: |
| 110 | +# return result[0] |
| 111 | +# elif len(result) == 0: |
| 112 | +# raise NoMatches() |
| 113 | +# else: |
| 114 | +# raise TooManyMatches() |
| 115 | +# |
| 116 | +# async def all(self): |
| 117 | +# return await self.client.fetch(self.content_type, self.filters) |
| 118 | +# |
| 119 | +# async def one_or_none(self): |
| 120 | +# result = await self.client.fetch(self.content_type, self.filters) |
| 121 | +# if len(result) == 1: |
| 122 | +# return result[0] |
| 123 | +# elif len(result) == 0: |
| 124 | +# return None |
| 125 | +# else: |
| 126 | +# raise TooManyMatches() |
| 127 | +# |
| 128 | +# class CMS: |
| 129 | +# """ |
| 130 | +# A slim wrapper around a client which provides some sqlalchemy api-like query behavior where we can |
| 131 | +# """ |
| 132 | +# |
| 133 | +# client : ContentClient |
| 134 | +# def __init__(self,client : ContentClient): |
| 135 | +# self.client = client |
| 136 | +# |
| 137 | +# def query(self, content_type: str): |
| 138 | +# return |
0 commit comments