Package: cosecurity-amqp-lib
AMQP Internal LibraryInternal library for exchanging messages between services instantiated in the AWS environment.
The base used for the elaboration of the package was the PIKA library developed by the RabbitMQ team.InstallationTo use the library it is necessary to havePython3installed on the machine and run the following command:python3-mpipinstallcosecurity-amqp-libEnvironment Variables FileAMQP_USERuser for connecting to RabbitMQAMQP_PASSWORDpassword for connecting to RabbitMQ[SERVICE_NAME]service name and respective service queue name, can be more than oneAMQP_SOCKET_TIMEOUTsocket connect timeout in secondsAMQP_HEARTBEATAMQP connection heartbeat timeout value for negotiation during connection tuning or callable which is invoked during connection tuningif it is a local instance:AMQP_HOSThost to connect to RabbitMQ, if localhost does not need to fill the portif the connection is via Amazon MQ:AMQP_BROKER_IDamqp id inside awsAWS_REGIONregion where amqp is allocated within awsConsumersConsumers are instances that monitor a specific queue, and if there is a change in the queue, they perform a certain action.In this library a consumer can have more than one action/method, calledprimitive. In addition, each action will still have its default input set.
Each method that must be an action must be registered so that it can be triggered if there is a change in the directed queue.Below is an example of how to create a consumer class:fromtypingimportAny,Dictfromcosecurity_amqp_lib.consumerimportConsumerclassConsumerExample(Consumer):def__init__(self)->None:super().__init__(name='example')self.register(self.primitive_one)self.register(self.primitive_two)self.start()defprimitive_one(self,content:Dict[str,Any])->None:print(content['hello'])defprimitive_two(self,content:Dict[str,Any])->None:print(content['message'])ProducersProducers are responsible for producing and/or posting new messages in consumer queues.In the internal library the producers are calledstub, I try their methods defined and typed based on what has already been defined asprimitivein their consumer.Below is an example of how to create astubinside the library in thestub.py file:classExampleStub(Stub):def__init__(self):super().__init__(destination='example')defprimitive_one(self)->None:self._send(primitive='primitive_one',content={'hello':'word'})defprimitive_two(self,message:str)->None:self._send(primitive='primitive_two',content={'message':message})Now, an example of how to use an already createdstuband publish it in the library in Pypi:fromcosecurity_amqp_lib.stubimportExampleStubexample_stub=ExampleStub()example_stub.primitive_one()example_stub.primitive_two(message='Hello world!')Additional arguments for queuesIn the creation of the consumers it is possible to pass some additional configurations for the creation of the queue, these configurations can be found on the official RabbitMQ website. Below is a representation of how to pass an additional argument:classConsumerExample(Consumer):def__init__(self)->None:[...]self.start(arguments={'x-queue-mode':'lazy'})ExampleIn theexample/simplefolder we have a real case example of astubsending an string to a consumer.