-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathRequestsKeywords.py
More file actions
1301 lines (1003 loc) · 42.4 KB
/
Copy pathRequestsKeywords.py
File metadata and controls
1301 lines (1003 loc) · 42.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import types
import sys
import requests
from requests.sessions import merge_setting
from requests.cookies import merge_cookies
import logging
from requests.packages.urllib3.util import Retry
import robot
from robot.api import logger
from robot.libraries.BuiltIn import BuiltIn
from RequestsLibrary.compat import httplib, urlencode, PY3
try:
from requests_ntlm import HttpNtlmAuth
except ImportError:
pass
class WritableObject:
''' HTTP stream handler '''
def __init__(self):
self.content = []
def write(self, string):
self.content.append(string)
class RequestsKeywords(object):
"""``RequestsLibrary`` is a [http://code.google.com/p/robotframework/|Robot Framework] test library that uses the [https://github.com/kennethreitz/requests|Requests] HTTP client.
Here is an example testcase
| ***** Settings ***** | | | | |
| Library | Collections | | | |
| Library | RequestsLibrary | | | |
| ***** Test Cases ***** | | | | |
| Get Requests | | | | |
| | Create Session | github | http://api.github.com | |
| | Create Session | google | http://www.google.com | |
| | ${resp}= | Get Request | google | / |
| | Should Be Equal As Strings | ${resp.status_code} | 200 | |
| | ${resp}= | Get Request | github | /users/bulkan |
| | Should Be Equal As Strings | ${resp.status_code} | 200 | |
| | Dictionary Should Contain Value | ${resp.json()} | Bulkan Savun Evcimen | |
"""
ROBOT_LIBRARY_SCOPE = 'Global'
def __init__(self):
self._cache = robot.utils.ConnectionCache('No sessions created')
self.builtin = BuiltIn()
self.debug = 0
def _create_session(
self,
alias,
url,
headers,
cookies,
auth,
timeout,
max_retries,
backoff_factor,
proxies,
verify,
debug,
disable_warnings):
""" Create Session: create a HTTP session to a server
``url`` Base url of the server
``alias`` Robot Framework alias to identify the session
``headers`` Dictionary of default headers
``cookies`` Dictionary of cookies
``auth`` List of username & password for HTTP Basic Auth
``timeout`` Connection timeout
``max_retries`` The maximum number of retries each connection should attempt.
``backoff_factor`` The pause between for each retry
``proxies`` Dictionary that contains proxy urls for HTTP and HTTPS communication
``verify`` Whether the SSL cert will be verified. A CA_BUNDLE path can also be provided.
It will disable warnings about self signed certs too.
``debug`` Enable http verbosity option more information
https://docs.python.org/2/library/httplib.html#httplib.HTTPConnection.set_debuglevel
``disable_warnings`` Disable requests warning useful when you have large number of testcases
"""
self.builtin.log('Creating session: %s' % alias, 'DEBUG')
s = session = requests.Session()
s.headers.update(headers)
s.auth = auth if auth else s.auth
s.proxies = proxies if proxies else s.proxies
try:
max_retries = int(max_retries)
except ValueError as err:
raise ValueError("Error converting max_retries parameter: %s" % err)
if max_retries > 0:
http = requests.adapters.HTTPAdapter(max_retries=Retry(total=max_retries, backoff_factor=backoff_factor))
https = requests.adapters.HTTPAdapter(max_retries=Retry(total=max_retries, backoff_factor=backoff_factor))
# Replace the session's original adapters
s.mount('http://', http)
s.mount('https://', https)
# Disable requests warnings, useful when you have large number of testcase
# you will observe drastical changes in Robot log.html and output.xml files size
if disable_warnings:
logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests
logging.getLogger().setLevel(logging.ERROR)
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.ERROR)
requests_log.propagate = True
if not verify:
requests.packages.urllib3.disable_warnings()
# verify can be a Boolean or a String
if isinstance(verify, bool):
s.verify = verify
if not verify:
requests.packages.urllib3.disable_warnings()
elif isinstance(verify, str) or isinstance(verify, unicode):
if verify.lower() == 'true' or verify.lower() == 'false':
s.verify = self.builtin.convert_to_boolean(verify)
else:
# String for CA_BUNDLE, not a Boolean String
s.verify = verify
else:
# not a Boolean nor a String
s.verify = verify
# cant pass these into the Session anymore
self.timeout = float(timeout) if timeout is not None else None
self.cookies = cookies
self.verify = verify if self.builtin.convert_to_boolean(verify) != True else None
s.url = url
# Enable http verbosity
if int(debug) >= 1:
self.debug = int(debug)
httplib.HTTPConnection.debuglevel = self.debug
self._cache.register(session, alias=alias)
return session
def create_session(self, alias, url, headers={}, cookies={},
auth=None, timeout=None, proxies=None,
verify=False, debug=0, max_retries=3, backoff_factor=0.10, disable_warnings=0):
""" Create Session: create a HTTP session to a server
``url`` Base url of the server
``alias`` Robot Framework alias to identify the session
``headers`` Dictionary of default headers
``cookies`` Dictionary of cookies
``auth`` List of username & password for HTTP Basic Auth
``timeout`` Connection timeout
``proxies`` Dictionary that contains proxy urls for HTTP and HTTPS communication
``verify`` Whether the SSL cert will be verified. A CA_BUNDLE path can also be provided.
Defaults to False.
``debug`` Enable http verbosity option more information
https://docs.python.org/2/library/httplib.html#httplib.HTTPConnection.set_debuglevel
``max_retries`` The maximum number of retries each connection should attempt.
``backoff_factor`` The pause between for each retry
``disable_warnings`` Disable requests warning useful when you have large number of testcases
"""
auth = requests.auth.HTTPBasicAuth(*auth) if auth else None
logger.info('Creating Session using : alias=%s, url=%s, headers=%s, \
cookies=%s, auth=%s, timeout=%s, proxies=%s, verify=%s, \
debug=%s ' % (alias, url, headers, cookies, auth, timeout,
proxies, verify, debug))
return self._create_session(
alias,
url,
headers,
cookies,
auth,
timeout,
max_retries,
backoff_factor,
proxies,
verify,
debug,
disable_warnings)
def create_custom_session(
self,
alias,
url,
auth,
headers={},
cookies={},
timeout=None,
proxies=None,
verify=False,
debug=0,
max_retries=3,
backoff_factor=0.10,
disable_warnings=0):
""" Create Session: create a HTTP session to a server
``url`` Base url of the server
``alias`` Robot Framework alias to identify the session
``headers`` Dictionary of default headers
``cookies`` Dictionary of cookies
``auth`` A Custom Authentication object to be passed on to the reqests library.
http://docs.python-requests.org/en/master/user/advanced/#custom-authentication
``timeout`` Connection timeout
``proxies`` Dictionary that contains proxy urls for HTTP and HTTPS communication
``verify`` Whether the SSL cert will be verified. A CA_BUNDLE path can also be provided.
Defaults to False.
``debug`` Enable http verbosity option more information
https://docs.python.org/2/library/httplib.html#httplib.HTTPConnection.set_debuglevel
``max_retries`` The maximum number of retries each connection should attempt.
``backoff_factor`` The pause between for each retry
``disable_warnings`` Disable requests warning useful when you have large number of testcases
"""
logger.info('Creating Custom Authenticated Session using : alias=%s, url=%s, headers=%s, \
cookies=%s, auth=%s, timeout=%s, proxies=%s, verify=%s, \
debug=%s ' % (alias, url, headers, cookies, auth, timeout,
proxies, verify, debug))
return self._create_session(
alias,
url,
headers,
cookies,
auth,
timeout,
max_retries,
backoff_factor,
proxies,
verify,
debug,
disable_warnings)
def create_ntlm_session(
self,
alias,
url,
auth,
headers={},
cookies={},
timeout=None,
proxies=None,
verify=False,
debug=0,
max_retries=3,
backoff_factor=0.10,
disable_warnings=0):
""" Create Session: create a HTTP session to a server
``url`` Base url of the server
``alias`` Robot Framework alias to identify the session
``headers`` Dictionary of default headers
``cookies`` Dictionary of cookies
``auth`` ['DOMAIN', 'username', 'password'] for NTLM Authentication
``timeout`` Connection timeout
``proxies`` Dictionary that contains proxy urls for HTTP and HTTPS communication
``verify`` Whether the SSL cert will be verified. A CA_BUNDLE path can also be provided.
Defaults to False.
``debug`` Enable http verbosity option more information
https://docs.python.org/2/library/httplib.html#httplib.HTTPConnection.set_debuglevel
``max_retries`` The maximum number of retries each connection should attempt.
``backoff_factor`` The pause between for each retry
``disable_warnings`` Disable requests warning useful when you have large number of testcases
"""
if not HttpNtlmAuth:
raise AssertionError('Requests NTLM module not loaded')
elif len(auth) != 3:
raise AssertionError('Incorrect number of authentication arguments'
' - expected 3, got {}'.format(len(auth)))
else:
ntlm_auth = HttpNtlmAuth('{}\\{}'.format(auth[0], auth[1]),
auth[2])
logger.info('Creating NTLM Session using : alias=%s, url=%s, \
headers=%s, cookies=%s, ntlm_auth=%s, timeout=%s, \
proxies=%s, verify=%s, debug=%s '
% (alias, url, headers, cookies, ntlm_auth,
timeout, proxies, verify, debug))
return self._create_session(
alias,
url,
headers,
cookies,
ntlm_auth,
timeout,
max_retries,
backoff_factor,
proxies,
verify,
debug,
disable_warnings)
def create_digest_session(self, alias, url, auth, headers={}, cookies={},
timeout=None, proxies=None, verify=False,
debug=0, max_retries=3,backoff_factor=0.10, disable_warnings=0):
""" Create Session: create a HTTP session to a server
``url`` Base url of the server
``alias`` Robot Framework alias to identify the session
``headers`` Dictionary of default headers
``cookies`` Dictionary of cookies
``auth`` ['DOMAIN', 'username', 'password'] for NTLM Authentication
``timeout`` Connection timeout
``proxies`` Dictionary that contains proxy urls for HTTP and HTTPS communication
``verify`` Whether the SSL cert will be verified. A CA_BUNDLE path can also be provided.
Defaults to False.
``debug`` Enable http verbosity option more information
https://docs.python.org/2/library/httplib.html#httplib.HTTPConnection.set_debuglevel
``max_retries`` The maximum number of retries each connection should attempt.
``backoff_factor`` The pause between for each retry
``disable_warnings`` Disable requests warning useful when you have large number of testcases
"""
digest_auth = requests.auth.HTTPDigestAuth(*auth) if auth else None
return self._create_session(
alias,
url,
headers,
cookies,
digest_auth,
timeout,
max_retries,
backoff_factor,
proxies,
verify,
debug,
disable_warnings)
def create_client_cert_session(self, alias, url, headers={}, cookies={},
client_certs=None, timeout=None, proxies=None,
verify=False, debug=0, max_retries=3, backoff_factor=0.10, disable_warnings=0):
""" Create Session: create a HTTP session to a server
``url`` Base url of the server
``alias`` Robot Framework alias to identify the session
``headers`` Dictionary of default headers
``cookies`` Dictionary of cookies
``client_certs`` ['client certificate', 'client key'] PEM files containing the client key and certificate
``timeout`` Connection timeout
``proxies`` Dictionary that contains proxy urls for HTTP and HTTPS communication
``verify`` Whether the SSL cert will be verified. A CA_BUNDLE path can also be provided.
Defaults to False.
``debug`` Enable http verbosity option more information
https://docs.python.org/2/library/httplib.html#httplib.HTTPConnection.set_debuglevel
``max_retries`` The maximum number of retries each connection should attempt.
``backoff_factor`` The pause between for each retry
``disable_warnings`` Disable requests warning useful when you have large number of testcases
"""
logger.info('Creating Session using : alias=%s, url=%s, headers=%s, \
cookies=%s, client_certs=%s, timeout=%s, proxies=%s, verify=%s, \
debug=%s ' % (alias, url, headers, cookies, client_certs, timeout,
proxies, verify, debug))
session = self._create_session(
alias,
url,
headers,
cookies,
None,
timeout,
max_retries,
backoff_factor,
proxies,
verify,
debug,
disable_warnings)
session.cert = tuple(client_certs)
return session
def delete_all_sessions(self):
""" Removes all the session objects """
logger.info('Delete All Sessions')
self._cache.empty_cache()
def update_session(self, alias, headers=None, cookies=None):
"""Update Session Headers: update a HTTP Session Headers
``alias`` Robot Framework alias to identify the session
``headers`` Dictionary of headers merge into session
"""
session = self._cache.switch(alias)
session.headers = merge_setting(headers, session.headers)
session.cookies = merge_cookies(session.cookies, cookies)
def to_json(self, content, pretty_print=False):
""" Convert a string to a JSON object
``content`` String content to convert into JSON
``pretty_print`` If defined, will output JSON is pretty print format
"""
if PY3:
if isinstance(content, bytes):
content = content.decode(encoding='utf-8')
if pretty_print:
json_ = self._json_pretty_print(content)
else:
json_ = json.loads(content)
logger.info('To JSON using : content=%s ' % (content))
logger.info('To JSON using : pretty_print=%s ' % (pretty_print))
return json_
def get_request(
self,
alias,
uri,
headers=None,
json=None,
params=None,
allow_redirects=None,
timeout=None):
""" Send a GET request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the GET request to
``params`` url parameters to append to the uri
``headers`` a dictionary of headers to use with the request
``json`` json data to send in the body of the :class:`Request`.
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``timeout`` connection timeout
"""
session = self._cache.switch(alias)
redir = True if allow_redirects is None else allow_redirects
response = self._get_request(
session, uri, params, headers, json, redir, timeout)
logger.info(
'Get Request using : alias=%s, uri=%s, headers=%s json=%s' %
(alias, uri, headers, json))
return response
def get(
self,
alias,
uri,
params=None,
headers=None,
allow_redirects=None,
timeout=None):
""" **Deprecated- See Get Request now**
Send a GET request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the GET request to
``headers`` a dictionary of headers to use with the request
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``timeout`` connection timeout
"""
logger.warn("Deprecation Warning: Use Get Request in the future")
session = self._cache.switch(alias)
redir = True if allow_redirects is None else allow_redirects
response = self._get_request(
session, uri, params, headers, redir, timeout, json)
return response
def post_request(
self,
alias,
uri,
data=None,
json=None,
params=None,
headers=None,
files=None,
allow_redirects=None,
timeout=None):
""" Send a POST request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the POST request to
``data`` a dictionary of key-value pairs that will be urlencoded
and sent as POST data
or binary data that is sent as the raw body content
or passed as such for multipart form data if ``files`` is also
defined
``json`` a value that will be json encoded
and sent as POST data if files or data is not specified
``params`` url parameters to append to the uri
``headers`` a dictionary of headers to use with the request
``files`` a dictionary of file names containing file data to POST to the server
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``timeout`` connection timeout
"""
session = self._cache.switch(alias)
if not files:
data = self._format_data_according_to_header(session, data, headers)
redir = True if allow_redirects is None else allow_redirects
response = self._body_request(
"post",
session,
uri,
data,
json,
params,
files,
headers,
redir,
timeout)
dataStr = self._format_data_to_log_string_according_to_header(data, headers)
logger.info('Post Request using : alias=%s, uri=%s, data=%s, headers=%s, files=%s, allow_redirects=%s '
% (alias, uri, dataStr, headers, files, redir))
return response
def post(
self,
alias,
uri,
data={},
headers=None,
files=None,
allow_redirects=None,
timeout=None):
""" **Deprecated- See Post Request now**
Send a POST request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the GET request to
``data`` a dictionary of key-value pairs that will be urlencoded
and sent as POST data
or binary data that is sent as the raw body content
``headers`` a dictionary of headers to use with the request
``files`` a dictionary of file names containing file data to POST to the server
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``timeout`` connection timeout
"""
logger.warn("Deprecation Warning: Use Post Request in the future")
session = self._cache.switch(alias)
data = self._utf8_urlencode(data)
redir = True if allow_redirects is None else allow_redirects
response = self._body_request(
"post",
session,
uri,
data,
None,
None,
files,
headers,
redir,
timeout)
return response
def patch_request(
self,
alias,
uri,
data=None,
json=None,
params=None,
headers=None,
files=None,
allow_redirects=None,
timeout=None):
""" Send a PATCH request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the PATCH request to
``data`` a dictionary of key-value pairs that will be urlencoded
and sent as PATCH data
or binary data that is sent as the raw body content
``json`` a value that will be json encoded
and sent as PATCH data if data is not specified
``headers`` a dictionary of headers to use with the request
``files`` a dictionary of file names containing file data to PATCH to the server
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``params`` url parameters to append to the uri
``timeout`` connection timeout
"""
session = self._cache.switch(alias)
data = self._format_data_according_to_header(session, data, headers)
redir = True if allow_redirects is None else allow_redirects
response = self._body_request(
"patch",
session,
uri,
data,
json,
params,
files,
headers,
redir,
timeout)
if isinstance(data, bytes):
data = data.decode('utf-8')
logger.info('Patch Request using : alias=%s, uri=%s, data=%s, \
headers=%s, files=%s, allow_redirects=%s '
% (alias, uri, data, headers, files, redir))
return response
def patch(
self,
alias,
uri,
data={},
headers=None,
files={},
allow_redirects=None,
timeout=None):
""" **Deprecated- See Patch Request now**
Send a PATCH request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the PATCH request to
``data`` a dictionary of key-value pairs that will be urlencoded
and sent as PATCH data
or binary data that is sent as the raw body content
``headers`` a dictionary of headers to use with the request
``files`` a dictionary of file names containing file data to PATCH to the server
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``timeout`` connection timeout
"""
logger.warn("Deprecation Warning: Use Patch Request in the future")
session = self._cache.switch(alias)
data = self._utf8_urlencode(data)
redir = True if allow_redirects is None else allow_redirects
response = self._body_request(
"patch",
session,
uri,
data,
None,
None,
files,
headers,
redir,
timeout)
return response
def put_request(
self,
alias,
uri,
data=None,
json=None,
params=None,
files=None,
headers=None,
allow_redirects=None,
timeout=None):
""" Send a PUT request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the PUT request to
``data`` a dictionary of key-value pairs that will be urlencoded
and sent as PUT data
or binary data that is sent as the raw body content
``json`` a value that will be json encoded
and sent as PUT data if data is not specified
``headers`` a dictionary of headers to use with the request
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``params`` url parameters to append to the uri
``timeout`` connection timeout
"""
session = self._cache.switch(alias)
data = self._format_data_according_to_header(session, data, headers)
redir = True if allow_redirects is None else allow_redirects
response = self._body_request(
"put",
session,
uri,
data,
json,
params,
files,
headers,
redir,
timeout)
if isinstance(data, bytes):
data = data.decode('utf-8')
logger.info('Put Request using : alias=%s, uri=%s, data=%s, \
headers=%s, allow_redirects=%s ' % (alias, uri, data, headers, redir))
return response
def put(
self,
alias,
uri,
data=None,
headers=None,
allow_redirects=None,
timeout=None):
""" **Deprecated- See Put Request now**
Send a PUT request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the PUT request to
``headers`` a dictionary of headers to use with the request
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``timeout`` connection timeout
"""
logger.warn("Deprecation Warning: Use Put Request in the future")
session = self._cache.switch(alias)
data = self._utf8_urlencode(data)
redir = True if allow_redirects is None else allow_redirects
response = self._body_request(
"put",
session,
uri,
data,
None,
None,
None,
headers,
redir,
timeout)
return response
def delete_request(
self,
alias,
uri,
data=None,
json=None,
params=None,
headers=None,
allow_redirects=None,
timeout=None):
""" Send a DELETE request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the DELETE request to
``json`` a value that will be json encoded
and sent as request data if data is not specified
``headers`` a dictionary of headers to use with the request
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``timeout`` connection timeout
"""
session = self._cache.switch(alias)
data = self._format_data_according_to_header(session, data, headers)
redir = True if allow_redirects is None else allow_redirects
response = self._delete_request(
session, uri, data, json, params, headers, redir, timeout)
if isinstance(data, bytes):
data = data.decode('utf-8')
logger.info('Delete Request using : alias=%s, uri=%s, data=%s, \
headers=%s, allow_redirects=%s ' % (alias, uri, data, headers, redir))
return response
def delete(
self,
alias,
uri,
data=(),
headers=None,
allow_redirects=None,
timeout=None):
""" * * * Deprecated- See Delete Request now * * *
Send a DELETE request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the DELETE request to
``headers`` a dictionary of headers to use with the request
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``timeout`` connection timeout
"""
logger.warn("Deprecation Warning: Use Delete Request in the future")
session = self._cache.switch(alias)
data = self._utf8_urlencode(data)
redir = True if allow_redirects is None else allow_redirects
response = self._delete_request(
session, uri, data, json, None, headers, redir, timeout)
return response
def head_request(
self,
alias,
uri,
headers=None,
allow_redirects=None,
timeout=None):
""" Send a HEAD request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the HEAD request to
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``headers`` a dictionary of headers to use with the request
"""
session = self._cache.switch(alias)
redir = False if allow_redirects is None else allow_redirects
response = self._head_request(session, uri, headers, redir, timeout)
logger.info('Head Request using : alias=%s, uri=%s, headers=%s, \
allow_redirects=%s ' % (alias, uri, headers, redir))
return response
def head(
self,
alias,
uri,
headers=None,
allow_redirects=None,
timeout=None):
""" **Deprecated- See Head Request now**
Send a HEAD request on the session object found using the
given `alias`
``alias`` that will be used to identify the Session object in the cache
``uri`` to send the HEAD request to
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
``headers`` a dictionary of headers to use with the request
"""
logger.warn("Deprecation Warning: Use Head Request in the future")
session = self._cache.switch(alias)
redir = False if allow_redirects is None else allow_redirects
response = self._head_request(session, uri, headers, redir, timeout)
return response
def options_request(
self,
alias,
uri,
headers=None,
allow_redirects=None,
timeout=None):
""" Send an OPTIONS request on the session object found using the