def put_key(self, uid, key_data, api_uri, api_version):
"""
Send a PUT request to C{uri} containing C{data}.
The request will be sent using the configured CA certificate path to
verify the server certificate and the configured session id for
authentication.
:param uid: The URI of the request.
:type uid: str
:param key_data: The body of the request.
:type key_data: dict, str or file
:return: A deferred that will be fired when PUT request finishes
:rtype: Deferred
"""
data = {
self.PUBKEY_KEY: key_data
}
uri = "%s/%s/users/%s.json" % (
api_uri,
api_version,
uid)
leap_assert(
self.token is not None,
'We need a token to interact with webapp!')
if type(data) == dict:
data = urllib.urlencode(data)
headers = {'Authorization': [str('Token token=%s' % self.token)]}
headers['Content-Type'] = ['application/x-www-form-urlencoded']
try:
res = yield self._async_client_pinned.request(str(uri), 'PUT',
body=str(data),
headers=headers)
except Exception as e:
self.log.warn('Error uploading key: %r' % (e,))
raise e
if 'error' in res:
# FIXME: That's a workaround for 500,
# we need to implement a readBody to assert response code
self.log.warn('Error uploading key: %r' % (res,))
raise Exception(res)
评论列表
文章目录