This Pull Request introduces the upcoming Algolia API Client Python v2.
-
Follows 100% of the API Client v2 specs, just like PHP v2, c# v2: contains waitable response objects, up-to-date retry strategy,
replace_all_objects
,clear_objects
, and more! -
Supports Python: 2.7 🥳, 3.4, 3.5, 3.6, and 3.7.
-
Strong test suite, 100% test coverage on
unittest
(tests === 30secs locally! 🏎),flake8
(linter), andmypy
(static-analysis) to ensure the quality of the code. -
Supports synchronous and asynchronous environments. 👉🏻 Yes, in the same client and with support Python 2. It's amazing! Asynchronous methods are available using the
async
suffix:
synchronous | asynchronous |
---|---|
search | search_async |
save_objects | save_objects_async |
... | ... |
Thank you for considering to contribute to Algolia's API Client Python v2. Here is the list of tasks that I would love to get your help:
- Using Python v1 within Algolia for non-critical projects? Start using this v2 today! Found a bug? Report it here: https://github.com/algolia/algoliasearch-client-python.
- Have experience in Python? Check if the code/structure Pythonic
- Coming from a robust language? Take a moment to analyze the code quality
- Love testing like me? Take a moment to analyze the quality of tests
- Create a free account at Algolia, and play with this client.
Requires:
First, use Homebrew to install Python 3.7:
# Install Python 3
brew install python3
# Create your Python project directory
mkdir my-new-project
cd my-new-project
# Create a Python Virtual Environment inside your directory
python3 -m venv venv
# Activate the Python Virtual Environment
source venv/bin/activate
# At any time, use the following command to deactivate it
deactivate
Finally, install algoliasearch
- API Client Python v2:
pip install --pre algoliasearch
import os
from algoliasearch.search_client import SearchClient
from algoliasearch.exceptions import AlgoliaException
client = SearchClient.create(
os.environ.get('ALGOLIA_APPLICATION_ID_1'),
os.environ.get('ALGOLIA_ADMIN_KEY_1')
)
index = client.init_index('articles')
index.save_objects([
{'objectID': 1, 'firstname': 'Jimmie', 'lastname': 'Barninger'},
{'objectID': 2, 'firstname': 'Warren', 'lastname': 'Speach'}
]).wait()
hits = index.search('Jimmie')
print(hits)
First, require asynchronous libraries:
pip install 'asyncio>=3.4,<4.0' 'aiohttp>=2.0,<4.0' 'async_timeout>=2.0,<4.0'
import asyncio
import os
from algoliasearch.search_client import SearchClient
from algoliasearch.exceptions import AlgoliaException
from algoliasearch.responses import MultipleResponse
app_id = os.environ.get('ALGOLIA_APPLICATION_ID_1')
api_key = os.environ.get('ALGOLIA_ADMIN_KEY_1')
async def main():
async with SearchClient.create(app_id, api_key) as client:
index = client.init_index('articles')
try:
(await index.clear_objects_async()).wait()
except AlgoliaException: # Index not found
pass
results = await asyncio.gather(
index.save_object_async({'objectID': 1, 'foo': 'bar'}),
index.save_object_async({'objectID': 2, 'foo': 'foo'})
)
MultipleResponse(results).wait()
print(await index.search_async(''))
asyncio.run(main())