Created
April 28, 2016 10:14
-
-
Save javier/1af2d354b3fcf297e53a5addc5c9b110 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.appengine.ext import vendor | |
vendor.add('lib') | |
import json | |
import os | |
import webapp2 | |
#from oauth2client.contrib.appengine import AppAssertionCredentials | |
from httplib2 import Http | |
from apiclient.discovery import build | |
from oauth2client.service_account import ServiceAccountCredentials | |
import datetime | |
from google.appengine.ext import db | |
from google.appengine.api import users | |
scopes = ['https://www.googleapis.com/auth/bigquery'] | |
credentials = ServiceAccountCredentials.from_json_keyfile_name( | |
'client_secrets.json', scopes=scopes) | |
# credentials = AppAssertionCredentials('https://www.googleapis.com/auth/bigquery') | |
http_auth = credentials.authorize(Http()) | |
service = build('bigquery', 'v2', http=http_auth, credentials=credentials) | |
# The project id whose datasets you'd like to list | |
PROJECTID = 'javier-cp300' | |
class Monitor(db.Model): | |
name = db.StringProperty(required=True) | |
last_date = db.DateTimeProperty() | |
successful = db.BooleanProperty(indexed=False) | |
class MainPage(webapp2.RequestHandler): | |
# oauth_required ensures that the user goes through the OAuth2 | |
# authorization flow before reaching this handler. | |
def get(self): | |
# This is an httplib2.Http instance that is signed with the user's | |
# credentials. This allows you to access the BigQuery API on behalf | |
# of the user. | |
m = Monitor.all() | |
m.filter("last_date >=", datetime.datetime.now().date() ) | |
m.order('-last_date') | |
result = m.get() | |
if result: | |
self.response.out.write( result.name + " : " + result.last_date.strftime('%m/%d/%Y %H:%M:%S')) | |
response = service.datasets().list(projectId=PROJECTID).execute() | |
self.response.out.write('<h3>Datasets.list raw response:</h3>') | |
self.response.out.write('<pre>%s</pre>' % | |
json.dumps(response, sort_keys=True, indent=4, | |
separators=(',', ': '))) | |
query_data = { | |
'query': ( | |
'SELECT fullName,age ' | |
'FROM [course.people];') | |
} | |
query_response = service.jobs().query( | |
projectId=PROJECTID, | |
body=query_data).execute() | |
for row in query_response['rows']: | |
self.response.out.write('<br/>') | |
for field in row['f']: | |
self.response.out.write(field['v'] + ' | ' ) | |
m = Monitor(name="last query", successful = True, last_date=datetime.datetime.now()) | |
m.put() | |
app = webapp2.WSGIApplication([ | |
('/', MainPage), | |
# Create the endpoint to receive oauth flow callbacks | |
], debug=True) | |
# [END all] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment