qgenda rest api的python客户端
python-qgenda的Python项目详细描述
/$$$$$$$ /$$ /$$ | $$__ $$ | $$ | $$ | $$ \ $$ /$$ /$$ /$$$$$$ | $$$$$$$ /$$$$$$ /$$$$$$$ | $$$$$$$/| $$ | $$|_ $$_/ | $$__ $$ /$$__ $$| $$__ $$ | $$____/ | $$ | $$ | $$ | $$ \ $$| $$ \ $$| $$ \ $$ | $$ | $$ | $$ | $$ /$$| $$ | $$| $$ | $$| $$ | $$ | $$ | $$$$$$$ | $$$$/| $$ | $$| $$$$$$/| $$ | $$ |__/ \____ $$ \___/ |__/ |__/ \______/ |__/ |__/ /$$ | $$ | $$$$$$/ \______/ /$$$$$$ /$$$$$$ /$$ /$$__ $$ /$$__ $$ | $$ | $$ \ $$| $$ \__/ /$$$$$$ /$$$$$$$ /$$$$$$$ /$$$$$$ | $$ | $$| $$ /$$$$ /$$__ $$| $$__ $$ /$$__ $$ |____ $$ | $$ | $$| $$|_ $$| $$$$$$$$| $$ \ $$| $$ | $$ /$$$$$$$ | $$/$$ $$| $$ \ $$| $$_____/| $$ | $$| $$ | $$ /$$__ $$ | $$$$$$/| $$$$$$/| $$$$$$$| $$ | $$| $$$$$$$| $$$$$$$ \____ $$$ \______/ \_______/|__/ |__/ \_______/ \_______/ \__/
说明
一个简单的python包,用于促进与qgenda的rest api的交互。
概述
python qgenda是一个与qgenda的rest api交互的客户端库。它提供了一些现成的好东西 您喜欢自动身份验证和身份验证存储。
只实现了get方法,因此如果需要更新/删除,就必须扩展api。 可以找到正式的qgenda api文档here。
安装
pip install python-qgenda
设置
当然,你需要有一个qgenda的api帐户才能让这些东西正常工作。你会 希望有一个如下所示的配置文件:
[qgenda] company_key = YOUR-COMPANY-KEY username = API-USERNAME password = API-PASSWORD api_url = https://api.qgenda.com/ documentation = http://restapi.qgenda.com api_version = v2 ; you can use redis or memcached, but you ; don't have to use caching at all if you don't want to. cache_backend = cache_host = 127.0.0.1 cache_port = 6379 cache_lifetime = 600 ; in seconds debug = 0
简单用法
登录
您可以手动登录以测试您的凭据,但此库保持对客户端的身份验证 这是自动的,这样你知道你的证书有效后就不用担心了。
importos# tell configparser where to look for configos.environ['QGENDA_CONF_FILE']='/path/to/qgenda.conf'# optionalos.environ['QGENDA_CONF_REGION']='name_of_region'# defaults to qgendafromqgenda.apiimportclientclient=client.QGendaClient()client.authenticate()
基础知识
每个方法都从请求库返回一个response对象,因此由您来处理json(或错误) 出来吧。
importjsonodata_kwargs={"$select":"StartDate,EndDate,StaffLName"}api_response=client.get_schedule(start_date='2019-01-01',odata_kwargs=odata_kwargs)# the response is now in a dictionary for easy consumptionresponse_dict=json.loads(api_response.text)print(json.dumps(response_dict[0],indent=4))
- 输出
{ "StaffLName": "Holmes K", "EndDate": "2019-01-01T00:00:00", "StartDate": "2019-01-01T00:00:00" }
获取方法示例
每个get方法都有可选的odata参数,允许您排序、筛选, 或者限制从api中提取的数据。每种get方法都不同, 因此,您需要查看官方的QGenda API docs以了解更多有关此的详细信息。
qgendaclient.get_schedule
# odata is completely optional, but pretty useful.odata_kwargs={"$select":"StartDate,EndDate,StaffLName","$orderby":"StartDate","$filter":"startswith(StaffLName, 'H')"}api_response=client.get_schedule(start_date='2019-01-01',end_date='2019-01-14',odata_kwargs=odata_kwargs)response_dict=json.loads(api_response.text)print(json.dumps(response_dict[:2],indent=4))
- 输出
[ { "StaffLName": "Holmes K", "EndDate": "2019-01-01T00:00:00", "StartDate": "2019-01-01T00:00:00" }, { "StaffLName": "Hoover", "EndDate": "2019-01-01T00:00:00", "StartDate": "2019-01-01T00:00:00" } ]
qgendaclient.get_设施
在编写本指南时,尝试对空请求使用odata会导致 请求响应。在使用api时,您可能需要记住这一点。
odata_kwargs={'$select':'Name,ID',}api_response=client.get_facility()response_dict=json.loads(api_response.text)# looks like there aren't any yet.print(json.dumps(response_dict[:2],indent=4))
qgendaclient.get_timeevent
api_response=client.get_timeevent(start_date='2019-01-01')response_dict=json.loads(api_response.text)# looks like there aren't any yet.print(json.dumps(response_dict[:2],indent=4))
qgendaclient.获取数据库
api_response=client.get_dailycase(start_date='2019-01-01')response_dict=json.loads(api_response.text)# looks like there aren't any yet.print(json.dumps(response_dict[:2],indent=4))
高级
缓存身份验证
客户端将其身份验证令牌保存在缓存中,因此不需要在实例之间重新进行身份验证,除非您的令牌 到期。redis和python memcached是目前唯一受支持的缓存后端。使用以下配置
redis
您需要在您的环境中安装redis并运行redis服务器。
pip install redis
- 配置
[qgenda] company_key = YOUR-COMPANY-KEY username = API-USERNAME password = API-PASSWORD api_url = https://api.qgenda.com/ documentation = http://restapi.qgenda.com api_version = v2 cache_backend = redis cache_host = 127.0.0.1 cache_port = 6379 cache_lifetime = 600 ; in seconds debug = 0
memcached
您需要在环境中安装python memecached并运行memcached服务器。
pip install python-memcached
- 配置
[qgenda] company_key = YOUR-COMPANY-KEY username = API-USERNAME password = API-PASSWORD api_url = https://api.qgenda.com/ documentation = http://restapi.qgenda.com api_version = v2 cache_backend = memcached cache_host = 127.0.0.1 cache_port = 11211 cache_lifetime = 600 ; in seconds debug = 0