4 from urllib import urlencode
9 from robot.libraries.BuiltIn import BuiltIn
12 class RequestsLibrary(object):
13 ROBOT_LIBRARY_SCOPE = 'Global'
16 self._cache = robot.utils.ConnectionCache('No sessions created')
17 self.builtin = BuiltIn()
19 def _utf8_urlencode(self, data):
20 if not type(data) is dict:
24 for k,v in data.iteritems():
25 utf8_data[k] = unicode(v).encode('utf-8')
26 return urlencode(utf8_data)
28 def create_session(self, alias, url, headers={}, cookies=None,
29 auth=None, timeout=None, proxies=None,
32 """ Create Session: create a HTTP session to a server
34 `url` Base url of the server
36 `alias` Robot Framework alias to identify the session
38 `headers` Dictionary of default headers
40 `auth` Dictionary of username & password for HTTP Basic Auth
42 `timeout` connection timeout
44 `proxies` proxy server url
46 `verify` set to True if Requests should verify the certificate
49 self.builtin.log('Creating session: %s' % alias, 'DEBUG')
50 auth = requests.auth.HTTPBasicAuth(*auth) if auth else None
51 s = session = requests.Session()
52 s.headers.update(headers)
53 s.auth = auth if auth else s.auth
54 s.proxies = proxies if proxies else s.proxies
56 s.verify = self.builtin.convert_to_boolean(verify)
58 # cant pass these into the Session anymore
59 self.timeout = timeout
60 self.cookies = cookies
66 self._cache.register(session, alias=alias)
69 def delete_all_sessions(self):
70 """ Removes all the session objects """
72 self._cache.empty_cache()
74 def to_json(self, content):
75 """ Convert a string to a JSON object
77 `content` String content to convert into JSON
79 return json.loads(content)
82 def _get_url(self, session, uri):
83 ''' Helpere method to get the full url
87 slash = '' if uri.startswith('/') else '/'
88 url = "%s%s%s" %(session.url, slash, uri)
91 def get(self, alias, uri, headers=None):
92 """ Send a GET request on the session object found using the
95 `alias` that will be used to identify the Session object in the cache
97 `uri` to send the GET request to
99 `headers` a dictionary of headers to use with the request
102 session = self._cache.switch(alias)
103 resp = session.get(self._get_url(session, uri),
105 cookies=self.cookies, timeout=self.timeout)
107 # store the last response object
108 session.last_resp = resp
111 def post(self, alias, uri, data={}, headers=None, files={}):
112 """ Send a POST request on the session object found using the
115 `alias` that will be used to identify the Session object in the cache
117 `uri` to send the GET request to
119 `data` a dictionary of key-value pairs that will be urlencoded
120 and sent as POST data
121 or binary data that is sent as the raw body content
123 `headers` a dictionary of headers to use with the request
125 `files` a dictionary of file names containing file data to POST to the server
128 session = self._cache.switch(alias)
129 data = self._utf8_urlencode(data)
131 resp = session.post(self._get_url(session, uri),
132 data=data, headers=headers,
134 cookies=self.cookies, timeout=self.timeout)
136 # store the last response object
137 session.last_resp = resp
138 self.builtin.log("Post response: " + resp.content, 'DEBUG')
141 def postjson(self, alias, uri, data={}, headers=None, files={}):
142 """ Send a POST request on the session object found using the
145 `alias` that will be used to identify the Session object in the cache
147 `uri` to send the GET request to
149 `data` a dictionary of key-value pairs that will be urlencoded
150 and sent as POST data
151 or binary data that is sent as the raw body content
153 `headers` a dictionary of headers to use with the request
155 `files` a dictionary of file names containing file data to POST to the server
158 session = self._cache.switch(alias)
159 data = json.dumps(data)
161 resp = session.post(self._get_url(session, uri),
162 data=data, headers=headers,
164 cookies=self.cookies, timeout=self.timeout)
166 # store the last response object
167 session.last_resp = resp
168 self.builtin.log("Post response: " + resp.content, 'DEBUG')
171 def put(self, alias, uri, data=None, headers=None):
172 """ Send a PUT request on the session object found using the
175 `alias` that will be used to identify the Session object in the cache
177 `uri` to send the PUT request to
179 `headers` a dictionary of headers to use with the request
183 session = self._cache.switch(alias)
184 #data = self._utf8_urlencode(data)
185 data = json.dumps(data)
187 resp = session.put(self._get_url(session, uri),
188 data=data, headers=headers,
189 cookies=self.cookies, timeout=self.timeout)
191 self.builtin.log("PUT response: %s DEBUG" % resp.content)
193 # store the last response object
194 session.last_resp = resp
197 def put_xml(self, alias, uri, data=None, headers=None):
198 """ Send a PUT_xml request on the session object found using the
201 `alias` that will be used to identify the Session object in the cache
203 `uri` to send the PUT_xml request to
205 `headers` a dictionary of headers to use with the request
209 session = self._cache.switch(alias)
210 data = self._utf8_urlencode(data)
211 #data = json.dumps(data)
213 resp = session.put(self._get_url(session, uri),
214 data=data, headers=headers,
215 cookies=self.cookies, timeout=self.timeout)
217 self.builtin.log("PUT response: %s DEBUG" % resp.content)
219 # store the last response object
220 session.last_resp = resp
223 def delete(self, alias, uri, data=(), headers=None):
224 """ Send a DELETE request on the session object found using the
227 `alias` that will be used to identify the Session object in the cache
229 `uri` to send the DELETE request to
231 `headers` a dictionary of headers to use with the request
235 session = self._cache.switch(alias)
236 args = "?%s" % urlencode(data) if data else ''
237 resp = session.delete("%s%s" % (self._get_url(session, uri), args),
238 headers=headers, cookies=self.cookies,
239 timeout=self.timeout)
241 # store the last response object
242 session.last_resp = resp
246 def head(self, alias, uri, headers=None):
247 """ Send a HEAD request on the session object found using the
250 `alias` that will be used to identify the Session object in the cache
252 `uri` to send the HEAD request to
254 `headers` a dictionary of headers to use with the request
258 session = self._cache.switch(alias)
259 resp = session.head(self._get_url(session, uri), headers=headers,
260 cookies=self.cookies, timeout=self.timeout)
262 # store the last response object
263 session.last_resp = resp