From eaf0b51fe94648dc247c74f064e1725d3f905fb6 Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Fri, 4 Sep 2020 19:45:43 +0200 Subject: [PATCH] Take out common code in a base class --- src/chweb/base.py | 24 ++++++++++++++++++++++++ src/chweb/collector.py | 14 ++++---------- src/chweb/consumer.py | 11 +++-------- 3 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 src/chweb/base.py diff --git a/src/chweb/base.py b/src/chweb/base.py new file mode 100644 index 0000000..f05438d --- /dev/null +++ b/src/chweb/base.py @@ -0,0 +1,24 @@ +""" +Base classes used in multiple modules. +""" +import asyncio +import logging + +from chweb.models import Config + + +class Application: + """ + A base class for applications / services. + """ + def __init__(self, config: Config, + logger: logging.Logger, + event_loop: asyncio.AbstractEventLoop, + queue: asyncio.Queue): + self.config = config + self.logger = logger + self.loop = event_loop + self.queue = queue + + def run(self): + raise NotImplementedError() diff --git a/src/chweb/collector.py b/src/chweb/collector.py index dcec68f..346419c 100644 --- a/src/chweb/collector.py +++ b/src/chweb/collector.py @@ -10,21 +10,15 @@ from urllib.parse import urlparse import aiokafka # type: ignore import requests -from chweb.models import Config, Check +from chweb.base import Application +from chweb.models import Check -class Collector: +class Collector(Application): """ A class that contains all methods needed to check the statuses of all websites present in the config. """ - def __init__(self, config: Config, - event_loop: asyncio.AbstractEventLoop, - queue: asyncio.Queue): - self.config = config - self.loop = event_loop - self.queue = queue - async def check(self, url: str, regex: Optional[str]) -> Check: """ Checks the status of a website and optionally matches a regex on the @@ -45,7 +39,7 @@ class Collector: response_time=res.elapsed.microseconds, regex_matches=matches, status=res.status_code, - url=res.url, + url=url, ) async def check_forever(self, site): diff --git a/src/chweb/consumer.py b/src/chweb/consumer.py index ae14caa..d7f7df6 100644 --- a/src/chweb/consumer.py +++ b/src/chweb/consumer.py @@ -3,22 +3,17 @@ Sample consumer. """ import asyncio import json +import logging from typing import Any, Dict import aiokafka # type: ignore import asyncpg # type: ignore +from chweb.base import Application from chweb.models import Check -class Consumer: - def __init__(self, config: Dict[str, Any], - event_loop: asyncio.AbstractEventLoop, - queue: asyncio.Queue): - self.config = config - self.loop = event_loop - self.queue = queue - +class Consumer(Application): async def consume(self): """ Consumes messages from a Kafka topic.