Generating Client SDKs for the Looker API

Hi Chris,

Having pre-built Looker SDKs for a variety of programming languages is definitely on my wish list.

This requires two steps:

  • Learning how to build and distribute packages in each of those languages
  • Integrating that build & distribute into our Looker release process

We’d only be able to do this for the top few languages customers use to call our API. Everyone else will still have to use the Swagger codegen process to generate a client SDK in their language, or roll-their-own SDK that wraps the REST endpoints directly.

Good news for you! Python is already in the top 5 languages used to call the Looker API. ��

I can’t offer any timeframes for getting this investigated and done, as I have to fit this work in around my other responsibilities. (shh! New Looker APIs in the works!) But I hear ya and I definitely want to make getting up to speed with Looker client SDKs a lot easier for you all!

-Danny

Thanks for your attention here, Danny. I love that y’all are so receptive and helpful on this forum �� .

1 Like

Installing as a python module is easy with pip:

$ ls
lookerapi.json  python_sdk  swagger-codegen  swagger-codegen-cli.jar
$ cd python_sdk/
$ ls
docs         README.md         setup.py        test                   tox.ini
git_push.sh  requirements.txt  swagger_client  test-requirements.txt
$ pip install .

Note this will install the module as “swagger_client”, unless you rename the directory.
Virtual environment highly recommended.

The sdk seems to work fine with Python 3.6. I tried using the current stable version of swagger-codegen (2.2.3), but got an error, something to do with “can’t create Role” (can’t remember exactly and it has scrolled off my terminal). Using swagger-codegen v2.2.2 as specified above, no problems.

Very nice API and tutorial, many thanks!

I get the following error starting the server after I generated a nodejs package with codegen:

API Errors:

  #/paths/~1dashboards~1{dashboard_id}~1prefetch/get/parameters/1: Not a valid parameter definition
    #/paths/~1dashboards~1{dashboard_id}~1prefetch/get/parameters/1: Not a valid parameter definition
      #: Missing required property: schema
      #: Not a valid undefined definition
        #/items: Additional properties not allowed: $ref
        #/items: Additional properties not allowed: $ref
        #/items: Additional properties not allowed: $ref
        #/items: Additional properties not allowed: $ref
    #/paths/~1dashboards~1{dashboard_id}~1prefetch/get/parameters/1: Missing required property: $ref

1 error and 0 warnings

It appears the dashboard_filters query arg in the dashboard prefetch request is supposed to be an array of objects which I believe is disallowed by OpenAPI spec.

After I removed this section from swagger.json and regenerated the client I could run the sample server

{
    "name": "dashboard_filters",
    "in": "query",
    "description": "JSON encoded string of Dashboard filters that were applied to prefetch",
    "required": false,
    "type": "array",
    "items": {
        "$ref": "#/definitions/PrefetchDashboardFilterValue"
    },
    "collectionFormat": "csv"
}

danny:

Open a new browser window and navigate to this URL: https://

I am stuck with 2 problem at present.

Problem 1:
In the below process step

  1. Open a new browser window and navigate to this URL: https://:19999/api/3.0/swagger.json Subsitute your custom Looker API Host domain name and port if necessary.
  2. Save the contents of that swagger.json page to looker_sdk/lookerapi.json.

I am able to browse toward the URL but that has many in list. Should I manually copy paste them and save it to lookerapi.json or only which is required. Like authentication alone ?

Problem 2: GIT

when I try to access GIT
git clone git@github.com:swagger-api/swagger-codegen.git ./swagger-codegen

I am prompted with error

Cloning into ‘swagger-codegen’…
ssh: connect to host github.com port 22: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

Can you please help me with this ?

Thanks

1 Like

Do we have any Video on the how can the looker api be setup and configured.

@svarun , here’s a few links to documentation to get you started:

  • https://yourdomain.looker.com:19999/api-docs/index.html, switch out your domain here to access the api-docs, where you can test API calls.

  • https://vimeo.com/213741524 (data science with Python)

Regarding

mvn package> Make yourself a cup of coffee. Fry up some donuts while you’re at it - this will take awhile!

If you run mvn clean package -DskipTests it takes less than 30 seconds to build.

Also swagger-codegen 2.2.3 worked for auto generating Java against Looker 5.2.21

Is there any plan to distribute examples of using the swagger generated api’s in languages such as Java? I’ve found the link below before, and it has just about every language other than java.

Hey Dan,

It looks like LookerEmbedClientExample is the Java version of the SSO script you linked to here,
but this is for generating an SSO Login URL.

To generate a Swagger SDK for Java, follow the above instructions until you get to the header “Generate a Looker API Client SDK.” After the command there is a paragraph describing how to alter the statement to get a Java SDK.

You can use the same command to generate Looker API client SDKs for Java, .NET, and a variety of other languages and platforms supported by the Swagger-Codegen tools. Change the language target with the -l parameter and the output directory with the -o parameter.

Let me know if you have any more questions.

Spencer

Sorry my original comment was misleading.

I’ve generated the Looker API SDK via the swagger generation. My problem is finding tangible examples of the swagger api sdk in action. Would love to see some tangible examples in java. Let me know if that makes sense.

@dgroman1988

Ah I see, thanks for clarifying. At this time there aren’t tangible examples of using the Looker API with Java but I will let our Product/Docs team know these would be helpful.

We do have these examples in Python, but it doesn’t look like we currently have the same thing for Java.

FWIW, when creating a Python SDK v.2.3.1 for Python 3.6.4, I found I needed to change the sample usage script:

# instantiate Auth API
unauthed_client = looker.ApiClient(configuration=None)
unauthed_auth_api = looker.ApiAuthApi(unauthed_client)

# authenticate client
token = unauthed_auth_api.login(
    client_id=client_id, client_secret=client_secret)
client = looker.ApiClient(
    header_name='Authorization',
    header_value='token ' + token.access_token)

The reason is that if I merely passed strings as positional parameters, then the configuration object wasn’t an object, but was just strings, which don’t have the configuration object’s methods, so the SDK code would break.

1 Like

Thanks, this took me ages to work out!

1 Like

Hi Danny,

I’ve managed to perform all the steps until the last one.

A few questions to debug it:

  1. The file test_app.py must be placed in python work directory (~/pywd/) and not in ~/pywd/looker/, ~/looker_sdk/python_sdk/ nor ~/looker_sdk/python_sdk/swagger_client/ ?

  2. When I run python test_app.py in my python work directory ~/pywd/, the following error occurs

AttributeError: module 'looker' has no attribute 'ApiClient'

I did filled the base_url, client_idand `client_secret``
Is there something I missed ?

Many thanks in advance,

Alexandre

PS : I also tried Eric’s suggested amendment, without much success (same error)

Alexandre,

You could try this method:

  • leave the Python SDK where it was built: ~/looker_sdk/python_sdk/swagger_client

  • add this directory to the environment variable PYTHONPATH (I’m assuming you use bash shell)
    $ export PYTHONPATH=~/looker_sdk/python_sdk:$PYTHONPATH

  • in your program in the Python working directory, add this line
    import swagger_client as looker

  • now you can call looker.ApiClient(), etc., in your code

The Java SDK Example

ApiClient lookerApiClient = new ApiClient();
lookerApiClient.setBasePath(“https://xxxxx:19999/api/3.0”);
ApiAuthApi authAPI = new ApiAuthApi(lookerApiClient);
AccessToken authenticatingToken = authAPI.login(clientid, clientkey);
lookerApiClient.addDefaultHeader(“Authorization”, "token " + authenticatingToken.getAccessToken());
DashboardApi dashboardApi = new DashboardApi(lookerApiClient);

1 Like

I am trying to generate a client using swagger-codegen in aspnetcore. When I generate the client everything gets generated but all the code in the controllers is filled with TODO comments rather than the actual code for the method:

Example:

public virtual IActionResult Login([FromQuery]string clientId, [FromQuery]string clientSecret)
        { 
            //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(200, default(AccessToken));

            //TODO: Uncomment the next line to return response 400 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(400, default(Error));

            //TODO: Uncomment the next line to return response 404 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(404, default(Error));

            string exampleJson = null;
            exampleJson = "{\n  \"access_token\" : \"access_token\",\n  \"token_type\" : \"token_type\",\n  \"expires_in\" : 0\n}";
            
            var example = exampleJson != null
            ? JsonConvert.DeserializeObject<AccessToken>(exampleJson)
            : default(AccessToken);
            //TODO: Change the data returned
            return new ObjectResult(example);
        }

However, when I generate the client using .net starndard, everything gets generated fine and I can consume the API endpoints as documented.

Here is the command I’m running:

swagger-codegen generate -i swagger.json -l aspnetcore -o LookerAPI

Does the swagger file provided by looker (API 3.0) support dotnet core? Seems like it might not be completed yet…