betty.fetch module - Betty 0.4.0a1 documentation

Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# betty.fetch module

Fetch content from the internet.

exception betty.fetch.FetchError

  Bases: `UserFacingError`, `RuntimeError`

  An error that occurred when fetching a URL.

  Parameters:

    message (`betty.locale.Localizable`)

class betty.fetch.FetchResponse

  Bases: `object`

  An HTTP response.

  Parameters:

    - headers (`multidict._multidict.CIMultiDict`[`str`])

    - body (`bytes`)

    - encoding (`str`)

  __init__(headers: multidict._multidict.CIMultiDict[str], body: bytes, encoding: str) → None
    Parameters:

      - headers (`multidict._multidict.CIMultiDict`[`str`])

      - body (`bytes`)

      - encoding (`str`)

  body: `bytes`
  encoding: `str`
  headers: `multidict._multidict.CIMultiDict`[`str`]
  property json: Any

    The body as JSON.

    This may raise an error if the response body cannot be represented as JSON or plain text.

  property text: str

    The body as plain text.

    This may raise an error if the response body cannot be represented as plain text.

class betty.fetch.Fetcher

  Bases: `object`

  Fetch content from the internet.

  Parameters:

    - http_client (`aiohttp.client.ClientSession`)

    - response_cache (`betty.cache.Cache`[`betty.fetch.FetchResponse`])

    - binary_file_cache (`betty.cache.file.BinaryFileCache`)

    - ttl (`int`)

  __init__(http_client: aiohttp.client.ClientSession, response_cache: betty.cache.Cache[betty.fetch.FetchResponse], binary_file_cache: betty.cache.file.BinaryFileCache, ttl: int = 604800)
    Parameters:

      - http_client (`aiohttp.client.ClientSession`)

      - response_cache (`betty.cache.Cache`[`betty.fetch.FetchResponse`])

      - binary_file_cache (`betty.cache.file.BinaryFileCache`)

      - ttl (`int`)

  async fetch(url: str) → betty.fetch.FetchResponse

    Fetch an HTTP resource.

    Parameters:

      url (`str`)

    Return type:

      `betty.fetch.FetchResponse`

  async fetch_file(url: str) → pathlib.Path

    Fetch a file.

    Return type:

      `pathlib.Path`

    Returns:

      The path to the file on disk.

    Parameters:

      url (`str`)