/
app.py
175 lines (149 loc) · 5.17 KB
/
app.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Hive Google API
# Copyright (c) 2008-2018 Hive Solutions Lda.
#
# This file is part of Hive Google API.
#
# Hive Google API is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Google API is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Google API. If not, see <http://www.apache.org/licenses/>.
__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """
__version__ = "1.0.0"
""" The version of the module """
__revision__ = "$LastChangedRevision$"
""" The revision number of the module """
__date__ = "$LastChangedDate$"
""" The last change date of the module """
__copyright__ = "Copyright (c) 2008-2018 Hive Solutions Lda."
""" The copyright for the module """
__license__ = "Apache License, Version 2.0"
""" The license for the module """
import appier
from . import base
class GoogleApp(appier.WebApp):
def __init__(self, *args, **kwargs):
appier.WebApp.__init__(
self,
name = "google",
*args, **kwargs
)
@appier.route("/", "GET")
def index(self):
return self.userinfo()
@appier.route("/me", "GET")
def me(self):
url = self.ensure_api()
if url: return self.redirect(url)
api = self.get_api()
user = api.self_user()
return user
@appier.route("/userinfo", "GET")
def userinfo(self):
url = self.ensure_api()
if url: return self.redirect(url)
api = self.get_api()
userinfo = api.userinfo_oauth()
return userinfo
@appier.route("/files", "GET")
def files(self):
url = self.ensure_api()
if url: return self.redirect(url)
api = self.get_api()
contents = api.list_drive()
return contents
@appier.route("/files/<std:id>", "GET")
def file(self, id):
url = self.ensure_api()
if url: return self.redirect(url)
api = self.get_api()
contents = api.get_drive(id)
return contents
@appier.route("/files/insert/<str:message>", "GET")
def file_insert(self, message):
url = self.ensure_api()
if url: return self.redirect(url)
api = self.get_api()
contents = api.insert_drive(
message,
content_type = "text/plain",
title = message
)
return contents
@appier.route("/folders/insert/<str:title>", "GET")
def folder_insert(self, title):
url = self.ensure_api()
if url: return self.redirect(url)
api = self.get_api()
contents = api.folder_drive(title)
return contents
@appier.route("/children", "GET")
def children(self):
url = self.ensure_api()
if url: return self.redirect(url)
id = self.field("id", "root")
api = self.get_api()
contents = api.children_drive(id = id)
return contents
@appier.route("/spreadsheets/<str:id>", "GET")
def spreadsheet(self, id):
ranges = self.field("ranges")
include_grid_data = self.field("include_grid_data", cast = bool)
url = self.ensure_api()
if url: return self.redirect(url)
api = self.get_api()
contents = api.get_spreadsheet(
id,
ranges = ranges,
include_grid_data = include_grid_data
)
return contents
@appier.route("/logout", "GET")
def logout(self):
return self.oauth_error(None)
@appier.route("/oauth", "GET")
def oauth(self):
code = self.field("code")
error = self.field("error")
appier.verify(
not error,
message = "Invalid OAuth response (%s)" % error,
exception = appier.OperationalError
)
api = self.get_api()
access_token = api.oauth_access(code)
self.session["gg.access_token"] = access_token
return self.redirect(
self.url_for("google.index")
)
@appier.exception_handler(appier.OAuthAccessError)
def oauth_error(self, error):
if "gg.access_token" in self.session: del self.session["gg.access_token"]
return self.redirect(
self.url_for("google.index")
)
def ensure_api(self):
access_token = self.session.get("gg.access_token", None)
if access_token: return
api = base.get_api()
return api.oauth_authorize()
def get_api(self):
access_token = self.session and self.session.get("gg.access_token", None)
api = base.get_api()
api.access_token = access_token
return api
if __name__ == "__main__":
app = GoogleApp()
app.serve()
else:
__path__ = []