[{"data":1,"prerenderedAt":175},["Reactive",2],{"/samples/python":3},[4,60,99,137],{"_path":5,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":9,"description":10,"language":11,"library":9,"link":12,"body":13,"_type":55,"_id":56,"_source":57,"_file":58,"_extension":59},"/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":14,"children":15,"toc":52},"root",[16,31,43],{"type":17,"tag":18,"props":19,"children":20},"element","p",{},[21,29],{"type":17,"tag":22,"props":23,"children":26},"a",{"href":12,"rel":24},[25],"nofollow",[27],{"type":28,"value":9},"text",{"type":28,"value":30}," 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":17,"tag":32,"props":33,"children":37},"pre",{"className":34,"code":36,"language":6,"meta":8},[35],"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",[38],{"type":17,"tag":39,"props":40,"children":41},"code",{"__ignoreMap":8},[42],{"type":28,"value":36},{"type":17,"tag":32,"props":44,"children":47},{"className":45,"code":46,"language":6,"meta":8},[35],"binary = await convert('sk_XXXXXXXXXXXXXX', {'source': 'https://en.wikipedia.org/wiki/REST'})\nwith open('result.pdf', 'wb') as f:\n    f.write(binary)\n",[48],{"type":17,"tag":39,"props":49,"children":50},{"__ignoreMap":8},[51],{"type":28,"value":46},{"title":8,"searchDepth":53,"depth":53,"links":54},2,[],"markdown","content:samples:python:aiohttp.md","content","samples/python/aiohttp.md","md",{"_path":61,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":62,"description":63,"language":11,"library":62,"link":64,"body":65,"_type":55,"_id":97,"_source":57,"_file":98,"_extension":59},"/samples/python/httplib2","Httplib2","Httplib2 is a comprehensive HTTP client library for Python. It provides support for features like caching, authentication, SSL/TLS, and proxy handling. httplib2 offers a higher-level API compared to Python's built-in http.client module and is commonly used for making HTTP requests and handling responses in Python applications.","https://httplib2.readthedocs.io/en/latest/",{"type":14,"children":66,"toc":95},[67,77,86],{"type":17,"tag":18,"props":68,"children":69},{},[70,75],{"type":17,"tag":22,"props":71,"children":73},{"href":64,"rel":72},[25],[74],{"type":28,"value":62},{"type":28,"value":76}," is a comprehensive HTTP client library for Python. It provides support for features like caching, authentication, SSL/TLS, and proxy handling. httplib2 offers a higher-level API compared to Python's built-in http.client module and is commonly used for making HTTP requests and handling responses in Python applications.",{"type":17,"tag":32,"props":78,"children":81},{"className":79,"code":80,"language":6,"meta":8},[35],"import httplib2\nimport json\n\ndef 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    # Set the API URL\n    url = f'https://api.pdfshift.io/v3/convert/{endpoint}'\n    \n    # Create an httplib2.Http instance\n    http = httplib2.Http()\n\n    # Set headers\n    headers = {\n        'Content-Type': 'application/json',\n        'X-API-Key': api_key\n    }\n    \n    # Convert params to JSON\n    body = json.dumps(params)\n    \n    # Make the POST request\n    response, content = http.request(url, method='POST', body=body, headers=headers)\n    \n    # Check for successful response\n    if response.status >= 400:\n        raise ValueError(f\"Request failed with status code {response.status}: {content.decode('utf-8')}\")\n    \n    # Decode the content\n    decoded_content = content.decode('utf-8')\n    \n    # Return the response based on the presence of filename or webhook\n    if 'filename' in params or 'webhook' in params:\n        return json.loads(decoded_content)\n    \n    return decoded_content\n",[82],{"type":17,"tag":39,"props":83,"children":84},{"__ignoreMap":8},[85],{"type":28,"value":80},{"type":17,"tag":32,"props":87,"children":90},{"className":88,"code":89,"language":6,"meta":8},[35],"binary = convert('sk_XXXXXXXXXXXXXX', {'source': 'https://en.wikipedia.org/wiki/REST'})\nwith open('result.pdf', 'wb') as f:\n    f.write(binary)\n",[91],{"type":17,"tag":39,"props":92,"children":93},{"__ignoreMap":8},[94],{"type":28,"value":89},{"title":8,"searchDepth":53,"depth":53,"links":96},[],"content:samples:python:httplib2.md","samples/python/httplib2.md",{"_path":100,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":101,"description":102,"language":11,"library":101,"link":103,"body":104,"_type":55,"_id":135,"_source":57,"_file":136,"_extension":59},"/samples/python/requests","Requests","Requests is a popular Python HTTP library that provides a simple and elegant API for making HTTP requests and handling responses. It abstracts away the complexities of HTTP communication and provides features like automatic JSON parsing, session management, and response content decoding. requests is widely used for web scraping, API integration, and web development in Python.","https://docs.python-requests.org/en/master/",{"type":14,"children":105,"toc":133},[106,116,125],{"type":17,"tag":18,"props":107,"children":108},{},[109,114],{"type":17,"tag":22,"props":110,"children":112},{"href":103,"rel":111},[25],[113],{"type":28,"value":101},{"type":28,"value":115}," is a popular Python HTTP library that provides a simple and elegant API for making HTTP requests and handling responses. It abstracts away the complexities of HTTP communication and provides features like automatic JSON parsing, session management, and response content decoding. requests is widely used for web scraping, API integration, and web development in Python.",{"type":17,"tag":32,"props":117,"children":120},{"className":118,"code":119,"language":6,"meta":8},[35],"import requests\n\ndef 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    response = requests.post(\n        f'https://api.pdfshift.io/v3/convert/{endpoint}',\n        headers={'X-API-Key': api_key},\n        json=params\n    )\n    response.raise_for_status()\n    \n    if 'filename' in params or 'webhook' in params:\n        return response.json()\n    \n    return response.content\n",[121],{"type":17,"tag":39,"props":122,"children":123},{"__ignoreMap":8},[124],{"type":28,"value":119},{"type":17,"tag":32,"props":126,"children":128},{"className":127,"code":89,"language":6,"meta":8},[35],[129],{"type":17,"tag":39,"props":130,"children":131},{"__ignoreMap":8},[132],{"type":28,"value":89},{"title":8,"searchDepth":53,"depth":53,"links":134},[],"content:samples:python:requests.md","samples/python/requests.md",{"_path":138,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":139,"description":140,"language":11,"library":139,"link":141,"body":142,"_type":55,"_id":173,"_source":57,"_file":174,"_extension":59},"/samples/python/urllib3","Urllib3","Urllib3 is a powerful HTTP client library for Python. It builds upon Python's built-in http.client module and provides features like connection pooling, SSL/TLS verification, and request retries. urllib3 offers a higher-level API compared to http.client and is commonly used for making secure and reliable HTTP requests in Python applications.","https://urllib3.readthedocs.io/en/latest/",{"type":14,"children":143,"toc":171},[144,154,163],{"type":17,"tag":18,"props":145,"children":146},{},[147,152],{"type":17,"tag":22,"props":148,"children":150},{"href":141,"rel":149},[25],[151],{"type":28,"value":139},{"type":28,"value":153}," is a powerful HTTP client library for Python. It builds upon Python's built-in http.client module and provides features like connection pooling, SSL/TLS verification, and request retries. urllib3 offers a higher-level API compared to http.client and is commonly used for making secure and reliable HTTP requests in Python applications.",{"type":17,"tag":32,"props":155,"children":158},{"className":156,"code":157,"language":6,"meta":8},[35],"import json\nimport urllib3\n\ndef 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    http = urllib3.PoolManager()\n    \n    url = f'https://api.pdfshift.io/v3/convert/{endpoint}'\n    body = json.dumps(params).encode('utf-8')\n    headers = urllib3.util.make_headers()\n    headers['X-API-Key'] = api_key\n    headers['Content-Type'] = 'application/json'\n    response = http.request('POST', url, headers=headers, body=body)\n    \n    if response.status >= 400:\n        raise ValueError(f\"Request failed with status code {response.status}: {response.data.decode('utf-8')}\")\n    \n    if 'filename' in params or 'webhook' in params:\n        return json.loads(response.data.decode('utf-8'))\n    \n    return response.data\n",[159],{"type":17,"tag":39,"props":160,"children":161},{"__ignoreMap":8},[162],{"type":28,"value":157},{"type":17,"tag":32,"props":164,"children":166},{"className":165,"code":89,"language":6,"meta":8},[35],[167],{"type":17,"tag":39,"props":168,"children":169},{"__ignoreMap":8},[170],{"type":28,"value":89},{"title":8,"searchDepth":53,"depth":53,"links":172},[],"content:samples:python:urllib3.md","samples/python/urllib3.md",1785426821627]