Skip to content

Commit

Permalink
📝 patch aiohttp refs in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ousret committed Nov 10, 2024
1 parent 25fc522 commit 1cd5e48
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 46 deletions.
56 changes: 16 additions & 40 deletions docs/getting_started/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ Environment variables have the highest priority, followed by keyword arguments t
Using an HTTP or HTTPS proxy with Async PRAW
--------------------------------------------

Async PRAW internally relies upon the aiohttp_ package to handle HTTP requests. Aiohttp
supports use of ``HTTP_PROXY`` and ``HTTPS_PROXY`` environment variables in order to
proxy HTTP and HTTPS requests respectively [`ref
<https://docs.aiohttp.org/en/stable/client_advanced.html?highlight=proxy#proxy-support>`_].
Async PRAW internally relies upon the niquests_ package to handle HTTP requests.
Niquests supports use of ``HTTP_PROXY`` and ``HTTPS_PROXY`` environment variables in
order to proxy HTTP and HTTPS requests respectively [`ref
<https://niquests.readthedocs.io/en/latest/user/advanced.html#proxies>`_].

Given that Async PRAW exclusively communicates with Reddit via HTTPS, only the
``HTTPS_PROXY`` option should be required.
Expand All @@ -44,50 +44,26 @@ variable can be provided on the command line like so:
HTTPS_PROXY=http://localhost:3128 ./prawbot.py
Contrary to the Requests library, aiohttp_ won’t read environment variables by default.
But you can do so by passing ``trust_env=True`` into a custom ClientSession_ and
configuring Async PRAW like so:

.. code-block:: python
import asyncpraw
from aiohttp import ClientSession
session = ClientSession(trust_env=True)
reddit = asyncpraw.Reddit(
client_id="SI8pN3DSbt0zor",
client_secret="xaxkj7HNh8kwg8e5t4m6KvSrbTI",
password="1guiwevlfo00esyy",
requestor_kwargs={"session": session}, # pass the custom Session instance
user_agent="testscript by u/fakebot3",
username="fakebot3",
)
Configuring a custom aiohttp ClientSession
Configuring a custom niquests AsyncSession
------------------------------------------

Async PRAW uses aiohttp_ to handle networking. If your use-case requires custom
configuration, it is possible to configure a ClientSession_ and then use it with Async
Async PRAW uses niquests_ to handle networking. If your use-case requires custom
configuration, it is possible to configure a AsyncSession_ and then use it with Async
PRAW.

For example, some networks use self-signed SSL certificates when connecting to HTTPS
sites. By default, this would raise an exception in aiohttp_. To use a self-signed SSL
certificate without an exception from aiohttp_, first export the certificate as a
sites. By default, this would raise an exception in niquests_. To use a self-signed SSL
certificate without an exception from niquests_, first export the certificate as a
``.pem`` file. Then configure Async PRAW like so:

.. code-block:: python
import ssl
import aiohttp
import niquests
import asyncpraw
session = niquests.AsyncSession()
session.verify = "/path/to/certfile.pem"
ssl_ctx = ssl.create_default_context(cafile="/path/to/certfile.pem")
conn = aiohttp.TCPConnector(ssl_context=ssl_ctx)
session = aiohttp.ClientSession(connector=conn)
reddit = asyncpraw.Reddit(
client_id="SI8pN3DSbt0zor",
client_secret="xaxkj7HNh8kwg8e5t4m6KvSrbTI",
Expand All @@ -97,12 +73,12 @@ certificate without an exception from aiohttp_, first export the certificate as
username="fakebot3",
)
The code above creates a ClientSession_ and `configures it to use a custom certificate
<https://docs.aiohttp.org/en/stable/client_advanced.html#ssl-control-for-tcp-sockets>`_,
The code above creates a AsyncSession_ and `configures it to use a custom certificate
<https://niquests.readthedocs.io/en/latest/user/advanced.html#ssl-cert-verification>`_,
then passes it as a parameter when creating the :class:`.Reddit` instance. Note that the
example above uses a :ref:`password_flow` authentication type, but this method will work
for any authentication type.

.. _aiohttp: https://docs.aiohttp.org/
.. _asyncsession: https://niquests.readthedocs.io/en/latest/user/advanced.html

.. _clientsession: https://docs.aiohttp.org/en/stable/client_advanced.html
.. _niquests: https://niquests.readthedocs.io/en/latest/
12 changes: 6 additions & 6 deletions docs/package_info/asyncpraw_migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Network Requests

.. _network_requests:

Async PRAW utilizes `aiohttp <https://docs.aiohttp.org/>`_ to make network requests to
Reddit's API. Since aiohttp can only be used in an asynchronous environment, all network
requests need to be awaited. Due to this, most Async PRAW methods need to be awaited as
well. You can tell if a method needs awaited by looking at the docs. For example,
:meth:`.me` has the word ``await`` before ``me(use_cache: bool = True)`` in the header
for that method since that method makes a network request.
Async PRAW utilizes `niquests <https://niquests.readthedocs.io/en/latest/>`_ to make
network requests to Reddit's API. We bound Async PRAW to the async part of Niquests, so
all network requests need to be awaited. Due to this, most Async PRAW methods need to be
awaited as well. You can tell if a method needs awaited by looking at the docs. For
example, :meth:`.me` has the word ``await`` before ``me(use_cache: bool = True)`` in the
header for that method since that method makes a network request.

Lazy Loading
------------
Expand Down

0 comments on commit 1cd5e48

Please sign in to comment.