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 self.logger.info(request.get_full_url())
86 self.logger.info(request.get_data())
87 self.logger.info(request.get_method())
88
89 try:
90
91 return urllib2.urlopen(request)
92
93 except urllib2.URLError, e:
94 return e
95 finally:
96 self.formatted_params = ""
97
99 """
100 Sends a POST HTTP request. Make sure you have set the classe's api_params,api_url,
101 api_header and api_method first
102
103 Args :null
104
105 Returns : (file-like object) the HTTP response
106
107 Raises :null
108 """
109 url = self.api_url + self.api_method
110 request = urllib2.Request(url)
111
112 encoded_param = urllib.urlencode(self.api_params)
113 request.add_data(encoded_param)
114
115 if self.api_post_payload:
116 request.add_data(json.dumps(self.api_post_payload))
117
118 for key in self.api_header.keys():
119 request.add_header(key, self.api_header[key])
120
121 try:
122
123 self.logger.info(request.get_full_url())
124 self.logger.info(request.get_data())
125 self.logger.info(request.get_method())
126 return urllib2.urlopen(request)
127
128 except urllib2.URLError, e:
129 return e
130 finally:
131 self.formatted_params = ""
132
134 """
135 Creates the authentication for accessing HTTP
136
137 Args :
138 credentials : (dict) 'username' and 'password' keys for a dictionary containing the HTTP Basic Auth to access
139 the api
140
141 Returns : null
142
143 Raises :null
144 """
145
146
147 passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
148
149 passman.add_password(None, self.api_url, credentials['username'], credentials['password'])
150
151
152 authhandler = urllib2.HTTPBasicAuthHandler(passman)
153 opener = urllib2.build_opener(authhandler)
154 urllib2.install_opener(opener)
155