#messages.add_message(request, messages.INFO, 'MyAPI has executed!') not working.

I have messages working in my app with messsages getting added and displayed. However, I have one service called from a CustomMiddleware that is failing. I can add a message and display it when the custom middleware executes but not the service it calls

Here is the middleware code that calls the service

getdata = requests.get(serviceUrl, params=request_get_params)

Where the serviceUrl comes from a registry and the params passed are the same params from the original request.

The atomic service called by serviceUrl is /correlator/api/atomicservice1

The atomicservice1 code that is not working…

class service1(APIView):

def get(self, request, format=None):

    messages.add_message(request, messages.INFO, 'Atomic Service 1 has executed!')
    print("=================== atomicservice1 get() ===============")
    return Response({"message": "AtomicService1!"})

My guess is that the requests.get() creates a request that contains the params of the original request but the add message is not working because this request object is not carried back to the middleware and on through the flow.

How can I get these atomic services to add messages. Can I serialize the original request, deserialize it and add the message?

Do I need to make the atomic service callable methods instead of URLs so the original request can be passed? I want to keep the URL method so I have loosely coupled atomic services.

Keep in mind that the request object provided in the get method within your service is the request created by your call to requests.get and not the original request submitted which is being handled by your middleware. Therefore, the messages are being applied to the session created for the requests request.

Your service is not going to be able to apply those messages directly to the original session without you making that original session available to the service. Otherwise, your middleware will need to process the response and add the messages to its original request.

If these “atomic services” are only going to be used within the context of this application, then yes, I think you should.

If these services are only being used within this application, you’re not improving anything by doing it this way. There is no structural or architectural benefit, and a lot of unnecessary overhead being created. There is zero reduction of coupling by only invoking a URL instead of a function call. The decoupling would occur by how you locate and identify the function being used - and that can be handled in Python just as easily with a function call as with a URL invocation.

1 Like