Skip to content
This repository has been archived by the owner on Sep 25, 2020. It is now read-only.

How can someone creates a Query object or a LookWithQuery object in Ruby? #54

Open
alan-krumholz opened this issue May 12, 2020 · 9 comments

Comments

@alan-krumholz
Copy link

How can someone creates a Query object or a LookWithQuery object in Ruby?

In python you show that these can be created by using:

from looker_sdk import models
models.WriteQuery(...)

However, I couldn't find any examples on how to do this in Ruby and they are needed to call run_inline_query() or update_look()

Thanks!

@joeldodge79
Copy link
Contributor

the looker-sdk-ruby package just uses hashes instead of class objects or any other strong types. so

query = {model: "mymodel", view: "myview", fields: ["mymodel.somefield"]}
sdk.create_query(query)

we will eventually release a new (strongly typed) version of the ruby sdk using our sdk-codgen project which will probably have something like query = WriteQuery.new(...)

@alan-krumholz
Copy link
Author

I see, let me give that a try!
thanks @joeldodge79 !

@alan-krumholz
Copy link
Author

@joeldodge79 Could you please let me know if this is what you meant? I get validation failed error :(

require 'looker-sdk'

sdk = LookerSDK::Client.new(
  :client_id => "cccccc",
  :client_secret => "pppppp",
  :api_endpoint => "https://xxxxxxx.looker.com:19999/api/3.1"
)

look = sdk.look(1)

output = sdk.run_inline_query(
    "csv",
    {
        "model" => look.query.model,
        "view" => look.query.view,
        "fields" => look.query.fields,
        "pivots" => look.query.pivots,
        "fill_fields" => look.query.fill_fields,
        "filters" => look.query.filters,
        "sorts" => look.query.sorts,
        "limit" => look.query.limit,
        "column_limit" => look.query.column_limit,
        "total" => look.query.total,
        "row_total" => look.query.row_total,
        "subtotals" => look.query.subtotals,
        "dynamic_fields" => look.query.dynamic_fields,
        "query_timezone" => look.query.query_timezone
    })

print output 

@joeldodge79
Copy link
Contributor

What's the error message?

@alan-krumholz
Copy link
Author

@joeldodge79 seems like it can't find such method:
method_missing
Validation Failed (LookerSDK::UnprocessableEntity)

aceback (most recent call last):
        12: from test.rb:11:in `<main>'
        11: from /Library/Ruby/Gems/2.6.0/gems/looker-sdk-0.0.7/lib/looker-sdk/client/dynamic.rb:106:in `method_missing'
        10: from /Library/Ruby/Gems/2.6.0/gems/looker-sdk-0.0.7/lib/looker-sdk/client/dynamic.rb:146:in `invoke_remote'
         9: from /Library/Ruby/Gems/2.6.0/gems/looker-sdk-0.0.7/lib/looker-sdk/client.rb:108:in `post'
         8: from /Library/Ruby/Gems/2.6.0/gems/looker-sdk-0.0.7/lib/looker-sdk/client.rb:304:in `request'
         7: from /Library/Ruby/Gems/2.6.0/gems/sawyer-0.8.2/lib/sawyer/agent.rb:94:in `call'
         6: from /Library/Ruby/Gems/2.6.0/gems/faraday-0.17.3/lib/faraday/connection.rb:175:in `post'
         5: from /Library/Ruby/Gems/2.6.0/gems/faraday-0.17.3/lib/faraday/connection.rb:387:in `run_request'
         4: from /Library/Ruby/Gems/2.6.0/gems/faraday-0.17.3/lib/faraday/rack_builder.rb:143:in `build_response'
         3: from /Library/Ruby/Gems/2.6.0/gems/faraday-0.17.3/lib/faraday/response.rb:8:in `call'
         2: from /Library/Ruby/Gems/2.6.0/gems/faraday-0.17.3/lib/faraday/response.rb:61:in `on_complete'
         1: from /Library/Ruby/Gems/2.6.0/gems/faraday-0.17.3/lib/faraday/response.rb:9:in `block in call'
/Library/Ruby/Gems/2.6.0/gems/looker-sdk-0.0.7/lib/looker-sdk/response/raise_error.rb:39:in `on_complete': Validation Failed (LookerSDK::UnprocessableEntity)

@joeldodge79
Copy link
Contributor

that's a misleading line in the stack trace - the sdk implementation relies on "missing_method" to dynamically dispatch to the correct api call.

The Validation Failed (LookerSDK::UnprocessableEntity) is actually being returned by API - which is funny because you're just supplying the values from the look.query - I suspect that maybe look.query.query_timezone isn't in the correct format that query_timezone wants. Try leaving it off to check. If that's not in then you can bisect the query body to identify which query property(ies) you're passing in that our API doesn't like

@alan-krumholz
Copy link
Author

@joeldodge79
query_timezone seems fine.
The problem seems to be with
"filters" => look.query.filters

I'll play around with this one to see if I can make it work

@alan-krumholz
Copy link
Author

alan-krumholz commented May 13, 2020

@joeldodge79
Maybe it is a bug but I don't need to copy the filter over anyways (this is the thing I do want to change :))

and that seems to work fine for me:

output = sdk.run_inline_query(
    "csv",
    {
        "model" => look.query.model,
        "view" => look.query.view,
        "fields" => look.query.fields,
        "pivots" => look.query.pivots,
        "fill_fields" => look.query.fill_fields,
        "filters" => {
            'x.id' => '255'},
        "sorts" => look.query.sorts,
        "limit" => look.query.limit,
        "column_limit" => look.query.column_limit,
        "total" => look.query.total,
        "row_total" => look.query.row_total,
        "subtotals" => look.query.subtotals,
        "dynamic_fields" => look.query.dynamic_fields,
        "query_timezone" => look.query.query_timezone
    })

Thank you

@joeldodge79
Copy link
Contributor

interesting, ok well glad you're unblocked

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants