-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththemis_client.py
More file actions
428 lines (362 loc) · 14.9 KB
/
themis_client.py
File metadata and controls
428 lines (362 loc) · 14.9 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: themis_client.py ║
Version: 0.0.47 ║
Last Modified: 2026-04-15 18:43:50 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟢 PRODUCTION-READY ║
• Quality Score: 100.0/100 ║
• Total Lines: 431 ║
• Open Issues: TODOs: 0, Stubs: 0 ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ✅ Production Ready ║
╚═════════════════════════════════════════════════════════════════════╝
"""
"""
ThemisDB Client Wrapper.
This module provides a simplified interface to interact with ThemisDB
for the Coding Platform application.
"""
import requests
from typing import List, Dict, Any, Optional
from datetime import datetime
import uuid
from models import CodeSnippet, Project, Documentation, ScrapingJob
class ThemisDBClient:
"""
Client for interacting with ThemisDB.
This is a wrapper around the ThemisDB HTTP API that provides
convenient methods for code snippet management.
"""
def __init__(self, host: str = "localhost", port: int = 8080, api_token: Optional[str] = None):
"""
Initialize ThemisDB client.
Args:
host: ThemisDB server hostname
port: ThemisDB server port
api_token: Optional API token for authentication
"""
self.base_url = f"http://{host}:{port}/api/v1"
self.session = requests.Session()
if api_token:
self.session.headers.update({"Authorization": f"Bearer {api_token}"})
def health_check(self) -> bool:
"""Check if ThemisDB server is healthy."""
try:
response = self.session.get(f"http://{self.base_url.split('/api')[0]}/health", timeout=5)
return response.status_code == 200
except requests.RequestException:
return False
# Snippet Methods
def create_snippet(self, snippet: CodeSnippet) -> CodeSnippet:
"""
Create a new code snippet.
Args:
snippet: CodeSnippet object to create
Returns:
Created snippet with assigned ID
"""
if not snippet.id:
snippet.id = str(uuid.uuid4())
if not snippet.created_at:
snippet.created_at = datetime.now()
snippet.updated_at = datetime.now()
response = self.session.post(
f"{self.base_url}/snippets",
json=snippet.to_dict()
)
response.raise_for_status()
return CodeSnippet.from_dict(response.json()["data"])
def get_snippet(self, snippet_id: str) -> Optional[CodeSnippet]:
"""Get a snippet by ID."""
try:
response = self.session.get(f"{self.base_url}/snippets/{snippet_id}")
response.raise_for_status()
return CodeSnippet.from_dict(response.json()["data"])
except requests.RequestException:
return None
def update_snippet(self, snippet: CodeSnippet) -> CodeSnippet:
"""Update an existing snippet."""
snippet.updated_at = datetime.now()
response = self.session.put(
f"{self.base_url}/snippets/{snippet.id}",
json=snippet.to_dict()
)
response.raise_for_status()
return CodeSnippet.from_dict(response.json()["data"])
def delete_snippet(self, snippet_id: str) -> bool:
"""Delete a snippet by ID."""
try:
response = self.session.delete(f"{self.base_url}/snippets/{snippet_id}")
response.raise_for_status()
return True
except requests.RequestException:
return False
def list_snippets(
self,
language: Optional[str] = None,
framework: Optional[str] = None,
tags: Optional[List[str]] = None,
limit: int = 100,
offset: int = 0
) -> List[CodeSnippet]:
"""
List snippets with optional filters.
Args:
language: Filter by programming language
framework: Filter by framework
tags: Filter by tags
limit: Maximum number of results
offset: Offset for pagination
Returns:
List of matching snippets
"""
params = {
"limit": limit,
"offset": offset
}
if language:
params["language"] = language
if framework:
params["framework"] = framework
if tags:
params["tags"] = ",".join(tags)
response = self.session.get(f"{self.base_url}/snippets", params=params)
response.raise_for_status()
data = response.json()["data"]
return [CodeSnippet.from_dict(item) for item in data]
def search_snippets(
self,
query: str,
language: Optional[str] = None,
limit: int = 10
) -> List[Dict[str, Any]]:
"""
Semantic search for snippets.
Args:
query: Natural language search query
language: Optional language filter
limit: Maximum number of results
Returns:
List of snippets with similarity scores
"""
response = self.session.post(
f"{self.base_url}/snippets/search",
json={
"query": query,
"language": language,
"limit": limit
}
)
response.raise_for_status()
return response.json()["data"]
def find_similar(
self,
code: str,
language: str,
limit: int = 5
) -> List[Dict[str, Any]]:
"""
Find similar code snippets.
Args:
code: Code to find similar snippets for
language: Programming language
limit: Maximum number of results
Returns:
List of similar snippets with similarity scores
"""
response = self.session.post(
f"{self.base_url}/snippets/similar",
json={
"code": code,
"language": language,
"limit": limit
}
)
response.raise_for_status()
return response.json()["data"]
# Project Methods
def create_project(self, project: Project) -> Project:
"""Create a new project."""
if not project.id:
project.id = str(uuid.uuid4())
if not project.created_at:
project.created_at = datetime.now()
project.updated_at = datetime.now()
response = self.session.post(
f"{self.base_url}/projects",
json=project.to_dict()
)
response.raise_for_status()
return project
def get_project(self, project_id: str) -> Optional[Project]:
"""Get a project by ID."""
try:
response = self.session.get(f"{self.base_url}/projects/{project_id}")
response.raise_for_status()
return Project(**response.json()["data"])
except requests.RequestException:
return None
def list_projects(
self,
language: Optional[str] = None,
limit: int = 50
) -> List[Project]:
"""List projects with optional filters."""
params = {"limit": limit}
if language:
params["language"] = language
response = self.session.get(f"{self.base_url}/projects", params=params)
response.raise_for_status()
data = response.json()["data"]
return [Project(**item) for item in data]
# Documentation Methods
def create_documentation(self, doc: Documentation) -> Documentation:
"""Create new documentation."""
if not doc.id:
doc.id = str(uuid.uuid4())
if not doc.created_at:
doc.created_at = datetime.now()
doc.updated_at = datetime.now()
response = self.session.post(
f"{self.base_url}/documentation",
json=doc.to_dict()
)
response.raise_for_status()
return doc
def get_documentation(self, doc_id: str) -> Optional[Documentation]:
"""Get documentation by ID."""
try:
response = self.session.get(f"{self.base_url}/documentation/{doc_id}")
response.raise_for_status()
return Documentation(**response.json()["data"])
except requests.RequestException:
return None
# Scraping Job Methods
def create_scraping_job(self, job: ScrapingJob) -> ScrapingJob:
"""Create a new scraping job."""
if not job.id:
job.id = str(uuid.uuid4())
response = self.session.post(
f"{self.base_url}/scraping/jobs",
json=job.to_dict()
)
response.raise_for_status()
return job
def get_scraping_job(self, job_id: str) -> Optional[ScrapingJob]:
"""Get scraping job status."""
try:
response = self.session.get(f"{self.base_url}/scraping/jobs/{job_id}")
response.raise_for_status()
data = response.json()["data"]
# Convert back to ScrapingJob (simplified)
return data
except requests.RequestException:
return None
def list_scraping_jobs(
self,
status: Optional[str] = None,
limit: int = 50
) -> List[Dict[str, Any]]:
"""List scraping jobs."""
params = {"limit": limit}
if status:
params["status"] = status
response = self.session.get(f"{self.base_url}/scraping/jobs", params=params)
response.raise_for_status()
return response.json()["data"]
def cancel_scraping_job(self, job_id: str) -> bool:
"""Cancel a running scraping job."""
try:
response = self.session.delete(f"{self.base_url}/scraping/jobs/{job_id}")
response.raise_for_status()
return True
except requests.RequestException:
return False
# Statistics Methods
def get_statistics(self) -> Dict[str, Any]:
"""Get platform statistics."""
try:
response = self.session.get(f"{self.base_url}/statistics")
response.raise_for_status()
return response.json()["data"]
except requests.RequestException:
return {
"total_snippets": 0,
"total_projects": 0,
"total_docs": 0,
"languages": {}
}
# Mock implementation for development/testing without actual ThemisDB server
class MockThemisDBClient(ThemisDBClient):
"""Mock client for testing without ThemisDB server."""
def __init__(self, *args, **kwargs):
# Don't call parent __init__ to avoid creating session
self.snippets = {}
self.projects = {}
self.docs = {}
self.jobs = {}
def health_check(self) -> bool:
return True
def create_snippet(self, snippet: CodeSnippet) -> CodeSnippet:
if not snippet.id:
snippet.id = str(uuid.uuid4())
if not snippet.created_at:
snippet.created_at = datetime.now()
snippet.updated_at = datetime.now()
self.snippets[snippet.id] = snippet
return snippet
def get_snippet(self, snippet_id: str) -> Optional[CodeSnippet]:
return self.snippets.get(snippet_id)
def update_snippet(self, snippet: CodeSnippet) -> CodeSnippet:
snippet.updated_at = datetime.now()
self.snippets[snippet.id] = snippet
return snippet
def delete_snippet(self, snippet_id: str) -> bool:
if snippet_id in self.snippets:
del self.snippets[snippet_id]
return True
return False
def list_snippets(self, language=None, framework=None, tags=None, limit=100, offset=0):
snippets = list(self.snippets.values())
if language:
snippets = [s for s in snippets if s.language == language]
if framework:
snippets = [s for s in snippets if s.framework == framework]
if tags:
snippets = [s for s in snippets if any(t in s.tags for t in tags)]
return snippets[offset:offset + limit]
def search_snippets(self, query, language=None, limit=10):
# Simple keyword search for mock
snippets = list(self.snippets.values())
if language:
snippets = [s for s in snippets if s.language == language]
query_lower = query.lower()
results = []
for snippet in snippets:
if (query_lower in snippet.title.lower() or
query_lower in snippet.description.lower() or
query_lower in snippet.code.lower()):
results.append({
"snippet": snippet,
"score": 0.85 # Mock similarity score
})
return results[:limit]
def find_similar(self, code, language, limit=5):
# Mock implementation
snippets = [s for s in self.snippets.values() if s.language == language]
return [{"snippet": s, "score": 0.75} for s in snippets[:limit]]
def get_statistics(self):
languages = {}
for snippet in self.snippets.values():
languages[snippet.language] = languages.get(snippet.language, 0) + 1
return {
"total_snippets": len(self.snippets),
"total_projects": len(self.projects),
"total_docs": len(self.docs),
"languages": languages
}