File size: 10,353 Bytes
90c6b42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""
Airtable Service for ATOM Platform
Provides comprehensive Airtable database and spreadsheet integration functionality
"""

import logging
import os
from typing import Any, Dict, List, Optional
from datetime import datetime, timezone
import httpx
from fastapi import HTTPException

logger = logging.getLogger(__name__)

from core.integration_service import IntegrationService

class AirtableService(IntegrationService):
    def __init__(self, tenant_id: str = "default", config: Dict[str, Any] = None):
        if config is None:
            config = {}
        super().__init__(tenant_id=tenant_id, config=config)
        self.api_key = config.get("api_key") or os.getenv("AIRTABLE_API_KEY")
        self.base_url = "https://api.airtable.com/v0"
        self.client = httpx.AsyncClient(timeout=30.0)

    async def close(self):
        """Close the HTTP client connection"""
        await self.client.aclose()

    def _get_headers(self, token: Optional[str] = None) -> Dict[str, str]:
        """Get headers for API requests"""
        api_key = token or self.api_key
        return {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        }

    async def get_bases(self, token: Optional[str] = None) -> List[Dict[str, Any]]:
        """List all bases accessible to the user"""
        try:
            headers = self._get_headers(token)
            response = await self.client.get(f"{self.base_url}/meta/bases", headers=headers)
            response.raise_for_status()
            return response.json().get("bases", [])
        except Exception as e:
            logger.error(f"Failed to list Airtable bases: {e}")
            return []

    async def get_tables(self, base_id: str, token: Optional[str] = None) -> List[Dict[str, Any]]:
        """List all tables in a base"""
        try:
            headers = self._get_headers(token)
            response = await self.client.get(f"{self.base_url}/meta/bases/{base_id}/tables", headers=headers)
            response.raise_for_status()
            return response.json().get("tables", [])
        except Exception as e:
            logger.error(f"Failed to list Airtable tables for base {base_id}: {e}")
            return []

    async def list_records(
        self,
        base_id: str,
        table_name: str,
        max_records: int = 100,
        view: str = None,
        filter_formula: str = None
    ) -> List[Dict[str, Any]]:
        """List records from a table"""
        try:
            if not self.api_key:
                raise HTTPException(status_code=401, detail="Not authenticated")
            
            headers = self._get_headers()
            params = {"maxRecords": max_records}
            
            if view:
                params["view"] = view
            if filter_formula:
                params["filterByFormula"] = filter_formula
            
            response = await self.client.get(
                f"{self.base_url}/{base_id}/{table_name}",
                headers=headers,
                params=params
            )
            response.raise_for_status()
            
            data = response.json()
            return data.get("records", [])
        except httpx.HTTPError as e:
            logger.error(f"Failed to list records: {e}")
            raise HTTPException(
                status_code=400,
                detail=f"Failed to list records: {str(e)}"
            )

    async def get_record(
        self,
        base_id: str,
        table_name: str,
        record_id: str
    ) -> Dict[str, Any]:
        """Get a specific record"""
        try:
            if not self.api_key:
                raise HTTPException(status_code=401, detail="Not authenticated")
            
            headers = self._get_headers()
            
            response = await self.client.get(
                f"{self.base_url}/{base_id}/{table_name}/{record_id}",
                headers=headers
            )
            response.raise_for_status()
            
            return response.json()
        except httpx.HTTPError as e:
            logger.error(f"Failed to get record: {e}")
            raise HTTPException(
                status_code=400,
                detail=f"Failed to get record: {str(e)}"
            )

    async def create_record(
        self,
        base_id: str,
        table_name: str,
        fields: Dict[str, Any]
    ) -> Dict[str, Any]:
        """Create a new record"""
        try:
            if not self.api_key:
                raise HTTPException(status_code=401, detail="Not authenticated")
            
            headers = self._get_headers()
            payload = {"fields": fields}
            
            response = await self.client.post(
                f"{self.base_url}/{base_id}/{table_name}",
                headers=headers,
                json=payload
            )
            response.raise_for_status()
            
            return response.json()
        except httpx.HTTPError as e:
            logger.error(f"Failed to create record: {e}")
            raise HTTPException(
                status_code=400,
                detail=f"Failed to create record: {str(e)}"
            )

    async def update_record(
        self,
        base_id: str,
        table_name: str,
        record_id: str,
        fields: Dict[str, Any]
    ) -> Dict[str, Any]:
        """Update a record"""
        try:
            if not self.api_key:
                raise HTTPException(status_code=401, detail="Not authenticated")
            
            headers = self._get_headers()
            payload = {"fields": fields}
            
            response = await self.client.patch(
                f"{self.base_url}/{base_id}/{table_name}/{record_id}",
                headers=headers,
                json=payload
            )
            response.raise_for_status()
            
            return response.json()
        except httpx.HTTPError as e:
            logger.error(f"Failed to update record: {e}")
            raise HTTPException(
                status_code=400,
                detail=f"Failed to update record: {str(e)}"
            )

    async def delete_record(
        self,
        base_id: str,
        table_name: str,
        record_id: str
    ) -> Dict[str, Any]:
        """Delete a record"""
        try:
            if not self.api_key:
                raise HTTPException(status_code=401, detail="Not authenticated")
            
            headers = self._get_headers()
            
            response = await self.client.delete(
                f"{self.base_url}/{base_id}/{table_name}/{record_id}",
                headers=headers
            )
            response.raise_for_status()
            
            return response.json()
        except httpx.HTTPError as e:
            logger.error(f"Failed to delete record: {e}")
            raise HTTPException(
                status_code=400,
                detail=f"Failed to delete record: {str(e)}"
            )

    async def health_check(self) -> Dict[str, Any]:
        """Health check for Airtable service"""
        try:
            return {
                "ok": True,
                "status": "healthy",
                "service": "airtable",
                "timestamp": datetime.now(timezone.utc).isoformat(),
                "version": "1.0.0",
            }
        except Exception as e:
            return {
                "ok": False,
                "status": "unhealthy",
                "service": "airtable",
                "error": str(e),
                "timestamp": datetime.now(timezone.utc).isoformat(),
            }

    async def sync_to_postgres_cache(self, workspace_id: str, base_id: str = None) -> Dict[str, Any]:
        """Sync Airtable analytics to PostgreSQL IntegrationMetric table."""
        try:
            from core.database import SessionLocal
            from core.models import IntegrationMetric
            
            # Note: Would need base_id and table name to count records
            # For now, just track basic connectivity
            record_count = 0
            
            db = SessionLocal()
            metrics_synced = 0
            try:
                metrics_to_save = [
                    ("airtable_connected", 1, "boolean"),
                ]
                
                for key, value, unit in metrics_to_save:
                    existing = db.query(IntegrationMetric).filter_by(
                        tenant_id=workspace_id,
                        integration_type="airtable",
                        metric_key=key
                    ).first()
                    
                    if existing:
                        existing.value = float(value)
                        existing.last_synced_at = datetime.now(timezone.utc)
                    else:
                        metric = IntegrationMetric(
                            tenant_id=workspace_id,
                            integration_type="airtable",
                            metric_key=key,
                            value=float(value),
                            unit=unit
                        )
                        db.add(metric)
                    metrics_synced += 1
                
                db.commit()
                logger.info(f"Synced {metrics_synced} Airtable metrics to PostgreSQL cache for workspace {workspace_id}")
            except Exception as e:
                logger.error(f"Error saving Airtable metrics to Postgres: {e}")
                db.rollback()
                return {"success": False, "error": str(e)}
            finally:
                db.close()
                
            return {"success": True, "metrics_synced": metrics_synced}
        except Exception as e:
            logger.error(f"Airtable PostgreSQL cache sync failed: {e}")
            return {"success": False, "error": str(e)}

    async def full_sync(self, workspace_id: str, base_id: str = None) -> Dict[str, Any]:
        """Trigger full dual-pipeline sync for Airtable"""
        cache_result = await self.sync_to_postgres_cache(workspace_id, base_id)
        
        return {
            "success": True,
            "workspace_id": workspace_id,
            "postgres_cache": cache_result,
            "timestamp": datetime.now(timezone.utc).isoformat()
        }