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.
Simply run:
pip install prismic-httpx
You can find out how to get started with prismic.io on our prismic.io developer's portal.
Also on our prismic.io developer's portal, on top of our full documentation, you will:
- get a thorough introduction of how to use prismic.io kits, including this one.
- see what else is available for Python: starter projects, examples, ...
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'
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.
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)
... [...]
Need to see what changed, or to upgrade your kit? We keep our changelog on this repository's "Releases" tab.
Contribution is open to all developer levels
This kit gets installed like any Python library.
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
.
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.
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.