Skip to content

Latest commit

 

History

History
39 lines (32 loc) · 2.96 KB

File metadata and controls

39 lines (32 loc) · 2.96 KB

Fixes to Looker's SDK

Looker's python_SDK is generated by swagger, which generates an up to date set of API classes and functions based on a JSON file that you can get from your Looker instance. However, there are a number of problems with the SDK that can be hard to troubleshoot. Here is a quick guide of the most common problems.

1. The Query() class saves some python objects in the wrong format

The Problem

  • If you make a call that returns a Query() object, such as query_api.query() or dashboard_api.dashboard_dashboard_elements() you will find that there are problems with some of the attributes:
    • Dynamic fields (table calculations) should be saved as JSON, but instead appear as a list of strings:
      • e.g. ['[', '{', '"', 't', 'a', 'b', 'l', 'e', '_', 'c', 'a', 'l', 'c', 'u', 'l', 'a', 't', 'i', 'o', 'n', '"', ':', ..., ']'
    • Vis_config should be saved as JSON, but instead the values are interpreted as strings:
      • e.g. [{"some_key":"True", "other_key": "['list', 'items']"} instead of [{"some_key":True, "other_key": ['list', 'items']}

The fix

  • The simplest fix is to change how the SDK interprets these objects.
  • Go to python_sdk/looker/models/query.py and change self.swagger_types (lines 39-67):
    • Change it to 'vis_config': 'object' (line 55) and 'dynamic_fields': 'object' (line 59)
  • Note there is a chance this may cause unexpected behaviour elsewhere in the SDK
  • Note changes made to the SDK will be overwritten next time you regenerate the swagger code
  • You may not wish to change the SDK itself, but instead convert these objects in your own code:

Manually changing dynamic fields

  • First concatenate all of the strings: new_string = ''.join([s for s in list_of_strings])
  • Then parse it into json using json.loads(new_string)
  • Note that this won't work on NoneType objects, so only apply this if dynamic_fields is not None

Manually changing vis_config

  • Iterate through the vis config dict and tell python to correctly convert any dict or list that has been saved as a string
  • The fastest way to do this is to tell python to literally evaluate the strings, converting them to objects
  • Note eval can be dangerous and should only be used on code you know is not malicious. You can also use from ast import literal_eval which is safer
for k, v in vis_config_object.items():
    if len(v) > 0 and (v[0] in ['[', '{'] or v in ['True', 'False'] or str.isdigit(v)):
        v = eval(v)

2. Allow requests without verifying SSL certificates (e.g. making API calls to a locally-hosted version of looker for testing)

IMPORTANT! This poses a security risk. Only use this if you are making calls to a local looker server that you manage yourself and trust

  • Go to python_sdk/looker/configuration.py and change line 93 from self.verify_ssl = True to self.verify_ssl = False