@@ -395,15 +395,9 @@ def enable_debug(self) -> None:
395
395
requests_log .setLevel (logging .DEBUG )
396
396
requests_log .propagate = True
397
397
398
- def _create_headers (self , content_type : Optional [str ] = None ) -> Dict [str , Any ]:
399
- request_headers = self .headers .copy ()
400
- if content_type is not None :
401
- request_headers ["Content-type" ] = content_type
402
- return request_headers
403
-
404
- def _get_session_opts (self , content_type : str ) -> Dict [str , Any ]:
398
+ def _get_session_opts (self ) -> Dict [str , Any ]:
405
399
return {
406
- "headers" : self ._create_headers ( content_type ),
400
+ "headers" : self .headers . copy ( ),
407
401
"auth" : self ._http_auth ,
408
402
"timeout" : self .timeout ,
409
403
"verify" : self .ssl_verify ,
@@ -443,12 +437,39 @@ def _check_redirects(self, result: requests.Response) -> None:
443
437
if location and location .startswith ("https://" ):
444
438
raise gitlab .exceptions .RedirectError (REDIRECT_MSG )
445
439
440
+ def _prepare_send_data (
441
+ self ,
442
+ files : Dict [str , Any ] = None ,
443
+ post_data : Dict [str , Any ] = None ,
444
+ raw : Optional [bool ] = False ,
445
+ ) -> Tuple :
446
+ if files :
447
+ if post_data is None :
448
+ post_data = {}
449
+ else :
450
+ # booleans does not exists for data (neither for MultipartEncoder):
451
+ # cast to string int to avoid: 'bool' object has no attribute 'encode'
452
+ for k , v in post_data .items ():
453
+ if isinstance (v , bool ):
454
+ post_data [k ] = str (int (v ))
455
+ post_data ["file" ] = files .get ("file" )
456
+ post_data ["avatar" ] = files .get ("avatar" )
457
+
458
+ data = MultipartEncoder (post_data )
459
+ return (None , data , data .content_type )
460
+
461
+ if raw and post_data :
462
+ return (None , post_data , "application/octet-stream" )
463
+
464
+ return (post_data , None , "application/json" )
465
+
446
466
def http_request (
447
467
self ,
448
468
verb : str ,
449
469
path : str ,
450
470
query_data : Optional [Dict [str , Any ]] = None ,
451
471
post_data : Optional [Dict [str , Any ]] = None ,
472
+ raw : Optional [bool ] = False ,
452
473
streamed : bool = False ,
453
474
files : Optional [Dict [str , Any ]] = None ,
454
475
timeout : Optional [float ] = None ,
@@ -466,7 +487,8 @@ def http_request(
466
487
'http://whatever/v4/api/projecs')
467
488
query_data (dict): Data to send as query parameters
468
489
post_data (dict): Data to send in the body (will be converted to
469
- json)
490
+ json by default)
491
+ raw (bool): If True, do not convert post_data to json
470
492
streamed (bool): Whether the data should be streamed
471
493
files (dict): The files to send to the server
472
494
timeout (float): The timeout, in seconds, for the request
@@ -505,7 +527,7 @@ def http_request(
505
527
else :
506
528
utils .copy_dict (params , kwargs )
507
529
508
- opts = self ._get_session_opts (content_type = "application/json" )
530
+ opts = self ._get_session_opts ()
509
531
510
532
verify = opts .pop ("verify" )
511
533
opts_timeout = opts .pop ("timeout" )
@@ -514,23 +536,8 @@ def http_request(
514
536
timeout = opts_timeout
515
537
516
538
# We need to deal with json vs. data when uploading files
517
- if files :
518
- json = None
519
- if post_data is None :
520
- post_data = {}
521
- else :
522
- # booleans does not exists for data (neither for MultipartEncoder):
523
- # cast to string int to avoid: 'bool' object has no attribute 'encode'
524
- for k , v in post_data .items ():
525
- if isinstance (v , bool ):
526
- post_data [k ] = str (int (v ))
527
- post_data ["file" ] = files .get ("file" )
528
- post_data ["avatar" ] = files .get ("avatar" )
529
- data = MultipartEncoder (post_data )
530
- opts ["headers" ]["Content-type" ] = data .content_type
531
- else :
532
- json = post_data
533
- data = None
539
+ json , data , content_type = self ._prepare_send_data (files , post_data , raw )
540
+ opts ["headers" ]["Content-type" ] = content_type
534
541
535
542
# Requests assumes that `.` should not be encoded as %2E and will make
536
543
# changes to urls using this encoding. Using a prepped request we can
@@ -685,6 +692,7 @@ def http_post(
685
692
path : str ,
686
693
query_data : Optional [Dict [str , Any ]] = None ,
687
694
post_data : Optional [Dict [str , Any ]] = None ,
695
+ raw : Optional [bool ] = False ,
688
696
files : Optional [Dict [str , Any ]] = None ,
689
697
** kwargs : Any ,
690
698
) -> Union [Dict [str , Any ], requests .Response ]:
@@ -695,7 +703,8 @@ def http_post(
695
703
'http://whatever/v4/api/projecs')
696
704
query_data (dict): Data to send as query parameters
697
705
post_data (dict): Data to send in the body (will be converted to
698
- json)
706
+ json by default)
707
+ raw (bool): If True, do not convert post_data to json
699
708
files (dict): The files to send to the server
700
709
**kwargs: Extra options to send to the server (e.g. sudo)
701
710
@@ -732,6 +741,7 @@ def http_put(
732
741
path : str ,
733
742
query_data : Optional [Dict [str , Any ]] = None ,
734
743
post_data : Optional [Dict [str , Any ]] = None ,
744
+ raw : Optional [bool ] = False ,
735
745
files : Optional [Dict [str , Any ]] = None ,
736
746
** kwargs : Any ,
737
747
) -> Union [Dict [str , Any ], requests .Response ]:
@@ -742,7 +752,8 @@ def http_put(
742
752
'http://whatever/v4/api/projecs')
743
753
query_data (dict): Data to send as query parameters
744
754
post_data (dict): Data to send in the body (will be converted to
745
- json)
755
+ json by default)
756
+ raw (bool): If True, do not convert post_data to json
746
757
files (dict): The files to send to the server
747
758
**kwargs: Extra options to send to the server (e.g. sudo)
748
759
@@ -762,6 +773,7 @@ def http_put(
762
773
query_data = query_data ,
763
774
post_data = post_data ,
764
775
files = files ,
776
+ raw = raw ,
765
777
** kwargs ,
766
778
)
767
779
try :
0 commit comments