数据源Python SDK
bocks-ds的Python项目详细描述
数据源python
所有数据源迭代的Python SDK
安装
通过pip安装此软件包:
pipinstallbocks_ds
使用
客户
导入这个包并实例化一个客户端对象。在
^{pr2}$错误示例:
try:bad_client=Client("bad_client")exceptDSTargetErrorase:print(e)# The target 'bad_client' provided in Client initialization is not in available target names:\n['starfinder', 'pathfinder']
正在获取数据
在获取数据时,目前只有两个选项需要考虑:类型和参数。在
“类型”可以看作是一个数据表,不过我们稍后将讨论一些复杂性。在
response=ds_client.armor.get(['name','price'])# 'armor' here is the query typeifresponse.status_code==200:# optional status checkdata=response.json()# '.json()' gives the API data output we came for
请注意,ds_client
将接受any value作为属性,然后使用该属性来编写API请求。在
错误示例:
try:client.bad_name.get(['name']).json()exceptDSQueryErrorase:print(e)# <Response 400> DataSource did not find table/field 'bad_name'.
完善您的请求
API允许在请求中主动过滤字符串和整数。为此,您需要在发出请求之前立即设置参数。在
对于返回字符串的数据值,可以使用术语<value_name>_like
和<value_name>_is
进行优化。对于“like”查询,不考虑排序,搜索也不区分大小写。对于“is”查询,只返回精确的、区分大小写的匹配项。在
对于返回整数的数据值,可以使用<value_name>_min
、<value_name>_max
和<value_name>_equals
进行优化。它们分别等于“大于或等于”、“小于或等于”和“等于”。在
最后,通过它的ID
来选择一个项通常是有价值的,它的功能比整数查询更直接(见下面的示例)。在
所有参数必须作为一个字典来表示,如下例所示。在
client.armor.set_arguments({"name_like":"basic"})response=ds_client.armor.get(['name','price'])
client.armor.set_arguments({"price_min":200,"price_max":2000})response=ds_client.armor.get(['name','price'])
client.armor.set_arguments({"id":1})response=ds_client.armor.get(['name','price'])
请注意,这些请求不会堆叠,但您可以将所有术语放入一个字典中,以进一步细化结果。如果多次调用set_arguments
,则最后一次调用将覆盖以前的调用。在
query={"name_like":"basic","price_min":200,"price_max":2000}client.armor.set_arguments(query)response=ds_client.armor.get(['name','price'])
最后一点:调用get
时,所有参数都将被清除。在
错误示例
try:client.armor.set_arguments({"name_min":200})erroneous_armor=client.armor.get(['name','price'])# Throws exception due to errors in response.json()exceptDSQueryErrorase:print(e)# <Response 400> ['Unknown argument "name_min" on field "Query.armor". Did you mean "name_is", "name_like", "type_min", "bulk_min", or "level_min"?']
获取嵌套数据
如前所述,并非所有类型都精确匹配其数据表。某些类型包括来自与其他表的关系的附加数据。这些关系的文档是在API文档中自动生成的(例如at docs.sfdatasource.com)。在
为了访问嵌套数据,需要提供一个描述要获取的数据的字典。在
以前我们提供了要查询的字符串字段名列表,现在将在该列表中包括一个字典,如下面的示例所示。在
query=["name",{"effect_ranges":["name","description"],}]all_spells=ds_client.spells.get(query)
此逻辑是递归的,因此,如果关系目标具有自己的关系,则可以执行以下操作:
query=["name",{"class_proficiencies":['name'],"class_features":["name",{"class_special_skills":['name']}],}]spells=client.classes.get(query)
- 项目
标签: