本文整理汇总了Python中connector.Connector.post方法的典型用法代码示例。如果您正苦于以下问题:Python Connector.post方法的具体用法?Python Connector.post怎么用?Python Connector.post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类connector.Connector
的用法示例。
在下文中一共展示了Connector.post方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FURSBaseAPI
# 需要导入模块: from connector import Connector [as 别名]
# 或者: from connector.Connector import post [as 别名]
class FURSBaseAPI():
def __init__(self, p12_path, p12_password, production=True, request_timeout=2.0):
self.connector = Connector(p12_path=p12_path,
p12_password=p12_password,
production=production,
request_timeout=request_timeout)
def is_server_accessible(self):
"""
Check if FURS server is accessible. Will return False if server responds with anything else
than HTTP Code: 200 or if the request timeouts.
:return: (boolean) True for ok, False if there was a problem accessing server.
"""
try:
return self.connector.send_echo().status_code == codes.ok
except Timeout as e:
return False
def _send_request(self, path, data):
"""
Sends request to the FURS Server and decodes response.
:param path: (string) Server path
:param data: (dict) Data to be sent
:return: (dict) Received response
:raises:
ConnectionTimedOutException: If connection timed out
ConnectionException: If FURS responded with status code different than 200
FURSException: If server responded with error
"""
try:
response = self.connector.post(path=path, json=data)
if response.status_code == codes.ok:
# TODO - we should verify server signature!
server_response = jws.get_unverified_claims(response.json()['token'])
return self._check_for_errors(server_response)
else:
raise ConnectionException(code=response.status_code,
message=response.text)
except Timeout as e:
raise ConnectionTimedOutException(e)
def _check_for_errors(self, server_response):
"""
Check if server response contains FURS Error message and raise FURSException if it does
:param server_response: (dict) FURS Server response data
:return: server_response: (dict) FURS Server response data
:raises
FURSException: If response contains error
"""
if server_response[server_response.keys()[0]].get('Error', None):
raise FURSException(code=server_response[server_response.keys()[0]]['Error']['ErrorCode'],
message=server_response[server_response.keys()[0]]['Error']['ErrorMessage'])
return server_response
def _sign(self, content, digest='SHA256'):
pkey = self.connector.p12.get_privatekey()
return crypto.sign(pkey=pkey, data=content, digest=digest)