Merge in RR/pyinfra from pika_encapsulation to master
Squashed commit of the following:
commit 251fea4094062a72f5f0b1f8a54f959a1d7309ec
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 15 15:02:25 2022 +0100
Adjusted readme and config to the actual changes.
commit d00a0aadc02be8ab1343dbaa2a8df82418e77673
Merge: ded29bb c34a1ce
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 15 14:06:21 2022 +0100
Merge branch 'master' of ssh://git.iqser.com:2222/rr/pyinfra into pika_encapsulation
commit ded29bb8d0a78c1d5fd172edb74f0b120da05d5f
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 15 14:02:37 2022 +0100
blackkkkkkkkkkkkkkkk
commit de3899c69f4e83fa5b8dc2702a18be66661201e9
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 15 13:59:43 2022 +0100
Judgement Day - The Great Refactoring
commit 48eb2bb792dd45539e7db20949fedae03316c2b3
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 15 13:30:41 2022 +0100
blacky
commit 38fb073291c6989fcc5560ef9f97e7dafd9570be
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 15 13:29:46 2022 +0100
update mock client/publish
commit 8744cdeb6d21adab81fb82c7850a4ada31a1a3f9
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Tue Mar 15 13:29:19 2022 +0100
quickfix consumer.consume_and_publish bug
commit 0ce30cafbf15142e3f773d0e510b7d22927ee86d
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Tue Mar 15 11:42:04 2022 +0100
updated serving logic for new queue, visior and storage logic
commit 5ac5fbf50bf9d8a6b6466cd04d82d21525250b4c
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Tue Mar 15 11:41:18 2022 +0100
changed callback signature to expect a dict with a key 'data'
commit 4c7c4b466e2b5228a6098c9130cd6c3334315153
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Tue Mar 15 09:46:39 2022 +0100
set dead letter queue related params
commit f8b60aad16eb0f5c91bed857ac3b352c4bcb7468
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Mar 14 20:22:57 2022 +0100
added republishing logic
commit a40f07139af9121d8b72efa52b6b2588e7d233e8
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Mar 14 19:10:08 2022 +0100
removed obsolete import
commit d3605b0913f8b155961b8c8e90e7c1d442a1da4f
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Mar 14 19:09:50 2022 +0100
removed obsolete code
commit 9130b9fc753c63bfe4abd3cc26b31caf5c5bf2cb
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Mar 14 19:05:59 2022 +0100
test refac
commit 1205adecdbd42a46dcc264a46086aab94c285b94
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Mar 14 14:49:59 2022 +0100
removed obsolete import
commit 59d0de97e39646b3fa6c6ca91644dbf8adbd0de3
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Mar 14 14:49:21 2022 +0100
applied black
commit f78a6894bc99b42d53a7c265ffd8c7869e9d606d
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Mar 14 14:49:03 2022 +0100
applied black
commit 32740a914f8be15eed954389f6ddbb565e1567e6
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Mar 14 14:47:47 2022 +0100
removed obsolete code
commit f21cbc141efc10d0e779f683dcc452e6c889dd6b
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Mar 14 14:47:27 2022 +0100
consumer with visitor test fixed (added connection.close() in case of pika queue manager)
commit c415dcd8e6375e015b959a9c3cdee61b8ffe7d60
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Mon Mar 14 13:56:54 2022 +0100
consumer with visitor test WIP 2
... and 15 more commits
89 lines
2.7 KiB
Python
89 lines
2.7 KiB
Python
import abc
|
|
import gzip
|
|
import json
|
|
import logging
|
|
from operator import itemgetter
|
|
from typing import Callable
|
|
|
|
from pyinfra.config import CONFIG
|
|
from pyinfra.exceptions import DataLoadingFailure
|
|
from pyinfra.storage.storage import Storage
|
|
|
|
|
|
def get_object_name(body):
|
|
dossier_id, file_id, target_file_extension = itemgetter("dossierId", "fileId", "targetFileExtension")(body)
|
|
object_name = f"{dossier_id}/{file_id}.{target_file_extension}"
|
|
return object_name
|
|
|
|
|
|
def get_response_object_name(body):
|
|
dossier_id, file_id, response_file_extension = itemgetter("dossierId", "fileId", "responseFileExtension")(body)
|
|
object_name = f"{dossier_id}/{file_id}.{response_file_extension}"
|
|
return object_name
|
|
|
|
|
|
def get_object_descriptor(body):
|
|
return {"bucket_name": CONFIG.storage.bucket, "object_name": get_object_name(body)}
|
|
|
|
|
|
def get_response_object_descriptor(body):
|
|
return {"bucket_name": CONFIG.storage.bucket, "object_name": get_response_object_name(body)}
|
|
|
|
|
|
class ResponseStrategy(abc.ABC):
|
|
@abc.abstractmethod
|
|
def handle_response(self, body):
|
|
pass
|
|
|
|
def __call__(self, body):
|
|
return self.handle_response(body)
|
|
|
|
|
|
class StorageStrategy(ResponseStrategy):
|
|
def __init__(self, storage):
|
|
self.storage = storage
|
|
|
|
def handle_response(self, body):
|
|
self.storage.put_object(**get_response_object_descriptor(body), data=gzip.compress(json.dumps(body).encode()))
|
|
body.pop("data")
|
|
return body
|
|
|
|
|
|
class ForwardingStrategy(ResponseStrategy):
|
|
def handle_response(self, body):
|
|
return body
|
|
|
|
|
|
class QueueVisitor:
|
|
def __init__(self, storage: Storage, callback: Callable, response_strategy):
|
|
self.storage = storage
|
|
self.callback = callback
|
|
self.response_strategy = response_strategy
|
|
|
|
def load_data(self, body):
|
|
def download():
|
|
logging.debug(f"Downloading {object_descriptor}...")
|
|
data = self.storage.get_object(**object_descriptor)
|
|
logging.debug(f"Downloaded {object_descriptor}.")
|
|
return data
|
|
|
|
object_descriptor = get_object_descriptor(body)
|
|
|
|
try:
|
|
return gzip.decompress(download())
|
|
except Exception as err:
|
|
logging.warning(f"Loading data from storage failed for {object_descriptor}.")
|
|
raise DataLoadingFailure() from err
|
|
|
|
def process_data(self, data, body):
|
|
return self.callback({**body, "data": data})
|
|
|
|
def load_and_process(self, body):
|
|
data = self.process_data(self.load_data(body), body)
|
|
result_body = {**body, "data": data}
|
|
return result_body
|
|
|
|
def __call__(self, body):
|
|
result_body = self.load_and_process(body)
|
|
return self.response_strategy(result_body)
|