Question

Generating Clients/SDKs Using Swagger Codegen

  • 18 November 2015
  • 44 replies
  • 1233 views

  • Anonymous
  • 0 replies

The Looker API v3.0 has a custom authentication scheme which requires a SHA-1 hash of the query parameters. Does the generated code know how to do this or I need to modify the generated code.


44 replies

Userlevel 2

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…

Right here while logged in to your instance as long as you are at least a Developer:


Chat would be better. Where can we get connected in chat. ?

Could you come on chat and we’ll work on this issue with you @svarun? Either that or you can pop us a mail at help.looker.com

Scott input was valuable and I was able to get swagger.json not I have moved to next level where I am writing the API script to connect looker via python. I was getting the error. The error is very much similar in early post. But I didn’t see any solution on that but people have overcome this.


to answer second question I even ran the curl via terminal and I get similar error which read as


HTTP/1.1 401 Unauthorized

Content-Type: application/json;charset=utf-8

Vary: Accept-Encoding

X-Content-Type-Options: nosniff

Content-Length: 84

Userlevel 4
Badge

@svarun, what is this related to exactly? What Scott linked to is the needed swagger json file for generating the sdk. As mentioned in the article, you can to this with curl or wget: curl -0 https://master.dev.looker.com:19999/api/3.0/swagger.json > swagger.json.


The response you pasted in here seems to be related to something else. You’re getting a 404, which means the resource you’re looking for is simply not there. Please reach out via help.looker.com or pop on chat so we can have a look at your code.

@scott.hoover

I am getting 404 error similar well not similar its same as mentioned by @Grayson_Williams. I am not sure how this was solved I am not able to follow the post comment on this clear. Could you please guide me on this?


raise ApiException(http_resp=r)

ApiException: (404)

Reason: Not Found

HTTP response headers: HTTPHeaderDict({‘Vary’: ‘Accept-Encoding’, ‘Content-Length’: ‘69’, ‘Content-Type’: ‘application/json;charset=utf-8’, ‘X-Content-Type-Options’: ‘nosniff’})

HTTP response body: {“message”:“Not found”,“documentation_url”:“http://docs.looker.com/”}

Userlevel 2

Take a look at Step 2 under the “Setup” section.


https://<your_looker_endpoint>:19999/api/3.0/swagger.json


You can retrieve the JSON from the above-mentioned endpoint.

Hello @scott.hoover -

I am stuck with the python script here. when I run the client side I get below error


Traceback (most recent call last):

File “/Users/sv/LookerPython/LKCONNECT2.py”, line 15, in

token = unauthenticated_authApi.login(client_id=client_id, client_secret=client_secret)

File “/Users/sv/LookerPython/swagger_client/apis/api_auth_api.py”, line 122, in login

callback=params.get(‘callback’))

File “/Users/sv/LookerPython/swagger_client/api_client.py”, line 322, in call_api

response_type, auth_settings, callback)

File “/Users/sv/LookerPython/swagger_client/api_client.py”, line 149, in __call_api

post_params=post_params, body=body)

File “/Users/sv/LookerPython/swagger_client/api_client.py”, line 358, in request

body=body)

File “/Users/sv/LookerPython/swagger_client/rest.py”, line 208, in POST

body=body)

File “/Users/sv/LookerPython/swagger_client/rest.py”, line 177, in request

raise ApiException(http_resp=r)

ApiException: (404)

Reason: Not Found

HTTP response headers: HTTPHeaderDict({‘Vary’: ‘Accept-Encoding’, ‘Content-Length’: ‘69’, ‘Content-Type’: ‘application/json;charset=utf-8’, ‘X-Content-Type-Options’: ‘nosniff’})

HTTP response body: {“message”:“Not found”,“documentation_url”:“http://docs.looker.com/”}

Finally I was able to make till end. Thanks to all support.

Now I am stuck with testing my Looker_Connect.py


when I run the .py code I am getting error as below Help!!!


Traceback (most recent call last):

File “/Users/sv/LookerPython/LKCONNECT2.py”, line 15, in

token = unauthenticated_authApi.login(client_id=client_id, client_secret=client_secret)

File “/Users/sv/LookerPython/swagger_client/apis/api_auth_api.py”, line 122, in login

callback=params.get(‘callback’))

File “/Users/sv/LookerPython/swagger_client/api_client.py”, line 322, in call_api

response_type, auth_settings, callback)

File “/Users/sv/LookerPython/swagger_client/api_client.py”, line 149, in __call_api

post_params=post_params, body=body)

File “/Users/sv/LookerPython/swagger_client/api_client.py”, line 358, in request

body=body)

File “/Users/sv/LookerPython/swagger_client/rest.py”, line 208, in POST

body=body)

File “/Users/sv/LookerPython/swagger_client/rest.py”, line 177, in request

raise ApiException(http_resp=r)

ApiException: (404)

Reason: Not Found

HTTP response headers: HTTPHeaderDict({‘Vary’: ‘Accept-Encoding’, ‘Content-Length’: ‘69’, ‘Content-Type’: ‘application/json;charset=utf-8’, ‘X-Content-Type-Options’: ‘nosniff’})

HTTP response body: {“message”:“Not found”,“documentation_url”:“http://docs.looker.com/”}

William - thanks for your input this was informative but it didn’t help me completely.


Appreciated. This is going to help me in future thou 🙂


@scott.hoover


How and from where do I get the swagger.json file and place the similar as below ?


/Users/scott/Desktop/looker_python_client/swagger.json


I am stuck with this file from few days. Can you help me out?


Cheers

VS

@svarun


You could indeed use the github site and hit “Clone or Download”:




I hope this helps!

William_Lane thanks for the update. This make sense. I shall try via different channel and come back.

But do we have any other way to manually get this configured ?

Hey @svarun ,


It sounds like you might have a firewall between your ssh host and github that is blocking traffic on port 22. Try from a location outside of your internal network.


Cheers,

William

I am not able to get the GIT.


git clone git@github.com:swagger-api/swagger-codegen.git


It prompts me an 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

Userlevel 3
Badge

👍

Solved - disregard 🙂

Hello there!


I’m having a similar problem to @scottmdavidson. But, my base_url is already correct syntax. I’m getting an ApiException: (400) error. It only happens when using functions whose method in the response variable is ‘POST’ (create_user, create_user_access_filter). I am able to run anything else that I’ve tried (all_users, all_user_access_filters).


What I mean by response variable whose method is post:



    response = self.api_client.call_api(resource_path, 'POST',
path_params,
query_params,
header_params,
body=body_params,
post_params=form_params,
files=local_var_files,
response_type='User',
auth_settings=auth_settings,
callback=params.get('callback'))


Any thoughts? Thanks!

Userlevel 3
Badge

@cross no problem! Glad to hear it’s working now 😃

Hey,


I still received the 404 error but the issue is resolved. You were correct it was a permission issue.


I re-looked (no pun intended) at the info from my original curl query and found the space id from the curl query was different than the space id from the front end application. I contacted the admin and the admin setup new api keys for the “right” account. Thanks for the help!

Userlevel 3
Badge

That basic syntax works for me, this worked on looker.learn.com:


curl -i -H "Authorization: token <BIG_TOKEN_HERE>" https://learn.looker.com:19999/api/3.0/looks/2938/run/json

HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
Transfer-Encoding: chunked

[{"users.age_tier":"80 or Above","order_items.count":1984,"order_items.percent_total_gm":4.8631375010242754},
. . .

Are you still getting a 404?

Ok thanks for the advice.



  1. I can see the look in the front end (I created it).


Can you point me in the direction to run the look via curl or tell me the syntax. Below is what I have:


curl https://looker.mycompany:19999/api/3.0/looks/15133/run/csv -H "Authorization: Bearer access_token here"
Userlevel 3
Badge

Hey there @cross!


I normally see this error when I’ve made a mistake in the permissions and groups that I’ve given to the user that I am authenticating as. This can also come up when the optional arguments provided to the function are not as expected.


I’d try these two steps first:



  1. Sudo as the API user and make sure you can see and run the Look in the front-end application. If you can’t, then you must adjust their permissions or access so that they can find and run the Look.

  2. EDIT: Nevermind, data format argument is only optional for other endpoints, required for this one.


If you still can’t run the Look from the API, try authenticating and running the Look via curl. If you can, this implies that maybe authentication is not working as expected in your Python script.

Hello,


I’m having similar issues to scottmdavidson and I am unable to run a look as I get a 404 error. See code below:


import swagger_client as looker
import os


client_id = os.environ.get('LOOKER_CLIENT_ID')
client_secret = os.environ.get('LOOKER_SECRET_KEY')
#base_url = 'https://looker.mycompany.com:9999/api/3.0/'
base_url = 'https://looker.mycompany.com:19999/api/3.0/'


unauthenticated_client = looker.ApiClient(base_url)
unauthenticated_authApi = looker.ApiAuthApi(unauthenticated_client)


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

looks = looker.LookApi(client)
looks.run_look(9669,'json')

The error I receive:


ApiException: (404)
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'Server': 'nginx', 'Date': 'Thu, 29 Jun 2017 19:14:36 GMT', 'Content-Type': 'application/json;charset=utf-8', 'Content-Length': '69', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding', 'X-Content-Type-Options': 'nosniff'})
HTTP response body: {"message":"Not found","documentation_url":"http://docs.looker.com/"}

I can authenticate successfully and receive a token. I tested that access_token in curl and it retrieves my user info successfully. Any help here to run a look would be great!

Reply