Package: postoffice-django
Post office django clientWhat is postoffice djangopostoffice_djangois a django app to communicate withpostoffice.FeaturesSet up server via django commands:Create necessarytopicsinpostofficewithconfigure_postoffice_publishersto be able to publish a messageCreate necessarypublishersinpostofficewithconfigure_postoffice_topicsto be able to consume messagesEasily send messages topostoffice serverHow to install itPrerequisitesTo be able to run the application, you must havedjangorequestsObviously, you need a django project up and runningInstalling postoffice_djangoAt the moment there are two ways to install the app:$pipinstallpostoffice-djangoor addpostoffice-djangoto your requirements fileAdd it to your Django installed apps:INSTALLED_APPS=[...'postoffice_django']Then, you need to set the required settings for your app:POSTOFFICE={'URL':'http://fake.service','CONSUMERS':[{'topic':'some_topic','target':'http://www.some_url.com','type':'http','from_now':True},{'topic':'another_topic','target':'http://www.another_url.com','type':'pubsub','from_now':False}],'TIMEOUT':0.3,'BULK_TIMEOUT':5,'ORIGIN_HOST':'example.com','TOPICS':['topic_to_create','another_topic_to_create'],'RECOVERY_ENABLED':True}URL: Is theurlwhere the Postoffice server is hosted.CONSUMERS: Are the consumers which must been configured as publishers in Postoffice server. With that, we create the necessary topics and publishers on Postoffice.topic: Topic name to the consumertarget: Url or pub/sub topic nametype: http/pubsubtimeout: Seconds Postoffice should wait before cancelling the request. [Optional]TIMEOUT: Specific timeout to use in every communication with Postoffice. If not specified, the default value is 0.5 seconds.BULK_TIMEOUT: Specific timeout to use when sending bulk messages to Postoffice. If not specified, the default value is 5 seconds.ORIGIN_HOST: The host from where the topic is created (your host).  It is necessary in order topostofficeknow where the topic come from.TOPICS: Topics to be created in order to send messages topostofficeRECOVERY_ENABLED: Enables the recovery of failed messages on all new topics. If not specified, the default value is false.Finally addpostofficepath in your django urls file:urlpatterns=[path('admin/',admin.site.urls),path('health/',include('service.health.urls')),path('postoffice/',include('postoffice_django.urls'))]How to setup Postoffice via django commandsNow we are ready to start sending messages topostoffice. But first, we must generatetopicsand/orpublishersin postoffice depending on the purpose of the project with postoffice.If we need to create the topics to be able to publish, we should execute:$./manage.pyconfigure_postoffice_topicsand, if we need to create the publishers, we should execute:$./manage.pyconfigure_postoffice_publishersSending messages to postofficeWe have three publishers in thepublishingmodulePublishing a single messagefrompostoffice_django.publishingimportpublishpublish(topic:str,message:dict,**attributes:dict)->Nonetopic: Topic name. This topicmustexist on postoffice to manage the message.payload: Message to be sent. Thismustbe a dict.attributes: Additional attr. All attributes are cast to string when publishing a message.Scheduling messages to be published in the futureIn cases where you want to publish a message to PostOffice now, but would like to receive it in the future, you can schedule the messagefrompostoffice_django.publishingimportscheduled_publishscheduled_publish(topic:str,message:dict,schedule_in:int,**attributes:dict)->NoneThe only difference with thepublishmethod, is that it receives a new param:schedule_in: The amount ofminutesin the future when you want to receive the message.Disclaimer: If the message cannot be published to PostOffice and is inserted as a PublishingError, it will not be scheduled as of right now. Please take this into consideration.Publishing messages in batchesIn case we want to send multiple messages to the same topic, the best way is sending them in batches.frompostoffice_django.publishingimportbulk_publishdefbulk_publish(topic:str,payload:list,**attrs:dict)->None:topic: Topic name. This topicmustexist on postoffice to manage the message.payload: Message's payloads to be sent. Thismustbe a list.attributes: Additional attr. All attributes are cast to string when publishing a message.An example:bulk_publish('test-topic',[{'message1':'key1'},{'message2':'key2'}],{'additional_key':1})This will be handled on Postoffices and we'll actually receive two messages for the same topictest_topic,{'message1': 'key1'},{'additional_key', 1}}test_topic,{'message2': 'key2'},{'additional_key', 1}}How to contributeImplementmake install-test-requirementsmake env-startWrite a test.make testImplement up to the test are green.Repeat.Deploymake deploy VERSION=1.0.0