1 import urllib2
2 import urllib
3 import json
4
5
7 """
8 Helper class for sending requests of HTTP with GET and POST
9 """
10
11 api_url = ""
12 api_method = ""
13 api_params = ""
14 api_header = {}
15 api_post_payload = {}
16 logger = None
17 api_path_params = ""
18 formatted_params = ""
19
21 """
22 Constructor for class
23
24 Args :
25 api_url : (str) URL to the base of the API
26 logger : (logger) instance of a logging object configured in testing project
27
28 """
29
30 self.api_url = api_url
31 self.logger = logger
32
34 """
35 Constucts a request with the parameters formated in path parameter format.
36 EX: http://myserver.com/api/method/pathParam1/pathParam2
37
38 Args :(list) a list of parameter values to send to the api
39
40 Returns :null
41
42 Raises :null
43
44 """
45
46 path_params = ""
47
48 for param in params:
49 path_params = path_params + "/" + param
50
51
52 self.formatted_params = urllib.quote_plus(path_params, "/") + self.formatted_params
53
55 """
56 Constucts a request with the parameters formated in query parameter format.
57 EX: http://myserver.com/api/method?queryParam=paramValue
58
59 Args :(dict) a dictionary of parameter names and values to send to the api
60
61 Returns :null
62
63 Raises :null
64
65 """
66
67 encoded_param = urllib.urlencode(params)
68
69
70 self.formatted_params = self.formatted_params + "?" + encoded_param
71
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
104 """
105 Sends a POST HTTP request. Make sure you have set the classe's api_params,api_url,
106 api_header and api_method first
107
108 Args :null
109
110 Returns : (file-like object) the HTTP response
111
112 Raises :null
113 """
114 url = self.api_url + self.api_method
115 request = urllib2.Request(url)
116
117 encoded_param = urllib.urlencode(self.api_params)
118 request.add_data(encoded_param)
119
120 if self.api_post_payload:
121 request.add_data(json.dumps(self.api_post_payload))
122
123 for key in self.api_header.keys():
124 request.add_header(key, self.api_header[key])
125
126 try:
127
128 self.logger.info(request.get_full_url())
129 self.logger.info(request.get_data())
130 self.logger.info(request.get_method())
131 return urllib2.urlopen(request)
132
133 except urllib2.URLError, e:
134 return e
135 finally:
136 self.formatted_params = ""
137
139 """
140 Creates the authentication for accessing HTTP
141
142 Args :
143 credentials : (dict) 'username' and 'password' keys for a dictionary containing the HTTP Basic Auth to access
144 the api
145
146 Returns : null
147
148 Raises :null
149 """
150
151
152 passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
153
154 passman.add_password(None, self.api_url, credentials['username'], credentials['password'])
155
156
157 authhandler = urllib2.HTTPBasicAuthHandler(passman)
158 opener = urllib2.build_opener(authhandler)
159 urllib2.install_opener(opener)
160