Back

Step by Step Guide to Building a Looker API Integration in Ruby

Aug 9, 20244 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Looker API integration? You're in for a treat. Looker's API is a powerhouse that lets you tap into your data like never before. In this guide, we'll walk through building a robust integration in Ruby. Let's get cracking!

Prerequisites

Before we jump in, make sure you've got:

  • A Ruby environment (2.5+ recommended)
  • Looker API credentials (client ID and secret)
  • Your favorite text editor or IDE

Setting up the project

First things first, let's set up our project:

mkdir looker_integration cd looker_integration bundle init

Now, open up your Gemfile and add these gems:

gem 'faraday' gem 'oauth2'

Run bundle install, and we're off to the races!

Authentication

Looker uses OAuth 2.0, so let's set that up:

require 'oauth2' client = OAuth2::Client.new( ENV['LOOKER_CLIENT_ID'], ENV['LOOKER_CLIENT_SECRET'], site: 'https://your-looker-instance.com:19999/api/3.1' ) token = client.client_credentials.get_token

Pro tip: Always use environment variables for sensitive info. Your future self will thank you!

Making API requests

Now that we're authenticated, let's make a simple request:

response = token.get('/user') puts response.body

Easy peasy, right? This will fetch info about the authenticated user.

Common Looker API operations

Let's tackle some common operations:

Running a Look

look_id = 123 response = token.get("/looks/#{look_id}/run/json") puts JSON.parse(response.body)

Retrieving dashboard data

dashboard_id = 456 response = token.get("/dashboards/#{dashboard_id}") puts JSON.parse(response.body)

Error handling and best practices

Always expect the unexpected! Here's a simple retry mechanism:

def make_request(endpoint) retries = 3 begin token.get(endpoint) rescue OAuth2::Error => e if e.response.status == 429 && retries > 0 sleep 5 retries -= 1 retry else raise end end end

This will retry up to 3 times if we hit a rate limit. Neat, huh?

Testing the integration

Don't forget to test! Here's a quick example using RSpec:

RSpec.describe LookerIntegration do it "fetches user info" do VCR.use_cassette("user_info") do response = subject.get_user_info expect(response).to have_key('id') end end end

Conclusion

And there you have it! You've just built a solid foundation for your Looker API integration. Remember, this is just the tip of the iceberg. Looker's API has tons more to offer, so don't be afraid to explore and experiment.

Keep coding, stay curious, and may your data always be insightful!