communicator requests api implementation done
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import logging
|
||||
import random
|
||||
import requests
|
||||
from singleton import Singleton
|
||||
from consumerlocator import ConsumerLocator
|
||||
|
||||
"""
|
||||
Communicator module
|
||||
@@ -13,32 +13,54 @@ __copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||
__module_name__ = "messagesender"
|
||||
__version__text__ = "1"
|
||||
|
||||
class Communicator(Singleton):
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
class Communicator:
|
||||
"""
|
||||
Class handling low level communication with consumers.
|
||||
"""
|
||||
|
||||
def __init__(self, consumerlocator: ConsumerLocator, uuid):
|
||||
"""
|
||||
Initialize object
|
||||
:param consumerlocator:
|
||||
:param uuid:
|
||||
"""
|
||||
self.consumerlocator=consumerlocator
|
||||
self.uuid = uuid
|
||||
|
||||
def sendmessage(self, message: str) -> None:
|
||||
"""
|
||||
Send message to consumer.
|
||||
:param message:
|
||||
:return: none
|
||||
"""
|
||||
return None
|
||||
currentconsumer=self.consumerlocator.getcurrentconsumer()
|
||||
LOGGER.debug(f"Sending message to {currentconsumer}")
|
||||
requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message})
|
||||
|
||||
def discoveravailableconsumers(self) -> list:
|
||||
"""
|
||||
Get the list of available consumer from the current primary consumer.
|
||||
:return:
|
||||
"""
|
||||
return ["10.69.42.1","10.42.69.1","10.10.10.10","10.6.66.1"]
|
||||
currentconsumer = self.consumerlocator.getcurrentconsumer()
|
||||
response = requests.get(f'http://{currentconsumer}/consumer')
|
||||
json = response.json()
|
||||
LOGGER.debug(f"List of currently available consumers: {json}")
|
||||
return json
|
||||
|
||||
def isconsumeravailable(self) -> bool:
|
||||
"""
|
||||
Readiness probe primary consumer.
|
||||
:return:
|
||||
"""
|
||||
return bool(random.getrandbits(1))
|
||||
currentconsumer = self.consumerlocator.getcurrentconsumer()
|
||||
response = requests.get(f'http://{currentconsumer}/consumer')
|
||||
isavailable = response.status_code == 200
|
||||
LOGGER.debug(f"Current consumer availability: {isavailable}")
|
||||
return isavailable
|
||||
|
||||
def checkconsumer(self, consumer: str) -> bool:
|
||||
"""
|
||||
@@ -46,4 +68,7 @@ class Communicator(Singleton):
|
||||
:param consumer:
|
||||
:return:
|
||||
"""
|
||||
return bool(random.getrandbits(1))
|
||||
response = requests.get(f'http://{consumer}/consumer')
|
||||
isavailable = response.status_code == 200
|
||||
LOGGER.debug(f"Consumer {consumer} availability: {isavailable}")
|
||||
return isavailable
|
||||
|
||||
Reference in New Issue
Block a user