| 29 | |
| 30 | |
| 31 | class QueueClient: |
| 32 | |
| 33 | def __init__(self): |
| 34 | if settings.DIFFGRAM_SYSTEM_MODE == 'testing': |
| 35 | return |
| 36 | ssl_options = None |
| 37 | if settings.RABBITMQ_USE_SSL: |
| 38 | ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) |
| 39 | ssl_context.set_ciphers('ECDHE+AESGCM:!ECDSA') |
| 40 | ssl_options = pika.SSLOptions(context = ssl_context) |
| 41 | |
| 42 | self.connection = pika.BlockingConnection( |
| 43 | pika.ConnectionParameters(host = settings.RABBITMQ_HOST, |
| 44 | port = settings.RABBITMQ_PORT, |
| 45 | ssl_options = ssl_options, |
| 46 | heartbeat = 10, |
| 47 | credentials = pika.PlainCredentials(settings.RABBITMQ_DEFAULT_USER, |
| 48 | settings.RABBITMQ_DEFAULT_PASS)) |
| 49 | ) |
| 50 | self.main_channel = self.connection.channel() |
| 51 | |
| 52 | self.main_channel.exchange_declare(exchange = Exchanges.actions.value, |
| 53 | exchange_type = ExchangeType.direct.value) |
| 54 | self.main_channel.exchange_declare( |
| 55 | exchange = Exchanges.events.value, |
| 56 | exchange_type = ExchangeType.direct.value) |
| 57 | |
| 58 | self.main_channel.exchange_declare( |
| 59 | exchange = Exchanges.exports.value, |
| 60 | exchange_type = ExchangeType.direct.value) |
| 61 | |
| 62 | def send_message(self, |
| 63 | message: dict, |
| 64 | routing_key: str, |
| 65 | exchange: str): |
| 66 | """ |
| 67 | Publishes a message to rabbit MQ. For now its using the default |
| 68 | actions exchange. But we can modify this wrapper to include more paremeters |
| 69 | for the exchange name. |
| 70 | :param message: |
| 71 | :param routing_key: |
| 72 | :param exchange: |
| 73 | :return: |
| 74 | """ |
| 75 | self.main_channel.basic_publish( |
| 76 | exchange = exchange, |
| 77 | routing_key = routing_key, |
| 78 | body = json.dumps(message).encode('utf-8'), |
| 79 | properties = pika.BasicProperties(content_type = 'application/json')) |
no outgoing calls
no test coverage detected