-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.py
81 lines (55 loc) · 2.02 KB
/
core.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
import os
import logging
from apiclient import discovery
from google_objects.auth import service_account_creds
log = logging.getLogger(__name__)
ENV_API_KEY = 'GOOGLE_API_KEY'
ENV_SERVICE_ACCOUNT = 'GOOGLE_SERVICE_ACCOUNT_PATH'
ENV_DELEGATED_USER = 'GOOGLE_DELEGATED_USER'
class GoogleClient(object):
"""Google API Base object that saves credentials
and build Resource objects. Responsible for permissions
as well.
"""
service = None
version = None
scope = {}
def __init__(self, resource=None):
self.resource = resource
@classmethod
def from_api_key(cls, api_key=None):
"""Authorizes a client from an Api Key."""
api_key = api_key or os.getenv(ENV_API_KEY)
if not api_key:
raise ValueError('API Key not provided.')
resource = discovery.build(cls.service, cls.version, developerKey=api_key)
return cls(resource)
@classmethod
def from_service_account(cls, creds_path=None, user=None):
"""Authorizes a client from an Service Account Credential File."""
creds_path = creds_path or os.getenv(ENV_SERVICE_ACCOUNT)
user = user or os.getenv(ENV_DELEGATED_USER)
if not creds_path:
raise ValueError('Service Account path not provided.')
http_client = service_account_creds(creds_path, user, scope=cls.scope)
resource = discovery.build(cls.service, cls.version, http=http_client)
return cls(resource)
class GoogleObject(object):
"""Sets private properties on subclasses,
corresponding one-to-one with Google API Resource
values.
"""
def __init__(self, *args, **kwargs):
"""Set Resource corresponding **kwargs
to private attributes.
"""
self.data = kwargs
@classmethod
def from_existing(cls, data, *args):
return cls(*args, **data)
def serialize(self):
"""convert __dict__ keys to camel case, get
intersection of this and _properties
"""
return self.data