Skip to main content

Looker API 3.1

Project description

The Looker SDK for Python provides a convenient way to communicate with the Looker API available on your Looker server. The library requires python3.7+ and is annotated using the typing module.

DISCLAIMER: This is a beta version of the Looker SDK, using a completely new code generator developed by Looker. Implementations are still subject to change, but we expect most SDK method calls to work correctly. If you run into problems with the SDK, please feel free to report an issue, and please indicate which language SDK you’re using in the report.

Sample project setup

In these instructions we’ll use pyenv to install python3.7 and pipenv to manage project dependencies. Here is how to install them on a mac

brew update && brew install pyenv && brew install pipenv

Create a project directory

mkdir looker-sdk-example

Install python3.7 and use it for this directory

cd looker-sdk-example/
pyenv install 3.7.4
pyenv local 3.7.4

Install looker_sdk using pipenv

$ pipenv install --pre looker_sdk

Configuring the SDK

In order to configure the SDK client, create a “looker.ini” file to reference during client.setup()

example file:

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

Code example

Copy the following code block into example.py

from looker_sdk import client, models, error

# client calls will now automatically authenticate using the
# api3credentials specified in 'looker.ini'
looker_client = client.setup("looker.ini")
looker_api_user = looker_client.me()

# models can be passed named parameters to the constructor
new_user = models.WriteUser(first_name="John", last_name="Doe")

# as well as have fields set on the instance
new_user.is_disabled = True
new_user.locale = "fr"

# create the user with the client
created_user = looker_client.create_user(new_user)
print(
    f"Created user({created_user.id}): "
    f"{created_user.display_name} "
    f"locale({created_user.locale})"
)


# Updating the user: change first_name and explicitly nullify
# locale so that it defaults to looker system locale
update_user = models.WriteUser(
    first_name="Jane", locale=models.EXPLICIT_NULL  # do not use None
)

# update the user with the client
user_id = created_user.id
updated_user = looker_client.update_user(user_id, body=update_user)
print(
    f"Updated user({user_id}): {updated_user.display_name} "
    f"locale({updated_user.locale})"
)

# perform API calls on behalf of the user: "sudo"
try:
    print(f"Sudo as {user_id}")
    looker_client.login_user(user_id)
except error.SDKError:
    print(f"Oops, we need to enable user({user_id}) first")
    looker_client.update_user(user_id, body=models.WriteUser(is_disabled=False))
    looker_client.login_user(user_id)

sudo_user = looker_client.me()
assert sudo_user.id == user_id
assert sudo_user.id != looker_api_user.id

# logout to switch back to authenticating per 'looker.ini'
looker_client.logout()
print(f"Ending sudo({user_id}) session")
assert looker_client.me().id == looker_api_user.id

# "sudo" using a context manager
with looker_client.login_user(user_id):
    assert looker_client.me().id == user_id

# exiting context manager is the same as
# calling looker_client.logout()
assert looker_client.me().id == looker_api_user.id

# cleanup
looker_client.delete_user(user_id)
print(f"Removed user({user_id})")

You can run the example code above but be aware it will actually create and delete a user in your looker instance.

pipenv run python example.py

If you see a lot of InsecureRequestWarning errors because you’re running against an instance with a self-signed cert, this will clean up the output:

PYTHONWARNINGS=ignore pipenv run python example.py

A note on static type checking

All client calls are annotated with with basic types as well as model types. Many client calls accept a fields argument which limits the JSON response from the API to the specified fields. For this reason, the all properties on the model are all typed as Optional[]. The effect is that static code analysis (mypy for example) will complain if you try to use a field from a model instance in a place that requires the value not be Optional. From the example above

created_user = looker_client.create_user(new_user)
user_id = created_user.id

# mypy error: Argument "user_id" to "update_user" of "LookerSDK"
# has incompatible type "Optional[int]"; expected "int"
looker_client.update_user(user_id, ...)

This is because created_user.id has type Optional[int] but we need to use it in the update_user() call which is annotated like this:

def update_user(
    self,
    user_id: int,  # note: not Optional[int]
    body: models.WriteUser,
    fields: Optional[str] = None,
) -> models.User:

We know that created_user.id is an int (we didn’t pass in a fields argument to create_user() excluding id from the response). However, mypy does not so we must guide it in one of the following ways

# assert about the type
assert isinstance(user_id, int)

# or cast
from typing import cast
user_id = cast(created_user.id, int)

Now mypy is happy with update_user(user_id, …)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

looker_sdk-0.1.3b1.tar.gz (85.7 kB view details)

Uploaded Source

Built Distribution

looker_sdk-0.1.3b1-py3-none-any.whl (96.8 kB view details)

Uploaded Python 3

File details

Details for the file looker_sdk-0.1.3b1.tar.gz.

File metadata

  • Download URL: looker_sdk-0.1.3b1.tar.gz
  • Upload date:
  • Size: 85.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for looker_sdk-0.1.3b1.tar.gz
Algorithm Hash digest
SHA256 5943f92c18ca721dc7a4b543bb8cc5660ef719f1ac856b31f4754c2580c6780d
MD5 79274e811ae2188f586557e7eff21f33
BLAKE2b-256 4b23a35333be9f668bab60c6fa3d996835cf0330ee16cad6e2e80f3b3aae3cbb

See more details on using hashes here.

Provenance

File details

Details for the file looker_sdk-0.1.3b1-py3-none-any.whl.

File metadata

  • Download URL: looker_sdk-0.1.3b1-py3-none-any.whl
  • Upload date:
  • Size: 96.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for looker_sdk-0.1.3b1-py3-none-any.whl
Algorithm Hash digest
SHA256 20d44bf245561192e78553181da012a1fe796dcd5fbd660a21548ad74e75b253
MD5 828f665947f4af0fe452e1aa131c3174
BLAKE2b-256 0199635fbb6338c327863c75e753bc1d84ef796cbb02de5b6c30576834b606ab

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page