/
query.py
24 lines (18 loc) · 796 Bytes
/
query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from apiclient import discovery, errors
from gitmostwanted.bigquery.result import Result
from gitmostwanted.app import app
from httplib2 import Http
from oauth2client.client import SignedJwtAssertionCredentials
def service():
config = app.config['GOOGLE_BIGQUERY']
with open(config['private_key_path'], 'rb') as f:
private_key = f.read()
auth = SignedJwtAssertionCredentials(config['email'], private_key, config['url'])
return discovery.build(config['service_name'], config['version'], http=auth.authorize(Http()))
def fetch(body):
try:
pid = app.config['GOOGLE_BIGQUERY']['project_id']
return Result(service().jobs().query(projectId=pid, body=body).execute())
except errors.HttpError as e:
app.logger.warning(e)
return False