Package tlib :: Package base :: Module HttpHelper
[hide private]
[frames] | no frames]

Source Code for Module tlib.base.HttpHelper

  1  from Helper import Helper 
  2  import urllib2, urllib 
  3  import StringIO 
  4  import gzip 
  5  import HTMLParser 
  6  # from qaException.apiQaException import ApiQaException 
  7   
8 -class HttpHelper(Helper):
9 """ 10 Helper class for sending requests of HTTP with GET and POST 11 """ 12 13 14 login_cred = {} #: Auth for HTTP access. type(dict) : {'username':'x','password':'y'} 15 client_cred = {} #: Auth for client credentials. to be added to header. 16 api_url = None #: 17 api_param = None 18 request_type = None 19 #logPath = None 20 responseFormat = None 21 22
23 - def __init__(self,request_type,api_url,login_cred={},client_cred={},request_header={}):
24 """ 25 Args : 26 request_type (str) : Takes POST or GET as type of HTTP requests 27 api_url (str) : The url to the base of the API call (everything but the call itself) 28 login_cred (dict) : Optionally takes a username and password if the HTTP takes authentication 29 30 Returns : null 31 32 Raises : null 33 """ 34 35 Helper.__init__(self) 36 self.request_type = request_type 37 self.api_url = api_url 38 self.login_cred = login_cred 39 self.client_cred = client_cred 40 self.request_header = request_header
41
42 - def getResponse(self,constructed_url):
43 """ 44 Sends the api call and returns the response 45 46 Args : 47 constructed_url: (str) url and parameters to send 48 49 Returns : 50 the_page: (json) response body from the api call 51 code: (int) response code from the HTTP response 52 error: (string) error response if api call returns error 53 54 Raises : urllib2.HTTPError,urllib2.URLError 55 """ 56 57 # Initialize returned variables 58 the_page = None 59 code = None 60 error = None 61 62 # add authentication if you need it 63 if len(self.login_cred) >= 2: 64 self.getAuth(constructed_url) 65 66 try: 67 self.logger.debug("The Url targeted is : %s"%constructed_url) 68 # Send the API call 69 if len(self.request_header) >= 1: 70 constructed_url = urllib2.Request(constructed_url, None, self.request_header) 71 pagehandle = urllib2.urlopen(constructed_url) 72 73 # Get the response status code 74 code = pagehandle.getcode() 75 # Read the response from the request 76 the_page = pagehandle.read() 77 if pagehandle.info().get('Content-Encoding') == 'gzip': 78 buf = StringIO.StringIO(the_page) 79 f = gzip.GzipFile(fileobj=buf) 80 the_page = f.read() 81 82 83 except urllib2.HTTPError, e: 84 85 # Get the error code and message to send back if something went wrong 86 #raise ApiQaException(e) 87 code = e.code 88 error = e.read() 89 except urllib2.URLError, e: 90 #if we get a timeout 91 #code = e.code 92 error = e.message 93 94 return the_page, code, error
95
96 - def getHTTPResponse(self,call,data):
97 """ 98 constructs and sends the api call and returns the response 99 100 Args : 101 call: (str) Name of the api service you are calling 102 data: (list) The parameters you will be providing the api service 103 104 Returns : null 105 106 Raises : null 107 """ 108 109 110 111 # add the API call to the URL 112 theurl = self.api_url+call 113 114 if self.request_type == "GET": 115 constructed_url = self.createUrl(data,theurl) 116 self.logger.debug("Sending a GET HTTP request") 117 elif self.request_type == "POST": 118 self.logger.debug("Sending a POST HTTP request") 119 # If credentials need to be sent in the header 120 if len(self.client_cred) >=2: 121 headers = {"Authorization":self.client_cred['key']+":"+self.client_cred['secretKey']} 122 print headers 123 constructed_url = urllib2.Request(theurl,urllib.urlencode(data),headers) 124 else: 125 constructed_url = urllib2.Request(theurl,urllib.urlencode(data)) 126 127 return self.getResponse(constructed_url)
128 129 130 131
132 - def createUrl(self,data,theurl):
133 """ 134 Helper function to create url in either path or parameter api methods 135 136 Args : 137 data: (str) values sent to api query 138 theurl: (str) the http path to the api method 139 140 Returns : null 141 142 Raises : null 143 """ 144 145 #path args type 146 if isinstance(data,str): 147 self.logger.debug("We are using path arguments for the url parameters") 148 return theurl+'/'+data 149 #query args type 150 elif isinstance(data,dict): 151 url_values = urllib.urlencode(data) 152 self.logger.debug("We are using query arguments for the url parameters") 153 return theurl+'?'+url_values
154 155
156 - def getAuth(self,theurl):
157 """ 158 Creates the authentication for accessing api 159 160 Args : 161 theurl: (str) the http path to the api method 162 163 Returns : null 164 165 Raises :null 166 """ 167 168 # create a handler for the username / password 169 passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 170 # none is so that all pages under the realm are opened with password 171 passman.add_password(None, theurl, self.login_cred['username'], self.login_cred['password']) 172 173 # authenticate and open the website 174 authhandler = urllib2.HTTPBasicAuthHandler(passman) 175 opener = urllib2.build_opener(authhandler) 176 urllib2.install_opener(opener) 177 self.logger.debug("We are using authentication")
178 179
180 - def decodeHtmlString(self,string):
181 """ 182 translates html entities into a readable string 183 184 Args : 185 string: (str) an html entity 186 187 Returns : 188 unescaped: (str) returns a readable string 189 190 Raises :null 191 """ 192 193 h = HTMLParser.HTMLParser() 194 return h.unescape(string)
195