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 put(self, alias, uri, data=None, headers=None):
142 """ Send a PUT 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 PUT request to
149 `headers` a dictionary of headers to use with the request
153 session = self._cache.switch(alias)
154 #data = self._utf8_urlencode(data)
155 data = json.dumps(data)
157 resp = session.put(self._get_url(session, uri),
158 data=data, headers=headers,
159 cookies=self.cookies, timeout=self.timeout)
161 self.builtin.log("PUT response: %s DEBUG" % resp.content)
163 # store the last response object
164 session.last_resp = resp
167 def put_xml(self, alias, uri, data=None, headers=None):
168 """ Send a PUT_xml request on the session object found using the
171 `alias` that will be used to identify the Session object in the cache
173 `uri` to send the PUT_xml request to
175 `headers` a dictionary of headers to use with the request
179 session = self._cache.switch(alias)
180 data = self._utf8_urlencode(data)
181 #data = json.dumps(data)
183 resp = session.put(self._get_url(session, uri),
184 data=data, headers=headers,
185 cookies=self.cookies, timeout=self.timeout)
187 self.builtin.log("PUT response: %s DEBUG" % resp.content)
189 # store the last response object
190 session.last_resp = resp
193 def delete(self, alias, uri, data=(), headers=None):
194 """ Send a DELETE request on the session object found using the
197 `alias` that will be used to identify the Session object in the cache
199 `uri` to send the DELETE request to
201 `headers` a dictionary of headers to use with the request
205 session = self._cache.switch(alias)
206 args = "?%s" % urlencode(data) if data else ''
207 resp = session.delete("%s%s" % (self._get_url(session, uri), args),
208 headers=headers, cookies=self.cookies,
209 timeout=self.timeout)
211 # store the last response object
212 session.last_resp = resp
216 def head(self, alias, uri, headers=None):
217 """ Send a HEAD request on the session object found using the
220 `alias` that will be used to identify the Session object in the cache
222 `uri` to send the HEAD request to
224 `headers` a dictionary of headers to use with the request
228 session = self._cache.switch(alias)
229 resp = session.head(self._get_url(session, uri), headers=headers,
230 cookies=self.cookies, timeout=self.timeout)
232 # store the last response object
233 session.last_resp = resp