Search code examples
pythonaiohttp

How to translate a cURL request to Python's aiohttp?


I need to add a function to my Python project that checks comments for toxicity. The example cURL is this:

 curl -H "Content-Type: application/json" --data \
    '{comment: {text: "what kind of idiot name is foo?"},
       languages: ["en"],
       requestedAttributes: {TOXICITY:{}} }' \
https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key=YOUR_KEY_HERE

Now there, is also an example Python code. But that is no good, because it's synchronous. I need it to be async, I need to use aiohttp. This is my attempt to translate the cURL request:

import aiohttp, asyncio

async def main():
    async with aiohttp.ClientSession(headers={"CONTENT-TYPE": "application/json"}) as session:
        async with session.get("https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key"
                               "=",
                               json={"comment": {"text": "what kind of idiot name is foo?"},
                                     "languages": ["en"],
                                     "requestedAttributes": {"TOXICITY": {}}},
                               ) as resp:
            print(resp)


asyncio.run(main())

(I've hid my API key) Unfortunately, that doesn't work, that yields:

<ClientResponse(https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key=) [400 Bad Request]>
<CIMultiDictProxy('Content-Type': 'text/html; charset=UTF-8', 'Referrer-Policy': 'no-referrer', 'Content-Length': '1555', 'Date': 'Thu, 22 Sep 2022 09:37:52 GMT')>

How do I fix this? I've went through the aiohttp docs, tried many things, played around with the kwargs and I still get the same thing. Please help

EDIT:

So, after some playing around in Postman, I managed to send a successful request. There were a couple mistakes. First off, it has to be a POST request. Second of all, it didn't work without these 2 headers:

Host: commentanalyzer.googleapis.com
Content-Length: 160

Content-Length is calculated automatically. Problem is when I try to do that in Pycharm on Fedora, it doesn't work. It hangs. After setting a timeout of 3 seconds, it raises that error.


Solution

  • After a lot of tinkering with Postman and my bot, I found the correct arguments:

    async def analyzeText(text: str, threshold=0.90, bot=None):
        analyze_request = {
            "comment": {"text": text.replace("'", "").replace('"', '')},
            "requestedAttributes": {
                "SEVERE_TOXICITY": {},
                "IDENTITY_ATTACK": {},
                "THREAT": {},
                "INSULT": {},
                "INFLAMMATORY": {},
            },
        }
        try:
            async with bot.session.post(
                    f"https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key"
                    f"={PERSPECTIVE_API_KEY}",
                    json=analyze_request,
                    headers={
                        "Content-Type": "application/json",
                        "Content-Length": str(len(str(analyze_request))),
                        "Host": "commentanalyzer.googleapis.com",
                    },
                    timeout=3,
            ) as resp:
                pass
        except aiohttp.web.HTTPException as e:
            pass
        return resp