Scripting mitmdump to intercept requests and alter them according to external conditions

Hello to everyone.
I was looking for a way to intercept http requests to an authentication service and give back custom answers base upon queries to a third service, in order to work around limitations of the authentication service in question I cannot alter myself.
I got the idea that scripting a mitmdump instance to perform the external requests and either allow the authentication request to pass through or give back and error response depending on the output of the external system.
Since I am pretty unfamiliar with the tools I am, however, uncertain whether this is the right tool for the job. I would like something stable over long periods of time and which I could customize to interface with external system communicating over HTTP. I’ve taken a look at the addon examples and found nothing initiating connections to external systems nor did I get an idea whether APIs allowing to initiate external connections exist or if I should roll something up on my own.

Any ideas/advice?

Thanks in advance