Simperium同步平台的Python3客户端
Simperium3的Python项目详细描述
西帕里姆Python3
simperium是一种简单的方法,开发人员可以在数据更改时立即移动数据 而且是自动的。这是python库。你可以browse the documentation。
对于托管版本的simperium,您可以sign up。在那里 是用于other languages的simperium库吗 我也是。
这还不是用于分析差异和更改的完整simperium库。是一个 我们HTTP API的包装 脚本和基本后端开发。
关于这个叉子
这是Python3的叉子 https://github.com/Simperium/simperium-python。
由塞缪尔·华莱奇负责搬运和维护。
在最初的python 2版本中进行了以下更改:
- 2to3
- 使用
requests
而不是urllib - 不要捕获任何http错误;客户端可以处理这些错误
- 添加类型提示
- 使用pipfile管理deps
- 更新setup.py
安装
pip install git+https://github.com/swalladge/simperium-python3.git#egg=Simperium3
开发
使用pipenv。使用以下命令安装环境和DEPS:
make install
测试(使用pytest的单元测试和使用mypy的类型检查):
将以下内容放入.env
(与实际值一起):
export SIMPERIUM_CLIENT_TEST_APPNAME="foo-bar-123"
export SIMPERIUM_CLIENT_TEST_APIKEY="<app api key>"
然后:
make tests
统一格式化所有代码:
pipenv run pip install black
make fmt
(如果black在dev依赖项中,pipenv会惊慌失措,因为它是一个预发行版 版本。)
示例
在examples/
目录中包含了许多示例。像这样运行它们:
pipenv run python examples/simpletodo list app-name-123 myusertoken
等
开始
首先登录https://simperium.com并 创建新应用程序。复制新应用的名称、api密钥和管理密钥。
下一步安装python客户端:
$ sudo pip install git+https://github.com/Simperium/simperium-python.git
启动python并导入lib:
$ python
>>> from simperium.core import Auth, Api
我们需要创建一个能够存储数据的用户:
>>> auth = Auth(yourappname, yourapikey)
>>> token = auth.create('joe@example.com', 'secret')
>>> token
'25c11ad089dd4c18b84f24bc18c58fe2'
我们现在可以从simperium中存储和检索数据。数据存储在存储桶中。 例如,我们可以将todo项的列表存储在todo bucket中。当你 存储项时,需要给它们一个唯一的标识符。uuid通常是 不错的选择。
>>> import uuid
>>> api = Api(yourappname, token)
>>> todo1_id = uuid.uuid4().hex
>>> api.todo.post(todo1_id,
{'text': 'Read general theory of love', 'done': False})
我们可以检索此项目:
>>> api.todo.get(todo1_id)
{'text': 'Read general theory of love', 'done': False}
存储另一个待办事项:
>>> api.todo.post(uuid.uuid4().hex,
{'text': 'Watch battle royale', 'done': False})
您可以检索所有存储桶项的索引:
>>> api.todo.index()
{
'count': 2,
'index': [
{'id': 'f6b680f8504c4e31a0e54a95401ffca0', 'v': 1},
{'id': 'c0d07bb7c46e48e693653425eca93af9', 'v': 1}],
'current': '4f8507b8faf44720dfc432b1',}
检索索引中的所有文档:
>>> [api.todo.get(x['id']) for x in api.todo.index()['index']]
[
{'text': 'Read general theory of love', 'done': False},
{'text': 'Watch battle royale', 'done': False}]
还可以获取索引中data=true的每个文档的数据:
>>> api.todo.index(data=True)
{
'count': 2,
'index': [
{'id': 'f6b680f8504c4e31a0e54a95401ffca0', 'v': 1,
'd': {'text': 'Read general theory of love', 'done': False},},
{'id': 'c0d07bb7c46e48e693653425eca93af9', 'v': 1,
'd': {'text': 'Watch battle royale', 'done': False},}],
'current': '4f8507b8faf44720dfc432b1'}
若要更新项目中的字段,请发布更新的字段。他们会合并的 使用当前文档:
>>> api.todo.post(todo1_id, {'done': True})
>>> api.todo.get(todo1_id)
{'text': 'Read general theory of love', 'done': True}
simperium项是版本化的。有可能回到过去找回 以前版本的文档:
>>> api.todo.get(todo1_id, version=1)
{'text': 'Read general theory of love', 'done': False}
当然,您可以删除项目:
>>> api.todo.delete(todo1_id)
>>> api.todo.get(todo1_id) == None
True
>>> api.todo.index()['count']
1
许可证
simperium python库在mit许可下可免费和商业使用。