forked from manictime/manictime-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.py
More file actions
374 lines (311 loc) · 10.9 KB
/
Copy pathschema.py
File metadata and controls
374 lines (311 loc) · 10.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
"""JSON schema validation for ManicTime API responses."""
import json
import logging
from typing import Dict, Any, Optional, List, Union
import jsonschema
from jsonschema import Draft7Validator, validators
from pathlib import Path
logger = logging.getLogger("manictime.schema")
# Default schema directory
SCHEMA_DIR = Path(__file__).parent / "schemas"
# Ensure schema directory exists
SCHEMA_DIR.mkdir(exist_ok=True)
def extend_with_default(validator_class):
"""
Extend validator class with default values
Based on jsonschema documentation example for handling default values
"""
validate_properties = validator_class.VALIDATORS["properties"]
def set_defaults(validator, properties, instance, schema):
for property_name, subschema in properties.items():
if "default" in subschema and property_name not in instance:
instance[property_name] = subschema["default"]
for error in validate_properties(validator, properties, instance, schema):
yield error
return validators.extend(validator_class, {"properties": set_defaults})
# Create validator with default value support
DefaultValidatingDraft7Validator = extend_with_default(Draft7Validator)
class SchemaValidator:
"""JSON schema validator for API responses"""
def __init__(self, schema_dir: Optional[Path] = None):
"""
Initialize schema validator
Args:
schema_dir: Directory containing schema files
"""
self.schema_dir = schema_dir or SCHEMA_DIR
self.schemas: Dict[str, Dict[str, Any]] = {}
self._load_schemas()
def _load_schemas(self) -> None:
"""Load all schema files from schema directory"""
if not self.schema_dir.exists():
logger.warning(f"Schema directory does not exist: {self.schema_dir}")
return
for schema_file in self.schema_dir.glob("*.json"):
try:
schema_name = schema_file.stem
with open(schema_file, "r") as f:
schema = json.load(f)
self.schemas[schema_name] = schema
logger.debug(f"Loaded schema: {schema_name}")
except (json.JSONDecodeError, IOError) as e:
logger.error(f"Error loading schema {schema_file}: {str(e)}")
def get_schema(self, schema_name: str) -> Optional[Dict[str, Any]]:
"""
Get schema by name
Args:
schema_name: Schema name without extension
Returns:
Schema definition or None if not found
"""
return self.schemas.get(schema_name)
def validate(self, data: Dict[str, Any], schema_name: str) -> List[str]:
"""
Validate data against schema
Args:
data: Data to validate
schema_name: Schema name
Returns:
List of validation error messages (empty if valid)
"""
schema = self.get_schema(schema_name)
if not schema:
logger.warning(f"Schema not found: {schema_name}")
return [f"Schema not found: {schema_name}"]
validator = DefaultValidatingDraft7Validator(schema)
errors = list(validator.iter_errors(data))
if not errors:
return []
# Format error messages
error_messages = []
for error in errors:
path = ".".join(str(p) for p in error.path) if error.path else "root"
message = f"{path}: {error.message}"
error_messages.append(message)
return error_messages
def validate_and_transform(self,
data: Dict[str, Any],
schema_name: str) -> Dict[str, Any]:
"""
Validate data and apply schema transformations (like defaults)
Args:
data: Data to validate
schema_name: Schema name
Returns:
Validated and transformed data
Raises:
jsonschema.exceptions.ValidationError: If validation fails
"""
schema = self.get_schema(schema_name)
if not schema:
raise ValueError(f"Schema not found: {schema_name}")
# Make a copy to avoid modifying original
data_copy = json.loads(json.dumps(data))
# Use validator with default support
validator = DefaultValidatingDraft7Validator(schema)
validator.validate(data_copy)
return data_copy
def add_schema(self, schema_name: str, schema: Dict[str, Any],
save: bool = False) -> None:
"""
Add a new schema
Args:
schema_name: Schema name
schema: Schema definition
save: Whether to save to file
"""
self.schemas[schema_name] = schema
if save:
schema_file = self.schema_dir / f"{schema_name}.json"
try:
with open(schema_file, "w") as f:
json.dump(schema, f, indent=2)
logger.debug(f"Saved schema: {schema_name}")
except IOError as e:
logger.error(f"Error saving schema {schema_file}: {str(e)}")
def remove_schema(self, schema_name: str,
delete_file: bool = False) -> bool:
"""
Remove a schema
Args:
schema_name: Schema name
delete_file: Whether to delete schema file
Returns:
True if schema was removed, False otherwise
"""
if schema_name not in self.schemas:
return False
del self.schemas[schema_name]
if delete_file:
schema_file = self.schema_dir / f"{schema_name}.json"
try:
if schema_file.exists():
schema_file.unlink()
logger.debug(f"Deleted schema file: {schema_name}")
except IOError as e:
logger.error(f"Error deleting schema file {schema_file}: {str(e)}")
return True
# Create default schemas
activity_schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Activity",
"description": "Activity data from ManicTime API",
"type": "object",
"properties": {
"start": {
"type": "string",
"format": "date-time",
"description": "Activity start time"
},
"end": {
"type": "string",
"format": "date-time",
"description": "Activity end time"
},
"application": {
"type": "string",
"description": "Application name"
},
"title": {
"type": "string",
"description": "Activity title"
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"description": "Tags assigned to the activity"
},
"notes": {
"type": ["string", "null"],
"default": None,
"description": "Activity notes"
}
},
"required": ["start", "end", "application", "title"]
}
timeline_schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Timeline",
"description": "Timeline data from ManicTime API",
"type": "object",
"properties": {
"timelineId": {
"type": "string",
"description": "Timeline ID"
},
"name": {
"type": "string",
"description": "Timeline name"
},
"description": {
"type": ["string", "null"],
"default": None,
"description": "Timeline description"
}
},
"required": ["timelineId", "name"]
}
tag_combination_schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "TagCombination",
"description": "Tag combination data from ManicTime API",
"type": "object",
"properties": {
"combinationId": {
"type": "string",
"description": "Tag combination ID"
},
"name": {
"type": "string",
"description": "Tag combination name"
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"description": "List of tags in the combination"
},
"description": {
"type": ["string", "null"],
"default": None,
"description": "Tag combination description"
},
"color": {
"type": ["string", "null"],
"default": None,
"description": "Tag combination color (hex code)"
}
},
"required": ["combinationId", "name", "tags"]
}
webhook_schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Webhook",
"description": "Webhook data from ManicTime API",
"type": "object",
"properties": {
"webhookId": {
"type": "string",
"description": "Webhook ID"
},
"url": {
"type": "string",
"description": "Webhook URL"
},
"events": {
"type": "array",
"items": {
"type": "string"
},
"description": "List of event types"
},
"description": {
"type": ["string", "null"],
"default": None,
"description": "Webhook description"
},
"secret": {
"type": ["string", "null"],
"default": None,
"description": "Webhook secret (for signature verification)"
}
},
"required": ["webhookId", "url", "events"]
}
# Default schema validator instance
default_validator = SchemaValidator()
# Initialize default schemas
if not (SCHEMA_DIR / "activity.json").exists():
default_validator.add_schema("activity", activity_schema, save=True)
if not (SCHEMA_DIR / "timeline.json").exists():
default_validator.add_schema("timeline", timeline_schema, save=True)
if not (SCHEMA_DIR / "tag_combination.json").exists():
default_validator.add_schema("tag_combination", tag_combination_schema, save=True)
if not (SCHEMA_DIR / "webhook.json").exists():
default_validator.add_schema("webhook", webhook_schema, save=True)
def validate_response(data: Dict[str, Any], schema_name: str) -> List[str]:
"""
Validate API response against schema
Args:
data: Response data
schema_name: Schema name
Returns:
List of validation error messages (empty if valid)
"""
return default_validator.validate(data, schema_name)
def validate_and_transform(data: Dict[str, Any], schema_name: str) -> Dict[str, Any]:
"""
Validate and transform data according to schema
Args:
data: Data to validate
schema_name: Schema name
Returns:
Validated and transformed data
Raises:
jsonschema.exceptions.ValidationError: If validation fails
"""
return default_validator.validate_and_transform(data, schema_name)