Skip to content

Commit 116105c

Browse files
committed
Refactor
1 parent 05f1bb1 commit 116105c

File tree

4 files changed

+49
-28
lines changed

4 files changed

+49
-28
lines changed

app.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
from flask import Flask, jsonify, render_template, request
32
from waitress import serve
43
from lib import ValidationService
@@ -52,9 +51,7 @@ def get_profiles():
5251

5352
@app.route('/<profile>/validate', methods=['GET', 'POST'])
5453
def validate(profile):
55-
try:
56-
service.profile(profile)
57-
except Exception:
54+
if not service.has(profile):
5855
raise NotFound(f"Profile not found: {profile}")
5956

6057
if request.method == 'GET':

lib/service.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ def __init__(self, **config):
2626

2727
self.validator = Validator(**config)
2828

29-
def profiles(self):
30-
return self.validator.profiles_metadata()
29+
def profiles(self) -> list:
30+
return list(self.validator.profiles.values())
3131

32-
def profile(self, id):
33-
return self.validator.profile(id)
32+
def has(self, profile) -> bool:
33+
return profile in self.validator.profiles
3434

3535
def validate(self, profile, data=None, url=None, file=None):
3636

lib/validate/validator.py

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,58 @@
88
schema = json.load((Path(__file__).parent / 'profiles-schema.json').open())
99

1010

11+
class Check(object):
12+
def __init__(self, config):
13+
if type(config) == str:
14+
match config:
15+
case "json":
16+
self.method = parseJSON
17+
case "xml":
18+
self.method = parseXML
19+
case _:
20+
# TODO: allow to reference another profile
21+
raise Exception(f"Unknown check: {config}")
22+
elif "schema" in config and "language" in config:
23+
match config["language"]:
24+
case "xsd":
25+
pass # TODO: load as local file or from URL with cache
26+
case "jsonschema":
27+
pass # TODO
28+
case _:
29+
raise Exception(f"Unsupported schema language: {config['language']}")
30+
31+
else:
32+
raise Exception(f"Unkown check: {json.dumps(config)}")
33+
34+
def execute(self, data):
35+
self.method(data)
36+
37+
1138
class Validator(object):
1239
def __init__(self, profiles, **config):
1340
validateJSON(profiles, schema)
1441

15-
# TODO: check if id is unique
16-
self.profiles = dict([(p["id"], p) for p in profiles])
17-
# TODO: compile checks of profiles
42+
checks = {p["id"]: p.get("checks", []) for p in profiles}
43+
if len(checks) != len(profiles):
44+
raise ValueError("Profiles must have unique ids")
1845

19-
# self.reports = config.get('reports', None)
46+
self.profiles = {}
47+
for p in profiles:
48+
id = p["id"]
2049

21-
def profile(self, id):
22-
"Returns public metadata of a profile or None."
23-
p = self.profiles[id]
24-
fields = ['id', 'title', 'description', 'url']
25-
return {key: p[key] for key in fields if key in p}
50+
# TODO: support reference to profile as check
51+
checks[id] = [Check(c) for c in checks[id]]
2652

27-
def profiles_metadata(self):
28-
"List of profiles reduced to their their public metadata"
29-
return [self.profile(id) for id in self.profiles]
53+
about = ['id', 'title', 'description', 'url']
54+
self.profiles[id] = {key: p[key] for key in about if p.get(key, False)}
55+
56+
self.checks = checks
57+
58+
def profile(self, id) -> dict:
59+
return self.profiles[id]
3060

3161
def execute(self, profile, data=None, file=None):
3262
if file:
3363
data = Path(file).read_bytes()
34-
35-
checks = self.profiles[profile]['checks']
36-
for check in checks:
37-
if check == "json":
38-
parseJSON(data)
39-
elif check == "xml":
40-
parseXML(data)
64+
for check in self.checks[profile]:
65+
check.execute(data)

tests/test_api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import tempfile
33
from pathlib import Path
44
import io
5-
import json
65
from app import app, init
76

87

0 commit comments

Comments
 (0)