Package: bocks-ds
datasource-pythonPython SDK for all DataSource iterationsInstallationInstall this package via pip:pipinstallbocks_dsUsageThe ClientImport this package and instantiate a client object.importbocks_dsds_client=bocks_ds.Client("starfinder")# provide the name of the datasource to accessError Example: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']Fetching DataThere are currently only two options to consider when fetching data: the type and the parameters.The "type" can be thought of as a data table, though we will discuss some complexities later on.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 forNote that theds_clientwill acceptany valueas an attribute, which it will then use to craft an API request.Error Example:try:client.bad_name.get(['name']).json()exceptDSQueryErrorase:print(e)# <Response 400> DataSource did not find table/field 'bad_name'.Refining Your RequestThe API allows for active filtering of strings and integers within your request. To do this, you'll need to set arguments immediately prior to making the request.For data values that return strings, you may refine with the terms<value_name>_likeand<value_name>_is. For "like" queries, sequencing is not considered and the search is not case sensitive. For "is" queries, only exact, case-sensitive matches will be returned.For data values that return integers, you may refine with the terms<value_name>_min,<value_name>_maxand<value_name>_equals. These equate to "greater than or eqal to", "less than or equal to", and "equal to", respectively.Finally, it is often valuable to select an item specifically by itsID, which functions in a more direct manner than the queries for integers (see example below).All arguments must be presented as a single dictionary, as seen in the examples below.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'])Note that these requests do not stack, but you can place all terms into a single dictionary to futher refine results. Ifset_argumentsis called multiple times, the final call will overwrite previous calls.query={"name_like":"basic","price_min":200,"price_max":2000}client.armor.set_arguments(query)response=ds_client.armor.get(['name','price'])As a final note: all arguments are cleared whengetis called.Error Exampletry: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"?']Fetching Nested DataAs mentioned previously, not all types match their data tables precisely. Some types include additional data from relationships with other tables. Documentation for these relationships is automatically generated in the API Documentation (such as atdocs.sfdatasource.com).In order to access nested data, it is required that you present a dictionary describing the data to fetch.Where we previously provided a list of string field-names to query, we will now include a dictionary in that list, as seen in the examples below.query=["name",{"effect_ranges":["name","description"],}]all_spells=ds_client.spells.get(query)This logic is recursive, so in the event that a relationship target has it's own relationships you may do the following:query=["name",{"class_proficiencies":['name'],"class_features":["name",{"class_special_skills":['name']}],}]spells=client.classes.get(query)