-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrestapi.py
More file actions
439 lines (392 loc) · 12.9 KB
/
restapi.py
File metadata and controls
439 lines (392 loc) · 12.9 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/env python
"""
restapi.py Learn REST API with Python script
Usage: restapi.py [options]
Options:
-e --exer <number> exercise number
-h this help
Mail bug reports and suggestion to : Larry Cai <larry.caiyu AT gmail.com>
"""
import getopt, sys, os, errno
import getpass
import requests
import json
from requests.auth import HTTPBasicAuth
from requests.auth import HTTPDigestAuth
from HTMLParser import HTMLParser
# Reference:
# Understand REST API: http://www.restapitutorial.com/
# Requests python module: http://www.python-requests.org
# Github developer API v3 http://developer.github.com/v3/
# Jenkins API https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
class parselinks(HTMLParser):
''' HTML content parser.
The result contains all data that matches <a href="/job/></a>
'''
def __init__(self):
self.data=[]
self.href=0
self.linkname=''
HTMLParser.__init__(self)
def handle_starttag(self,tag,attrs):
if tag =='a':
for name,value in attrs:
if name == 'href' and value.startswith('/job/'):
self.href=1
def handle_data(self,data):
if self.href:
self.linkname+=data
def handle_endtag(self,tag):
if tag=='a':
self.linkname=''.join(self.linkname.split())
self.linkname=self.linkname.strip()
if self.linkname:
self.data.append(self.linkname)
self.linkname=''
self.href=0
def get_result(self):
for value in self.data:
print value
# exer1 : use curl to access http://httpbin.org/get
# exer2 : use requests module in python interactive module
#
def exer3():
"""
learn requests module
task:
get data from the server and print out response data like status_code, text, headers
"""
server = "http://httpbin.org/get"
r = requests.get(server)
#print json.dumps(r.json(), indent = 2)
print r.json()
# r = requests.get('https://github.com', verify=True)
# <Response [200]>
# r = requests.get('https://github.com', auth=HTTPBasicAuth(user,passwd))
#
def exer4():
"""
learn https and authentication in requests
learn real github example gist
task:
get your account's created date using authentication
"""
server = "https://api.github.com"
url = server + "/users"
user = "mamahaha"
passwd = getpass.getpass('Password:')
print "checking ", url, "using user:", user
r = requests.get(url, auth=HTTPBasicAuth(user,passwd))
print r
#print json.dumps(r.json(), indent = 2)
#>>> import json
#>>> url = 'https://api.github.com/some/endpoint'
#>>> payload = {'some': 'data'}
#>>> headers = {'content-type': 'application/json'}
#>>> r = requests.post(url, data=json.dumps(payload), headers=headers)
#
# check http://developer.github.com/v3/gists/#create-a-gist
#
def exer5():
"""
learn POST method in requests
learn create gist
task:
create the gist for this script with name "restapi.py" and description "CodingWithMe sample"
"""
server = "https://api.github.com"
url = server + "/gists"
user = "Mamahaha"
passwd = getpass.getpass('Password:')
local_file = "restapi.py"
with open(local_file) as fh:
mydata = fh.read()
files = {
"description": "rest api ",
"public": "true",
"user" : user,
"files": {
"file1.txt": {
"content": mydata
}
}
}
r1 = requests.post(url, data=json.dumps(files), auth=HTTPBasicAuth(user,passwd))
#print json.dumps(r1.json(), indent = 2)
print r1.json()['url']
#r2 = requests.get(r1.json()['url'], auth=HTTPBasicAuth(user,passwd))
#print r2.json()['files']['file1.txt']['content']
def exer6():
# this is bonus exercise
# 7.1 Implement for all gist API
#exer71()
# 7.2 oauth to access github in python script
#oauth_usage()
# 7.3 Get all jenkins jobs in python script
#get_jenkins_jobs()
pass
# global variables
server = "https://api.github.com"
url = server + "/gists"
user = "Mamahaha"
# Implementation of gist api
def exer71():
''' gist api implementation
'''
gist_cmds = {
'authentication' : lambda : gist_auth(),
'list' : lambda : gist_list(),
'get' : lambda : gist_get(),
'create' : lambda : gist_create(),
'edit' : lambda : gist_update(),
'star' : lambda : gist_star(),
'unstar' : lambda : gist_unstar(),
'checkstar' : lambda : gist_check_star(),
'fork' : lambda : gist_fork(),
'delete' : lambda : gist_delete(),
}
print '========gist api list==========='
for item in gist_cmds.keys():
print item
param = raw_input('\n$Choose a gist operation listed above: ')
if param in gist_cmds.keys():
gist_cmds[param]()
else:
print 'Error: Unsupported gist operation'
def gist_auth():
None
def gist_list():
global server
global user
passwd = getpass.getpass('$Password:')
resp = {
'user' : lambda : requests.get(server + '/users/' + user + '/gists', auth=HTTPBasicAuth(user,passwd)),
'all' : lambda : requests.get(server + '/gists'),
'public' : lambda : requests.get(server + '/gists/public'),
'starred' : lambda : requests.get(server + '/gists/starred', auth=HTTPBasicAuth(user,passwd)),
}
print '========list type==========='
for item in resp.keys():
print item
param = raw_input('\n$Choose a list type from above: ')
if param in resp.keys():
r = resp[param]()
print r
print json.dumps(r.json(), indent = 2)
else:
print 'Error: No such list type'
def gist_get():
global server
global user
passwd = getpass.getpass('$Password:')
id = raw_input('\n$Input the commit id that you want to get: ')
r = requests.get(server + '/gists/' + id, auth=HTTPBasicAuth(user,passwd))
print r
print json.dumps(r.json(), indent = 2)
def gist_create():
global server
global user
passwd = getpass.getpass('$Password:')
url = server + "/gists"
commit = {
"description": "the description for this gist",
"public": "true",
"user" : user,
"files": {
"file1.txt": {
"content": "ohyeah"
}
}
}
r = requests.post(url, data=json.dumps(commit), auth=HTTPBasicAuth(user,passwd))
print r
print json.dumps(r.json(), indent = 2)
def gist_update():
global server
global user
passwd = getpass.getpass('$Password:')
#id = raw_input('\n$Input the commit id that you want to get: ')
id = 'b584f07661af97270500'
url = server + "/gists/" + id
print url
commit = {
"description": "the description for this gist",
"files": {
"file1.txt": {
"content": "ohyeah again"
},
"file2.txt": {
"content": "bye bye"
}
}
}
r = requests.patch(url, data=json.dumps(commit), auth=HTTPBasicAuth(user,passwd))
print r
print json.dumps(r.json(), indent = 2)
def gist_star():
global server
global user
passwd = getpass.getpass('$Password:')
#id = raw_input('\n$Input the commit id that you want to get: ')
id = '9360b120fc97767b2959'
url = server + "/gists/" + id + '/star'
r = requests.put(url, auth=HTTPBasicAuth(user,passwd))
print r
def gist_unstar():
global server
global user
passwd = getpass.getpass('$Password:')
#id = raw_input('\n$Input the commit id that you want to get: ')
id = '9360b120fc97767b2959'
url = server + "/gists/" + id + '/star'
r = requests.delete(url, auth=HTTPBasicAuth(user,passwd))
print r
def gist_check_star():
global server
global user
passwd = getpass.getpass('$Password:')
#id = raw_input('\n$Input the commit id that you want to get: ')
id = '9360b120fc97767b2959'
url = server + "/gists/" + id + '/star'
r = requests.get(url, auth=HTTPBasicAuth(user,passwd))
print r
def gist_fork():
global server
global user
passwd = getpass.getpass('$Password:')
#id = raw_input('\n$Input the commit id that you want to get: ')
id = '9360b120fc97767b2959'
url = server + "/gists/" + id + '/forks'
r = requests.post(url, auth=HTTPBasicAuth(user,passwd))
print r
print json.dumps(r.json(), indent = 2)
def gist_delete():
global server
global user
passwd = getpass.getpass('$Password:')
#id = raw_input('\n$Input the commit id that you want to get: ')
id = '9360b120fc97767b2959'
url = server + "/gists/" + id
r = requests.delete(url, auth=HTTPBasicAuth(user,passwd))
print r
############end of exer71#####################
def oauth_usage():
server = 'https://api.github.com'
client_id = 'dd2593e8318bb93f713d'
client_secret = '' #your own client_secret
personal_token = '' #your token created from <client_id, client_secret>
auth_url = 'https://api.github.com/authorizations'
token_postfix = 'x-oauth-basic'
user = 'mamahaha'
passwd = getpass.getpass('Password:')
user_auth = HTTPBasicAuth(user, passwd)
token_auth = HTTPBasicAuth(personal_token, token_postfix)
#step1: list all client_id, token, id from authorize
data1 = {
'client_id' : client_id,
'redirect_uri' : 'https://api.github.com/gists/b584f07661af97270500',
'scope' : ['repo'],
}
#r1 = requests.get(auth_url, auth=user_auth)
#print r1
#print json.dumps(r1.json(), indent = 2)
#r1_client_id = r1.json()[4]['app']['client_id']
#r1_token = r1.json()[4]['token']
#r1_id = r1.json()[4]['id']
#print r1_client_id, r1_token, r1_id
#step2: get a single client_id and token according to a given id from authorize
#r2 = requests.get(auth_url+'/4908958', auth=user_auth)
#print r2
#print json.dumps(r2.json(), indent = 2)
#step3: create a new authorizations
data3 = {
'scopes' : ['repo', 'gist', 'user'],
'note' : 'exer_token',
'client_id' : client_id,
'client_secret' : client_secret
}
#r3 = requests.post(auth_url, data=json.dumps(data3), auth=user_auth)
#print r3
#print json.dumps(r3.json(), indent = 2)
#r3_id = r1.json()['id']
#r3_token = r1.json()['token']
#r3_client_id = r1.json()['app']['client_id']
#step4: get-or-create an authorization for a specific app'
data4 = {
'client_secret' : client_secret,
'scope' : ['repo'],
}
#r4 = requests.put(auth_url+'/clients/'+client_id, data=json.dumps(data4), auth=user_auth)
#print r4
#print json.dumps(r4.json(), indent = 2)
#step5: update an existing authorization
#TODO, use 'PATCH /authorizations/:id'
#step6: delete an authorization
#TODO, use 'DELETE /authorizations/:id'
#step7: validate token
#r7 = requests.get(server+'/applications/'+client_id+'/tokens/'+personal_token, auth=HTTPBasicAuth(client_id, client_secret))
#print r7
#print json.dumps(r7.json(), indent = 2)
#step8: use a repo token to access repository
#r8 = requests.get(server+'/users/Mamahaha/repos', auth=token_auth)
#print r8
#print json.dumps(r8.json(), indent = 2)
def get_jenkins_jobs():
url1 = '' #your jenkins url
user = '' #your jenkins user
passwd = getpass.getpass('Password:')
user_auth = HTTPBasicAuth(user, passwd)
#this will return a html-format respone, and we need a html parser to get all jobs
r = requests.get(url1, auth=user_auth, verify=False)
print r
r2 = parselinks()
data = r.content.decode('utf-8')
r2.feed(data)
r2.get_result()
r2.close()
def enable_debug():
import logging
# These two lines enable debugging at httplib level (requests->urllib3->httplib)
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# The only thing missing will be the response.body which is not logged.
import httplib
httplib.HTTPConnection.debuglevel = 1
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def main():
number = "3"
try:
cmdlineOptions, args= getopt.getopt(sys.argv[1:],'he:',["help","exer="])
except getopt.GetoptError, e:
raise "Error in a command-line option:\n\t" + str(e)
for (optName,optValue) in cmdlineOptions:
if optName in ("-h","--help"):
print __doc__
sys.exit()
elif optName in ("-e","--exer"):
number = optValue
else:
errorHandler('Option %s not recognized' % optName)
# mostly you don't need to understand below, focus on exercise
# Get it from globals and invoke `exerx()` method directly
method_name = "exer" + number
method_list = globals()
if method_name in method_list:
method_list[method_name]()
else:
print "the exericse %s is not ready, please create method `exer%s` directly" % (number, number )
if __name__ == "__main__":
main()
else:
#gist_create()
#gist_star()
#gist_check_star()
#gist_fork()
#get_jenkins_jobs()
#flask_auth()
#exer5()
pass