Get request in python script for Looker automation

Hi,

For looker automation, I need to write python script for looker API. I am able to authenticate looker api using client id and password and base_url. My question is: How and where in the script can I add get request containing different url than base_url for validation.

My script:

import lookerapi as looker

# replace with your custom Looker API Host domain and port, if applicable.
base_url = 'https://insights-ms-dev.hpcloud.hp.com:19999/api/3.1/'

#Looker admins can create API3 credentials on Looker's **Admin/Users** page
client_id = '  '
client_secret = ‘  '

# instantiate Auth API
unauthenticated_client = looker.ApiClient(base_url)
unauthenticated_authApi = looker.ApiAuthApi(unauthenticated_client)

# authenticate client
token = unauthenticated_authApi.login(client_id=client_id, client_secret=client_secret)
client = looker.ApiClient(base_url, 'Authorization', 'token ' + token.access_token)

# instantiate User API client
userApi = looker.UserApi(client)
me = userApi.me();
print(me)

# instantiate Look API client
queryAPI = looker.QueryApi(client)
body = {
  "model":"bird_mps_insights",
  "view":"customer",
  "fields":"",
  "query_timezone":"America/Los_Angeles"
}
resp = queryAPI.run_inline_query("json",body)

print(resp)

In the above code, where can I add GET request “https://insights-ms-dev.hpcloud.hp.com:19999/api/3.1/dashboards/9” for getting all dashboards.

Solved Solved
0 3 2,500
1 ACCEPTED SOLUTION

Hi @Khushboo1,


The code you pasted in your note seems to use our Legacy SDK. We have a new version that can be found here. With the new SDK, you can create an ini file to store the client id, client secret, and base URL with multiple profiles. In the init method, we can specify which profile to use for the script. Then, we would initialize both, but specify which instance we are using with the section parameter. We would create these as variables (in the example below sdk and sdk2). Then, we call the methods using sdk for our first instance and sdk2 for the second instance. 
 

### Example INI file
[somethingelse]
# API version is required
api_version=3.1
# Base URL for API. Do not include /api/* in the url
base_url=https://domain1.looker.com:19999
# API 3 client id
client_id=...
# API 3 client secret
client_secret=...
# Set to false if testing locally against self-signed certs. Otherwise leave True
verify_ssl=False

[Looker]
# API version is required
api_version=3.1
# Base URL for API. Do not include /api/* in the url
base_url=https://domain2.looker.com:19999
# API 3 client id
client_id=...
# API 3 client secret
client_secret=...
# Set to false if testing locally against self-signed certs. Otherwise leave True
verify_ssl=True


### How to initialize in the script.
sdk = looker_sdk.init40(section="Looker") # or init40() for v4.0 API
sdk2 = looker_sdk.init40(section="somethingelse")
me = sdk.me().url
me2 = sdk2.me().url

print(me)
print(me2)

print(sdk.look(look_id=123).title)
print(sdk2.look(look_id=123).title)

So, in your specific example we could follow this pattern and use  all dashboards to get all the dashboards from each instance and specific they instance by using sdk or sdk2. 
 

sdk.all_dashboards()
sdk2.all_dashboards()

Please let me know if you have any questions!

Thanks,
Eric

View solution in original post

3 REPLIES 3

Hi @Khushboo1,


The code you pasted in your note seems to use our Legacy SDK. We have a new version that can be found here. With the new SDK, you can create an ini file to store the client id, client secret, and base URL with multiple profiles. In the init method, we can specify which profile to use for the script. Then, we would initialize both, but specify which instance we are using with the section parameter. We would create these as variables (in the example below sdk and sdk2). Then, we call the methods using sdk for our first instance and sdk2 for the second instance. 
 

### Example INI file
[somethingelse]
# API version is required
api_version=3.1
# Base URL for API. Do not include /api/* in the url
base_url=https://domain1.looker.com:19999
# API 3 client id
client_id=...
# API 3 client secret
client_secret=...
# Set to false if testing locally against self-signed certs. Otherwise leave True
verify_ssl=False

[Looker]
# API version is required
api_version=3.1
# Base URL for API. Do not include /api/* in the url
base_url=https://domain2.looker.com:19999
# API 3 client id
client_id=...
# API 3 client secret
client_secret=...
# Set to false if testing locally against self-signed certs. Otherwise leave True
verify_ssl=True


### How to initialize in the script.
sdk = looker_sdk.init40(section="Looker") # or init40() for v4.0 API
sdk2 = looker_sdk.init40(section="somethingelse")
me = sdk.me().url
me2 = sdk2.me().url

print(me)
print(me2)

print(sdk.look(look_id=123).title)
print(sdk2.look(look_id=123).title)

So, in your specific example we could follow this pattern and use  all dashboards to get all the dashboards from each instance and specific they instance by using sdk or sdk2. 
 

sdk.all_dashboards()
sdk2.all_dashboards()

Please let me know if you have any questions!

Thanks,
Eric

Thanks for your help @Eric_Lyons, I am very new to development. I am not sure where to create .ini file and init method and what packages I need to import in the script.

I created .ini file where I have kept all my python scripts in looker-sdk-example folder. But when trying to run script after initialization, its giving following error-

Traceback (most recent call last):
  File "D:\Users\khushboo\pyenv-win-master\pyenv-win\bin\looker-sdk-example\Test2.py", line 1, in <module>
    import looker_sdk
  File "D:\Users\khushboo\AppData\Local\Programs\Python\Python39\lib\site-packages\looker_sdk\__init__.py", line 24, in <module>
    from looker_sdk.sdk import models  # noqa:F401
  File "D:\Users\khushboo\AppData\Local\Programs\Python\Python39\lib\site-packages\looker_sdk\sdk\models.py", line 8, in <module>
    import cattr
  File "D:\Users\khushboo\AppData\Local\Programs\Python\Python39\lib\site-packages\cattr\__init__.py", line 1, in <module>
    from .converters import Converter, GenConverter, UnstructureStrategy
  File "D:\Users\khushboo\AppData\Local\Programs\Python\Python39\lib\site-packages\cattr\converters.py", line 18, in <module>
    from ._compat import (
  File "D:\Users\khushboo\AppData\Local\Programs\Python\Python39\lib\site-packages\cattr\_compat.py", line 92, in <module>
    from typing import (
ImportError: cannot import name '_SpecialGenericAlias' from 'typing' (D:\Users\khushboo\AppData\Local\Programs\Python\Python39\lib\typing.py)

I might guess that the issue there is related to a messy python environment, since previously you had the old SDK and now have tried to install the new one. Could you try again in a clean virtual environment (https://docs.python-guide.org/dev/virtualenvs/#lower-level-virtualenv) and see if you have better luck?