HTTP¶
Internal API
These classes are for internal use only.
Users should interact with Client and other public API classes instead.
HTTPClient
¶
BASE = 'https://discord.com/api/v10'
class-attribute
instance-attribute
¶
MAX_RETRIES = 3
class-attribute
instance-attribute
¶
session = None
instance-attribute
¶
logger = logger
instance-attribute
¶
queues: dict[str, asyncio.Queue] = {}
instance-attribute
¶
queues_lock = asyncio.Lock()
instance-attribute
¶
workers: dict[str, asyncio.Task] = {}
instance-attribute
¶
buckets: dict[str, Bucket] = {}
instance-attribute
¶
bucket_lock: dict[str, asyncio.Lock] = {}
instance-attribute
¶
buckets_lock = asyncio.Lock()
instance-attribute
¶
global_lock = asyncio.Lock()
instance-attribute
¶
global_reset = 0.0
instance-attribute
¶
start
async
¶
Start the HTTP session.
close
async
¶
Gracefully stop all workers and close the HTTP session.
request
async
¶
Queue a request for the given endpoint.
Parameters:
-
method(str) –HTTP method (e.g., POST, GET, DELETE, PATCH, etc.)
-
endpoint(str) –Discord endpoint (e.g., /channels/123/messages)
-
data(dict | None, default:None) –relevant data
-
params(dict | None, default:None) –relevant query params
-
files(Any | None, default:None) –relevant files
Returns:
-
Future | None–result or promise of request or None if failed
_worker
async
¶
Background worker that processes requests for this endpoint.
Parameters:
-
endpoint(str) –the endpoint to receive requests
_sleep_endpoint
async
¶
Let an endpoint sleep for the designated reset_after seconds.
Parameters:
-
endpoint(str) –endpoint to sleep
-
bucket(Bucket) –endpoint's bucket info
_check_global_rate_limit
async
¶
Checks if the global rate limit is after now (active).
_parse_response
async
¶
Parse the request's response for response details.
Parameters:
-
resp(ClientResponse) –the response object
Raises:
-
DiscordError–Error object for pretty printing if an error is returned.
Returns:
-
str | dict | None–request info (if any)
_update_bucket_rate_limit
async
¶
Update the bucket for this endpoint and sleep if necessary.
Parameters:
-
resp(ClientResponse) –the response object
-
bucket_id(str) –bucket ID provided by Discord's headers
-
endpoint(str) –endpoint in which request was sent
_prepare_payload
async
¶
Prepares the payload based on RequestItem.
Parameters:
-
item(RequestItem) –the request object
Returns:
-
dict–kwargs to pass to session.request
_send
async
¶
Core HTTP request executor.
Parameters:
-
item(RequestItem) –request object
Returns:
-
dict | str | None–Parsed JSON response if available, raw text if the response is not JSON, or None for HTTP 204 responses.