storage-service-feeder/storage_service_feeder/feeder.py
marcsello 7ca9f50af2
All checks were successful
continuous-integration/drone/push Build is passing
Did the needful
2021-08-17 16:59:22 +02:00

45 lines
1.4 KiB
Python

from requests_opentracing import SessionTracing
import opentracing
import json
from urllib.parse import urljoin
import redis
from config import Config
class Feeder:
def __init__(self):
self._session = SessionTracing(propagate=True)
self._redis_client = redis.from_url(Config.REDIS_URL)
def do_feed(self, tag: str, mimetype: str = 'audio/wave'):
with opentracing.tracer.start_active_span('feeder.getFromCache'):
sample_data = self._redis_client.get(tag)
with opentracing.tracer.start_active_span('feeder.uploadToStorageService'):
with opentracing.tracer.start_active_span('uploadToStorageService'):
files = {
'description': (None, json.dumps({'tag': tag}), 'application/json'),
'soundFile': (
'wave.wav', # <- this is not used by upstream
sample_data,
mimetype,
{
'Content-Length': len(sample_data)
}
)
}
r = self._session.post(
urljoin(Config.STORAGE_SERVICE_URL, "object"),
files=files
)
r.raise_for_status()
with opentracing.tracer.start_active_span('feeder.deleteFromCache'):
self._redis_client.delete(tag)