[{"data":1,"prerenderedAt":73},["Reactive",2],{"/samples/python/aiohttp":3,"related:kQJojXrixc":59},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":8,"link":11,"body":12,"_type":54,"_id":55,"_source":56,"_file":57,"_extension":58},"/samples/python/aiohttp","python",false,"","Aiohttp","Aiohttp is an asynchronous HTTP client/server framework for Python. It allows developers to write asynchronous web servers and clients using Python's asyncio library. aiohttp provides a high-level API for making HTTP requests and handling responses asynchronously, making it suitable for high-performance networking applications and web services.","Python","https://docs.aiohttp.org/en/stable/",{"type":13,"children":14,"toc":51},"root",[15,30,42],{"type":16,"tag":17,"props":18,"children":19},"element","p",{},[20,28],{"type":16,"tag":21,"props":22,"children":25},"a",{"href":11,"rel":23},[24],"nofollow",[26],{"type":27,"value":8},"text",{"type":27,"value":29}," is an asynchronous HTTP client/server framework for Python. It allows developers to write asynchronous web servers and clients using Python's asyncio library. aiohttp provides a high-level API for making HTTP requests and handling responses asynchronously, making it suitable for high-performance networking applications and web services.",{"type":16,"tag":31,"props":32,"children":36},"pre",{"className":33,"code":35,"language":5,"meta":7},[34],"language-python","import aiohttp\nimport asyncio\nimport json\n\nasync def convert(api_key, params, endpoint='pdf'):\n    \"\"\"\n    We focused on making this function as simple as possible.\n    Since PDFShift is a REST API, we just need to send a\n    POST request to the endpoint by passing a set of custom parameters.\n    \n    Args:\n        api_key (str): Your API key.\n        params (dict): A dictionary containing the parameters\n                       you want to send to the API.\n        endpoint (str): The type of conversion you want to perform\n                        (pdf, png, jpg, webp)\n    \n    Returns:\n        bytes | dict: Either the binary content of the PDF\n                      or a dictionary containing the details\n                      when filename/webhook are passed\n    \"\"\"\n    \n    assert endpoint in ('pdf', 'png', 'jpg', 'webp')\n\n    try:\n        async with aiohttp.ClientSession() as session:\n            async with session.post(\n                'https://api.pdfshift.io/v3/convert/{}'.format(endpoint),\n                headers={'X-API-Key': api_key},\n                json=params\n            ) as response:\n                if response.status >= 400:\n                    raise Exception('Invalid request: {}'.format(await response.text()))\n                \n                if 'filename' in params or 'webhook' in params:\n                    return json.loads(await response.text())\n                \n                return await response.read()\n    except asyncio.TimeoutError:\n        raise Exception('The request took too long to process')\n    except aiohttp.ClientError as e:\n        raise Exception(f'An error occurred: {e}')\n    except Exception as e:\n        # We highly recommend you to handle exceptions. Often, PDFShift will provide you with a clear explanation about what happened.\n        # Moreover, in case of error, no PDF are returned !\n        raise Exception(f'An error occurred: {e}')\n",[37],{"type":16,"tag":38,"props":39,"children":40},"code",{"__ignoreMap":7},[41],{"type":27,"value":35},{"type":16,"tag":31,"props":43,"children":46},{"className":44,"code":45,"language":5,"meta":7},[34],"binary = await convert('sk_XXXXXXXXXXXXXX', {'source': 'https://en.wikipedia.org/wiki/REST'})\nwith open('result.pdf', 'wb') as f:\n    f.write(binary)\n",[47],{"type":16,"tag":38,"props":48,"children":49},{"__ignoreMap":7},[50],{"type":27,"value":45},{"title":7,"searchDepth":52,"depth":52,"links":53},2,[],"markdown","content:samples:python:aiohttp.md","content","samples/python/aiohttp.md","md",[60,61,65,69],{"_path":4,"language":10,"library":8,"_id":55},{"_path":62,"language":10,"library":63,"_id":64},"/samples/python/httplib2","Httplib2","content:samples:python:httplib2.md",{"_path":66,"language":10,"library":67,"_id":68},"/samples/python/requests","Requests","content:samples:python:requests.md",{"_path":70,"language":10,"library":71,"_id":72},"/samples/python/urllib3","Urllib3","content:samples:python:urllib3.md",1785426823438]