-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfusionTablesRestTest.py
191 lines (168 loc) · 5.54 KB
/
fusionTablesRestTest.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import urllib2, urllib, simplejson, sys, httplib
# To generate values for the variables below:
# 1. Go to code.google.com/apis/console
# 2. Turn on the Fusion Tables API
# 3. Accept the terms
# 4. Click API Access in left-hend nav
# 5. api_key is under "Simple API Access"
# 6. Click "Create an OAuth 2.0 client ID..." to get the client_id and client_secret
# 7. Enter a product name.. this can be anything.
# 8. Select Installed Application
# 9. client_id, client_secret, and redirect_uri are available now (use http://localhost for redirect uri)
# 10. Enter a table id of a table you own
# 11. To run the code, from command line in the directory containing the file, type python infowindow_styling_demo.py
client_id = "xyz"
client_secret = "abc"
redirect_uri = "http://test.com/oauth2callback"
api_key = "123"
tableid = "123456"
class RunAPITest:
def __init__(self):
self.access_token = ""
self.params = ""
def main(self):
print "copy and paste the url below into browser address bar and hit enter"
print "https://accounts.google.com/o/oauth2/auth?%s%s%s%s" % \
("client_id=%s&" % (client_id),
"redirect_uri=%s&" % (redirect_uri),
"scope=https://www.googleapis.com/auth/fusiontables&",
"response_type=code")
code = raw_input("Enter code (parameter of URL): ")
data = urllib.urlencode({
"code": code,
"client_id": client_id,
"client_secret": client_secret,
"redirect_uri": redirect_uri,
"grant_type": "authorization_code"
})
serv_req = urllib2.Request(url="https://accounts.google.com/o/oauth2/token",
data=data)
serv_resp = urllib2.urlopen(serv_req)
response = serv_resp.read()
tokens = simplejson.loads(response)
access_token = tokens["access_token"]
self.access_token = access_token
self.params = "?key=%s&access_token=%s" % \
(api_key, self.access_token)
def getStyles(self):
print "GET STYLES"
self.runRequest(
"GET",
"/fusiontables/v1/tables/%s/styles%s" % \
(tableid, self.params))
def getStyle(self, styleId):
print "GET STYLE"
self.runRequest(
"GET",
"/fusiontables/v1/tables/%s/styles/%d%s" % \
(tableid, styleId, self.params))
def insertStyle(self, color):
print "INSERT STYLE"
data = '''{
"name": "myfirststyle",
"tableId": %s,
"markerOptions": {
"iconName": "%s"
}
}''' % (tableid, color)
response = self.runRequest(
"POST",
"/fusiontables/v1/tables/%s/styles%s" % \
(tableid, self.params),
data,
headers={'Content-Type':'application/json'})
json_response = simplejson.loads(response)
return json_response["styleId"]
def updateStyle(self, styleId):
print "UPDATE STYLE"
data = '''{
"styleId": %d,
"name": "myfirststyle",
"tableId": %s,
"markerOptions": {
"iconName": "small_red"
}
}''' % (styleId, tableid)
self.runRequest(
"PUT",
"/fusiontables/v1/tables/%s/styles/%d%s" % \
(tableid, styleId, self.params),
data,
headers={'Content-Type':'application/json'})
def deleteStyle(self, styleId):
print "DELETE STYLE"
self.runRequest(
"DELETE",
"/fusiontables/v1/tables/%s/styles/%d%s" % \
(tableid, styleId, self.params))
def getTemplates(self):
print "GET TEMPLATES"
self.runRequest(
"GET",
"/fusiontables/v1/tables/%s/templates%s" % \
(tableid, self.params))
def getTemplate(self, templateId):
print "GET TEMPLATE"
self.runRequest(
"GET",
"/fusiontables/v1/tables/%s/templates/%d%s" % \
(tableid, templateId, self.params))
def insertTemplate(self):
print "INSERT TEMPLATE"
data = '''{
"tableId": %s,
"name": "mytemplate",
"isDefaultForTable": true,
"body": "<p>this is a name: {name}</p>"
}''' % (tableid)
response = self.runRequest(
"POST",
"/fusiontables/v1/tables/%s/templates%s" % \
(tableid, self.params),
data,
headers={'Content-Type':'application/json'})
json_response = simplejson.loads(response)
return json_response["templateId"]
def updateTemplate(self, templateId):
print "UPDATE TEMPLATE"
data = '''{
"tableId": %s,
"templateId": %d,
"name": "mytemplate",
"isDefaultForTable": true,
"body": "<p>this is not a name: {name}</p>"
}''' % (tableid, templateId)
self.runRequest(
"PUT",
"/fusiontables/v1/tables/%s/templates/%d%s" % \
(tableid, templateId, self.params),
data,
headers={'Content-Type':'application/json'})
def deleteTemplate(self, templateId):
print "DELETE TEMPLATE"
self.runRequest(
"DELETE",
"/fusiontables/v1/tables/%s/templates/%d%s" % \
(tableid, templateId, self.params))
def runRequest(self, method, url, data=None, headers=None):
request = httplib.HTTPSConnection("www.googleapis.com")
if data and headers: request.request(method, url, data, headers)
else: request.request(method, url)
response = request.getresponse()
print response.status, response.reason
response = response.read()
print response
return response
if __name__ == "__main__":
api_test = RunAPITest()
api_test.main()
api_test.getStyles()
styleId = api_test.insertStyle("large_red")
api_test.getStyle(styleId)
api_test.updateStyle(styleId)
api_test.deleteStyle(styleId)
api_test.getTemplates()
templateId = api_test.insertTemplate()
api_test.getTemplate(templateId)
api_test.updateTemplate(templateId)
api_test.deleteTemplate(templateId)