Skip to main content

Asyncio fork of the Python client for prismic.io

Project description

Asyncio alternative of the Python client for prismic.io

Latest Version Build Status

Disclaimer: I am not affiliated with the Prismic team and do not provide professional support.

The Async paradigm and the async/await syntax (introduced in version 3.5) are Python code styles that can be used to write concurrent coroutines. The support for this code style is still sparse in the Python community's libraries.

You may find that the existing Prismic Python SDK, based on the requests library cannot be used with the async/await syntax. Hence its architecture is not optimal for use with natively async webservers like sanic or tornado.

The emerging standard HTTP client for the Python asyncio community is httpx, which the current package uses.

Getting started

Install the kit for your project

Simply run:

pip install prismic-httpx

Get started with prismic.io

You can find out how to get started with prismic.io on our prismic.io developer's portal.

Get started using the kit

Also on our prismic.io developer's portal, on top of our full documentation, you will:

Kit's detailed documentation

You can find the documentation of the Python kit right here: http://prismic.readthedocs.org/en/latest/

Here is a basic example of use: (You can directly run it in a IPython console or in an async Python console: python -m asyncio)

>>> import prismic
>>> async with prismic.get("http://your-repo.prismic.io/api", "access_token") as api:
...     doc = await api.get_by_uid("speculoos-macaron")
>>> doc.get_text("product.name")
u'Speculoos Macaron'

Using Memcached (or any other cache)

By default, the kit will use a basic in-memory cache.

For use of more advanced caches (Redis, memcached), see aiocache

For caching the requests in memory:

>>> from aiocache import Cache
>>> async with prismic.get("http://your-rep.prismic.io/api", "access_token", Cache(Cache.MEMORY)) as api:
...     [...]

Note: The official asyncio library currently provides no support of files I/O the ShelveCache object might be introduced again in the future using aiofiles if I get time.

Using a Custom Request Handler

By default, the kit will use an httpx client httpx. You can override the client parameters in this way:

>>> import prismic
...
>>> headers = {'X-Auth': 'from-client'}
>>> params = {'client_id': 'client1'}
>>> client_kwargs = {headers:headers, params:params}
...
>>> async with prismic.get("http://your-repo.prismic.io/api", "access_token", **client_kwargs) as api:
...     doc = await api.get_by_uid("speculoos-macaron")
>>> doc.get_text("product.name")
u'Speculoos Macaron'

You can also monkey patch the client itself with your own in such a way:

>>> import prismic
>>> import httpx
>>> headers = {'X-Auth': 'from-client'}
>>> params = {'client_id': 'client1'}
>>> with httpx.AsyncClient(headers=headers, params=params) as client:
...    api = prismic.get_with_client("http://your-rep.prismic.io/api", "access_token", client=client)
...    [...]

Changelog

Need to see what changed, or to upgrade your kit? We keep our changelog on this repository's "Releases" tab.

Contribute to the kit

Contribution is open to all developer levels

Install the kit locally

This kit gets installed like any Python library.

Test

Please write tests for any bugfix or new feature.

If you find existing code that is not optimally tested and wish to make it better, we really appreciate it; but you should document it on its own branch and its own pull request.

You can launch tests using: pytest test.

Documentation

Please document any bugfix or new feature.

If you find existing code that is not optimally documented and wish to make it better, we really appreciate it; but you should document it on its own branch and its own pull request.

Licence

This software is licensed under the Apache 2 license, quoted below.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prismic-httpx-0.0.2.tar.gz (32.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page