def dep_prep(self, query, method, authsession=None, token=None, params=False):
"""
Sets up common headers for DEP commands using the 'requests' Request
class to combine our auth token, required headers and other data
to generate a correct HTTP request to be sent to the DEP API.
Required parameters:
- query (The API request to use)
- method (The HTTP method to use: GET/PUT/POST)
- token (The auth session token retrieved by get_auth_token())
Optional parameters:
- authsession (expects an OAuth1Session instance)
- params (query string to send instead of JSON data)
"""
req = Request(method, self.dep_api_url + query)
prep = None
# Check whether we're requesting an auth session token or doing a regular
# API call with DEP.
if authsession:
prep = authsession.prepare_request(req)
# Regular API calls require the X-ADM-Auth-Session header to be set
elif token:
prep = req.prepare()
prep.headers['X-ADM-Auth-Session'] = token
# If we received no token or token is None we have a problem, halt.
else:
print "No token found, we must exit now..."
sys.exit(-1)
# Common required headers for DEP API calls, we use v2 as v1 is deprecated
prep.headers['X-Server-Protocol-Version'] = '2'
# A few (or just one) calls use a query string instead of JSON so we skip
# setting the Content-Type header for those.
if not params:
prep.headers['Content-Type'] = 'application/json;charset=UTF8'
return prep
评论列表
文章目录