contentful.py icon indicating copy to clipboard operation
contentful.py copied to clipboard

Implement asyncio support, plus session optimizations

Open seandstewart opened this issue 1 year ago • 5 comments

This change adds support for asyncio. In order to do so, I made the following changes:

  1. Isolated the use of the libraries behind a "Transport" abstraction.
  2. Integrated errors and error handling within the "Transport" implementations.
  3. Split the client implementation into BaseClient, Client, and AsyncClient.
  4. Transports are designed to re-use a Session (connection pool), which each library provides. This is much more efficient than the top-level functions in requests or re-creating a session for every call.
  5. Transports automatically handle deserialization from JSON and have default error handling for non-HTTP errors.
  6. Adds tests for the new AsyncClient
  7. Migrates the packaging to a modern toolchain for future-proofing (pyproject.toml, Poetry)

resolves #86

seandstewart avatar Feb 22 '24 22:02 seandstewart

@rubydog I'd appreciate a review when you have time, this is a major blocker for my company's onboarding. Here is the test output below:

❯ make test-all
tox
.pkg: _optional_hooks> python /Users/seanstewart/PycharmProjects/contentful.py/venv/lib/python3.9/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
.pkg: get_requires_for_build_sdist> python /Users/seanstewart/PycharmProjects/contentful.py/venv/lib/python3.9/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
.pkg: get_requires_for_build_wheel> python /Users/seanstewart/PycharmProjects/contentful.py/venv/lib/python3.9/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
.pkg: prepare_metadata_for_build_wheel> python /Users/seanstewart/PycharmProjects/contentful.py/venv/lib/python3.9/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
.pkg: build_sdist> python /Users/seanstewart/PycharmProjects/contentful.py/venv/lib/python3.9/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
py38-flakes: install_package> python -I -m pip install --force-reinstall --no-deps /Users/seanstewart/PycharmProjects/contentful.py/.tox/.tmp/package/33/contentful-2.1.1.tar.gz
py38-flakes: commands[0]> python -m unittest discover tests
.................................................................................................................................................................
----------------------------------------------------------------------
Ran 161 tests in 0.819s

OK
py38-flakes: OK ✔ in 2.25 seconds
py39-flakes: install_package> python -I -m pip install --force-reinstall --no-deps /Users/seanstewart/PycharmProjects/contentful.py/.tox/.tmp/package/34/contentful-2.1.1.tar.gz
py39-flakes: commands[0]> python -m unittest discover tests
.................................................................................................................................................................
----------------------------------------------------------------------
Ran 161 tests in 0.415s

OK
py39-flakes: OK ✔ in 1.98 seconds
py310-flakes: install_package> python -I -m pip install --force-reinstall --no-deps /Users/seanstewart/PycharmProjects/contentful.py/.tox/.tmp/package/35/contentful-2.1.1.tar.gz
py310-flakes: commands[0]> python -m unittest discover tests
.................................................................................................................................................................
----------------------------------------------------------------------
Ran 161 tests in 0.431s

OK
py310-flakes: OK ✔ in 1.5 seconds
py311-flakes: install_package> python -I -m pip install --force-reinstall --no-deps /Users/seanstewart/PycharmProjects/contentful.py/.tox/.tmp/package/36/contentful-2.1.1.tar.gz
py311-flakes: commands[0]> python -m unittest discover tests
.................................................................................................................................................................
----------------------------------------------------------------------
Ran 161 tests in 0.355s

OK
py311-flakes: OK ✔ in 1.46 seconds
py312-flakes: install_package> python -I -m pip install --force-reinstall --no-deps /Users/seanstewart/PycharmProjects/contentful.py/.tox/.tmp/package/37/contentful-2.1.1.tar.gz
py312-flakes: commands[0]> python -m unittest discover tests
.................................................................................................................................................................
----------------------------------------------------------------------
Ran 161 tests in 0.304s

OK
py312-flakes: OK ✔ in 2.54 seconds
pypy3-flakes: install_package> python -I -m pip install --force-reinstall --no-deps /Users/seanstewart/PycharmProjects/contentful.py/.tox/.tmp/package/38/contentful-2.1.1.tar.gz
pypy3-flakes: commands[0]> python -m unittest discover tests
.............Executing <Task pending name='Task-25' coro=<IsolatedAsyncioTestCase._asyncioLoopRunner() running at /Users/seanstewart/.pyenv/versions/pypy3.10-7.3.13/lib/pypy3.10/unittest/async_case.py:101> created at /Users/seanstewart/.pyenv/versions/pypy3.10-7.3.13/lib/pypy3.10/unittest/async_case.py:117> took 0.129 seconds
....................................................................................................................................................
----------------------------------------------------------------------
Ran 161 tests in 1.303s

OK
.pkg: _exit> python /Users/seanstewart/PycharmProjects/contentful.py/venv/lib/python3.9/site-packages/pyproject_api/_backend.py True setuptools.build_meta __legacy__
  py38-flakes: OK (2.25=setup[1.19]+cmd[1.05] seconds)
  py39-flakes: OK (1.98=setup[1.33]+cmd[0.65] seconds)
  py310-flakes: OK (1.50=setup[0.84]+cmd[0.67] seconds)
  py311-flakes: OK (1.46=setup[0.87]+cmd[0.59] seconds)
  py312-flakes: OK (2.54=setup[2.01]+cmd[0.53] seconds)
  pypy3-flakes: OK (3.26=setup[1.55]+cmd[1.71] seconds)
  congratulations :) (13.11 seconds)

seandstewart avatar Feb 28 '24 21:02 seandstewart

Hi @seandstewart, thanks for the PR.

I will take a look at it and try to get back to you by next week.

Cheers

rubydog avatar Mar 06 '24 03:03 rubydog

Hi @seandstewart, thanks for the PR.

I will take a look at it and try to get back to you by next week.

Cheers

@rubydog Sounds good. I've gone through and added a self-review!

seandstewart avatar Mar 06 '24 14:03 seandstewart

Hello, any status update for this PR? We're eager to try it out :) Thanks

pdelagrave avatar Apr 22 '24 15:04 pdelagrave

Hi @pdelagrave,

We are currently discussing internally whether we want to support asyncio. Given the scope of this PR, it will take me some time to review it thoroughly. Apologies for the delay.

rubydog avatar May 24 '24 16:05 rubydog