Session Interface

The Session and AsyncSession are abstract classes that act as adapters to underlying HTTP client libraries. A session instance is used by Client or AsyncClient to perform all the HTTP requests to the Yandex.Disk API.

These interfaces can be implemented to add support for any HTTP library. For a concrete example, see the source code of any existing implementation (e.g. HTTPXSession).

Synchronous

class yadisk.Session[source]

HTTP session class. Maintains open connections, stores headers and some other request parameters.

Must be explicitly closed (can be done using the with statement).

close() None[source]

Closes the session.

Note

This is an abstract method that needs to be implemented.

send_request(method: Literal['GET'] | Literal['POST'] | Literal['PUT'] | Literal['PATCH'] | Literal['DELETE'] | Literal['OPTIONS'] | Literal['HEAD'] | Literal['CONNECT'] | Literal['TRACE'], url: str, *, params: dict[str, Any] | None = None, data: bytes | Iterator[bytes] | None = None, timeout: float | tuple[float | None, float | None] | None = None, stream: bool = False, **kwargs) Response[source]

Sends an HTTP request with given parameters. In case an error occurs, the method should throw one of exceptions derived from YaDiskError. Additional keyword arguments may be passed, they may be forwarded to the underlying HTTP client without modification.

Note

This is an abstract method that needs to be implemented.

Parameters:
  • methodstr, HTTP method

  • urlstr, URL

  • paramsdict, GET parameters

  • databytes, an iterator or a file-like object, data to be sent in the request body

  • headersdict, additional headers to be set

  • timeout – request timeout, a tuple of (read timeout, connect timeout), float or None (no timeout)

  • streambool, if False, the response content will be immediately downloaded

Returns:

Response, response object

set_headers(headers: dict[str, str]) None[source]

Updates session’s headers.

Note

This is an abstract method that needs to be implemented.

Parameters:

headers – dictionary of headers to be set

set_token(token: str) None[source]

Sets token for the session by setting the Authorization header.

Parameters:

tokenstr, API token

class yadisk.Response[source]

Represents an HTTP response.

In case an error occurs, methods of this class should throw one of exceptions derived from YaDiskError.

Variables:

statusint, HTTP status code

close() None[source]

Closes the response and releases the underlying connection into the pool

Note

This is an abstract method that needs to be implemented.

download(consume_callback: Callable[[bytes], None]) None[source]

Downloads response’s content.

Note

This is an abstract method that needs to be implemented.

Parameters:

consume_callback – function, takes one parameter - chunk of data (bytes), consumes the chunk (e.g. by writing to a file)

get_exception() YaDiskError[source]

Convenience wrapper for yadisk.utils.get_exception.

json() dict | list | str | int | float | None[source]

Returns JSON-content of the response (parses JSON).

Note

This is an abstract method that needs to be implemented.

Returns:

dict, list, str, int, float or None

Asynchronous

class yadisk.AsyncSession[source]

HTTP session class. Maintains open connections, stores headers and some other request parameters.

Must be explicitly closed (can be done using the with statement).

async close() None[source]

Closes the session.

Note

This is an abstract method that needs to be implemented.

async send_request(method: Literal['GET'] | Literal['POST'] | Literal['PUT'] | Literal['PATCH'] | Literal['DELETE'] | Literal['OPTIONS'] | Literal['HEAD'] | Literal['CONNECT'] | Literal['TRACE'], url: str, *, params: dict[str, Any] | None = None, data: bytes | Iterator[bytes] | AsyncIterator[bytes] | None = None, timeout: float | tuple[float | None, float | None] | None = None, stream: bool = False, **kwargs) AsyncResponse[source]

Sends an HTTP request with given parameters. In case an error occurs, the method should throw one of exceptions derived from YaDiskError. Additional keyword arguments may be passed, they may be forwarded to the underlying HTTP client without modification.

Note

This is an abstract method that needs to be implemented.

Parameters:
  • methodstr, HTTP method

  • urlstr, URL

  • paramsdict, GET parameters

  • databytes, iterator (possibly async) or a file-like object (possible async), data to be sent in the request body

  • headersdict, additional headers to be set

  • timeout – request timeout, a tuple of (read timeout, connect timeout), float or None (no timeout)

  • streambool, if False, the response content will be immediately downloaded

Returns:

Response, response object

set_headers(headers: dict[str, str]) None[source]

Updates session’s headers.

Note

This is an abstract method that needs to be implemented.

Parameters:

headers – dictionary of headers to be set

set_token(token: str) None[source]

Sets token for the session by setting the Authorization header.

Parameters:

tokenstr, API token

class yadisk.AsyncResponse[source]

Represents an HTTP response.

In case an error occurs, methods of this class should throw one of exceptions derived from YaDiskError.

Variables:

statusint, HTTP status code

async close() None[source]

Closes the response and releases the underlying connection into the pool.

Note

This is an abstract method that needs to be implemented.

async download(consume_callback: Callable[[bytes], None] | Callable[[bytes], Awaitable[None]]) None[source]

Downloads response’s content.

Note

This is an abstract method that needs to be implemented.

Parameters:

consume_callback – regular or async function, takes one parameter - chunk of data (bytes), consumes the chunk (e.g. by writing to a file)

async get_exception() YaDiskError[source]

Convenience wrapper for yadisk.utils.get_exception.

async json() dict | list | str | int | float | None[source]

Returns JSON-content of the response (parses JSON).

Note

This is an abstract method that needs to be implemented.

Returns:

dict, list, str, int, float or None