forked from bullhorn/rest-api-example-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_example.py
More file actions
92 lines (75 loc) · 2.7 KB
/
api_example.py
File metadata and controls
92 lines (75 loc) · 2.7 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
import web, json, urllib
import oauth, api
from web_utils import *
app_base_path = '/sample'
oauth_redirect_path = app_base_path + '/oauth-callback'
urls = (
app_base_path, 'userid',
oauth_redirect_path, 'auth',
app_base_path + '/logout', 'reset'
)
"""
Set up a web.py application with session data stored on disk.
"""
app = web.application(urls, globals())
if web.config.get('_session') is None:
session = web.session.Session(app, web.session.DiskStore('sessions'),
initializer={'count': 0, 'access_token': None, 'rest_token': None, 'rest_url': None})
web.config._session = session
else:
session = web.config._session
def oauth_redirect_uri():
return web.ctx.home + oauth_redirect_path
def auth_check():
if session.rest_token is None:
raise web.seeother(oauth.build_authorize_url(web.ctx.path, oauth_redirect_uri()))
def api_call(command, method='GET', params=None):
return api.make_call(session.rest_url, session.rest_token, command, method, params)
class auth:
"""
This is the OAuth authentication callback.
Once the user enters credentials on the Bullhorn OAuth
login page, his/her browser will be redirected here, with
a "code" query parameter added, containing an authorization
code.
This code will then negotiate an access token with Bullhorn
OAuth given an authorization token on the query string.
It will then use that access token to obtain a REST API token.
The URL of this call is what you must give to Bullhorn as your
OAuth callback URI.
"""
def GET(self):
# parse the query string into a dictionary
params = parseQuery(web.ctx.query)
if session.access_token is None:
if 'code' in params:
session.access_token = oauth.get_access_token(params['code'], oauth_redirect_uri())
else:
web.ctx.status = "400"
return "oauth authorization code missing from parameters"
api_login_data = api.login(session.access_token)
session.rest_url = api_login_data.rest_url
session.rest_token = api_login_data.rest_token
if 'state' in params:
redirect = urllib.unquote(params['state'])
else:
redirect = app_base_path
raise web.seeother(redirect)
class userid:
"""
Our sample API call. Simply gets the logged-in user's
user ID using the /settings call.
"""
def GET(self):
auth_check()
data = api_call("settings/userId")
return "your user ID is: %s" % data["userId"]
class reset:
"""
Clears the session.
"""
def GET(self):
session.kill()
return "session reset"
if __name__ == "__main__":
app.run()