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

Source Code for Module tlib.base.HttpRequestor

  1  import urllib2 
  2  import urllib 
  3  import json 
  4  from logging import Logger 
  5  from TLibHelper import deprecated 
6 7 8 -class HttpRequestor(object):
9 """ 10 Note: This class is deprecated. New projects should use requests library instead\n 11 Helper class for sending requests of HTTP with GET and POST 12 """ 13 14 api_url = "" #: domain url to use when sending requests 15 api_method = "" #: api method to use when sending requests 16 api_params = "" #: parameters to send to the method when sending requests 17 api_header = {} #: Authorization to put into header when sending requests 18 api_post_payload = {} #: Payload to be delivered when sending a POST request 19 logger = None #: logger to send loggin information to. Logger comes from pytest test definitions 20 api_path_params = "" #: 21 formatted_params = "" #: 22 23 @deprecated
24 - def __init__(self, api_url, logger):
25 """ 26 Constructor for class 27 28 @param api_url: URL to the base of the API 29 @type api_url: str 30 @param logger: instance of a logging object configured in testing project 31 @type logger: Logger 32 33 """ 34 35 self.api_url = api_url 36 self.logger = logger
37 38 @deprecated
39 - def make_path_params(self, params):
40 """ 41 Constucts a request with the parameters formated in path parameter format. 42 EX: http://myserver.com/api/method/pathParam1/pathParam2 43 44 @param params: a list of parameter values to send to the api 45 @type params: dict 46 """ 47 48 path_params = "" 49 50 for param in params: 51 path_params = path_params + "/" + param 52 53 #path params must be at the start of the parameter list 54 self.formatted_params = urllib.quote_plus(path_params, "/") + self.formatted_params
55 56 @deprecated
57 - def make_query_params(self, params):
58 """ 59 Constucts a request with the parameters formated in query parameter format. 60 EX: http://myserver.com/api/method?queryParam=paramValue 61 62 @param params: a dictionary of parameter names and values to send to the api 63 @type params: dict 64 """ 65 66 encoded_param = urllib.urlencode(params) 67 68 #query params must be at the end of the paramater list 69 self.formatted_params = self.formatted_params + "?" + encoded_param
70 71 @deprecated
72 - def make_get_request(self):
73 """ 74 Sends a GET HTTP request. Make sure you have set the classe's api_params,api_url and api_method first 75 76 Args :null 77 78 Returns : (file-like object) the HTTP response 79 80 Raises :null 81 """ 82 83 url = self.api_url + self.api_method 84 request = urllib2.Request(url + self.formatted_params) 85 86 if len(self.api_header) > 0: 87 for key in self.api_header.keys(): 88 request.add_header(key, self.api_header[key]) 89 90 self.logger.info(request.get_full_url()) 91 self.logger.info(request.get_data()) 92 self.logger.info(request.get_method()) 93 94 try: 95 96 return urllib2.urlopen(request) 97 98 except urllib2.URLError, e: 99 return e 100 finally: 101 self.formatted_params = ""
102 103 @deprecated
104 - def make_post_request(self):
105 """ 106 Sends a POST HTTP request. Make sure you have set the classe's api_params,api_url, 107 api_header and api_method first 108 109 Returns : (file-like object) the HTTP response 110 """ 111 url = self.api_url + self.api_method 112 request = urllib2.Request(url) 113 114 encoded_param = urllib.urlencode(self.api_params) 115 request.add_data(encoded_param) 116 117 if self.api_post_payload: 118 request.add_data(json.dumps(self.api_post_payload)) 119 120 for key in self.api_header.keys(): 121 request.add_header(key, self.api_header[key]) 122 123 try: 124 125 self.logger.info(request.get_full_url()) 126 self.logger.info(request.get_data()) 127 self.logger.info(request.get_method()) 128 return urllib2.urlopen(request) 129 130 except urllib2.URLError, e: 131 return e 132 finally: 133 self.formatted_params = ""
134 135 @deprecated
136 - def get_auth(self, credentials):
137 """ 138 Creates the authentication for accessing HTTP 139 140 @param credentials: 'username' and 'password' keys for a dictionary containing the HTTP Basic Auth to access 141 the api 142 @type credentials: dict 143 """ 144 145 # create a handler for the username / password 146 passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 147 # none is so that all pages under the realm are opened with password 148 passman.add_password(None, self.api_url, credentials['username'], credentials['password']) 149 150 # authenticate and open the website 151 authhandler = urllib2.HTTPBasicAuthHandler(passman) 152 opener = urllib2.build_opener(authhandler) 153 urllib2.install_opener(opener)
154