当前位置: 首页>>代码示例>>Python>>正文


Python HTTPSConnectionPool.get_url方法代码示例

本文整理汇总了Python中urllib3.HTTPSConnectionPool.get_url方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPSConnectionPool.get_url方法的具体用法?Python HTTPSConnectionPool.get_url怎么用?Python HTTPSConnectionPool.get_url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在urllib3.HTTPSConnectionPool的用法示例。


在下文中一共展示了HTTPSConnectionPool.get_url方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from urllib3 import HTTPSConnectionPool [as 别名]
# 或者: from urllib3.HTTPSConnectionPool import get_url [as 别名]
class RapleafApi:
			 
	headers = {'User-Agent' : 'RapleafApi/Python/1.1'}
	basePath = '/v4/dr'
	host = 'personalize.rapleaf.com'
	timeout = 2.0
	
	def __init__(self, apiKey):
		self.handle = HTTPSConnectionPool(RapleafApi.host, timeout = RapleafApi.timeout)
		self.basePath = RapleafApi.basePath + '?api_key=%s' % (apiKey)
	
	def query_by_email(self, email, hash_email = False, show_available = False):
		"""
		Takes an e-mail and returns a hash which maps attribute fields onto attributes
		If the hash_email option is set, then the email will be hashed before it's sent to Rapleaf.
		If the show_available option is set, then the string "Data Available" will be returned for 
		those fields which the API account is not subscribed but for which RapLeaf has data.
		"""
		if hash_email:
			s = hashlib.sha1()
			s.update(email.lower())
			return self.query_by_sha1(s.hexdigest(), show_available)
		url = '%s&email=%s' % (self.basePath, urllib.quote(email))
		return self.__get_json_response(url, show_available)
	
	def query_by_md5(self, md5_email, show_available = False):
		"""
		Takes an e-mail that has already been hashed by md5
		and returns a hash which maps attribute fields onto attributes.
		If the show_available option is set, then the string "Data Available" will be returned for 
		those fields which the API account is not subscribed but for which RapLeaf has data.
		"""
		url = '%s&md5_email=%s' % (self.basePath, urllib.quote(md5_email))
		return self.__get_json_response(url, show_available)
	
	def query_by_sha1(self, sha1_email, show_available = False):
		"""
		Takes an e-mail that has already been hashed by sha1
		and returns a hash which maps attribute fields onto attributes.
		If the show_available option is set, then the string "Data Available" will be returned for 
		those fields which the API account is not subscribed but for which RapLeaf has data.
		"""
		url = '%s&sha1_email=%s' % (self.basePath, urllib.quote(sha1_email))
		return self.__get_json_response(url, show_available)
		
	def query_by_nap(self, first, last, street, city, state, email = None, show_available = False):
		"""
		Takes first name, last name, and postal (street, city, and state acronym),
		and returns a hash which maps attribute fields onto attributes
		Though not necessary, adding an e-mail increases hit rate.
		If the show_available option is set, then the string "Data Available" will be returned for 
		those fields which the API account is not subscribed but for which RapLeaf has data.
		"""
		if email:
			url = '%s&email=%s&first=%s&last=%s&street=%s&city=%s&state=%s' % (self.basePath, 
			urllib.quote(email), urllib.quote(first), urllib.quote(last), 
			urllib.quote(street), urllib.quote(city), urllib.quote(state))
		else:
			url = '%s&first=%s&last=%s&street=%s&city=%s&state=%s' % (self.basePath, 
			urllib.quote(first), urllib.quote(last), 
			urllib.quote(street), urllib.quote(city), urllib.quote(state))
		return self.__get_json_response(url, show_available)
	
	def query_by_naz(self, first, last, zip4, email = None, show_available = False):
		"""
		Takes first name, last name, and zip4 code (5-digit zip 
		and 4-digit extension separated by a dash as a string),
		and returns a hash which maps attribute fields onto attributes
		Though not necessary, adding an e-mail increases hit rate.
		If the show_available option is set, then the string "Data Available" will be returned for 
		those fields which the API account is not subscribed but for which RapLeaf has data.
		"""
		if email:
			url = '%s&email=%s&first=%s&last=%s&zip4=%s' % (self.basePath, 
			urllib.quote(email), urllib.quote(first), urllib.quote(last), zip4)
		else:
			url = '%s&first=%s&last=%s&zip4=%s' % (self.basePath, 
			urllib.quote(first), urllib.quote(last), zip4)
		return self.__get_json_response(url, show_available)
		
	def __get_json_response(self, path, show_available = False):
		"""
		Pre: Path is an extension to personalize.rapleaf.com
    Note that an exception is raised if an HTTP response code
    other than 200 is sent back. In this case, both the error code
    the error code and error body are accessible from the exception raised
		"""
		if show_available:
			path += '&show_available'
		json_response = self.handle.get_url(path, headers = RapleafApi.headers)
		if 200 <= json_response.status < 300:
			if json_response.data:
				return json.JSONDecoder().decode(json_response.data)
			else:
				return {}
		else:
			raise Exception(json_response.status, json_response.data)
开发者ID:abhirajchauhan,项目名称:Personalization-Dev-Kits,代码行数:99,代码来源:RapleafApi.py


注:本文中的urllib3.HTTPSConnectionPool.get_url方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。