-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite: check classes and report classes
- Loading branch information
1 parent
c38b434
commit b049fe6
Showing
15 changed files
with
374 additions
and
63 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import Union, List | ||
|
||
from app.check.report import CheckReport | ||
|
||
|
||
class Check: | ||
def execute(self): | ||
raise NotImplemented | ||
|
||
@property | ||
def success(self) -> bool: | ||
raise NotImplemented | ||
|
||
@property | ||
def report(self) -> Union[CheckReport, List[CheckReport]]: | ||
raise NotImplemented |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from typing import List, Union | ||
from urllib.parse import urlparse, ParseResult | ||
|
||
import requests | ||
|
||
from app.check.check import Check | ||
from app.check.report import CheckReport | ||
|
||
|
||
class HTTPCheckInstance: | ||
definition: str | ||
method: str | ||
url: ParseResult | ||
status_code: int | ||
report: CheckReport | ||
|
||
def __init__(self, definition: str): | ||
self.definition = definition | ||
self.report = CheckReport() | ||
self.report.summary = definition | ||
|
||
def execute(self): | ||
segments = self.definition.split('|') | ||
|
||
self.status_code = 200 | ||
self.method = 'get' | ||
|
||
self.url = urlparse(segments[0]) | ||
|
||
try: | ||
status = segments[1] | ||
self.status_code = int(status) | ||
except IndexError: | ||
pass | ||
except ValueError: | ||
self.report.success = False | ||
self.report.errors.append(f'expected status code to be integer, got {segments[1]}') | ||
return | ||
|
||
try: | ||
method = segments[2] | ||
self.method = method.lower() | ||
except IndexError: | ||
pass | ||
|
||
requester = getattr(requests, self.method, None) | ||
|
||
if not requester: | ||
self.report.success = False | ||
self.report.errors.append(f'method "{self.method.upper()}" is not a valid HTTP method') | ||
return | ||
|
||
try: | ||
r = requester(self.url.geturl()) | ||
|
||
if r.status_code != self.status_code: | ||
self.report.success = False | ||
self.report.errors.append( | ||
f"expected HTTP Status {self.status_code} but got {r.status_code}") | ||
|
||
except ValueError: | ||
self.report.success = False | ||
self.report.errors.append(f'"{self.url.geturl()}" is not a valid URL') | ||
except Exception as e: | ||
self.report.success = False | ||
self.report.errors.append(str(e)) | ||
|
||
|
||
class HTTPCheck(Check): | ||
checks: List[HTTPCheckInstance] | ||
|
||
def __init__(self, definition: str): | ||
self.checks = [HTTPCheckInstance(d) for d in definition.split(',')] | ||
|
||
def execute(self): | ||
for check in self.checks: | ||
check.execute() | ||
|
||
@property | ||
def success(self) -> bool: | ||
return all([c.report.success for c in self.checks]) | ||
|
||
@property | ||
def report(self) -> Union[CheckReport, List[CheckReport]]: | ||
return [c.report for c in self.checks] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import List | ||
|
||
import jinja2 | ||
|
||
|
||
class CheckReport: | ||
success: bool | ||
summary: str | ||
errors: List[str] | ||
|
||
def __init__(self, success: bool = True, summary: str = '', errors: List[str] = None) -> None: | ||
self.success = success | ||
self.summary = summary | ||
self.errors = errors if errors else [] | ||
|
||
def render(self) -> str: | ||
env = jinja2.Environment(loader=jinja2.FileSystemLoader("app/views"), | ||
keep_trailing_newline=True) | ||
|
||
report_template = env.get_template('report.html') | ||
|
||
return report_template.render({'report': self}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<strong>{{ report.summary }}</strong> | ||
{% for error in report.errors %} | ||
{{ error }} | ||
{% endfor %} |
Oops, something went wrong.