55
66
77class HttpRequestService (object ):
8- def get_response (self , url , auth , logger = None ):
8+ def get_response (self , url , auth , logger ):
99 """
1010 :param str url:
1111 :param HttpAuth auth:
@@ -14,37 +14,48 @@ def get_response(self, url, auth, logger=None):
1414 """
1515 is_gitlab_url = is_gitlab_rest_url (url )
1616 if is_gitlab_url :
17+ logger .info ("=== GITLAB Rest API request ===" .format (url ))
1718 response = self ._get_gitlab_response (url , auth , logger )
1819 self ._validate_response_status_code (response )
1920 self ._invalidate_gitlab_login_page (response )
2021 else :
22+ # if auth:
23+ # if not auth.username:
24+ # raise Exception("Auth download missing 'User' attribute value.")
25+
2126 auth = (auth .username , auth .password ) if auth else None
22- response = requests .get (url , auth = auth , stream = True , verify = False )
27+ if auth :
28+ logger .info ("Auth download flow." )
29+ response = requests .get (url , auth = auth , stream = True , verify = False )
30+ else :
31+ logger .info ("No-auth download flow." )
32+ response = requests .get (url , stream = True , verify = False )
2333 self ._validate_response_status_code (response )
2434 self ._invalidate_html (response .content )
35+
36+ logger .info ("Playbook download response: {}" .format (response .status_code ))
2537 return response
2638
2739 @staticmethod
28- def _get_gitlab_response (url , auth , logger = None ):
40+ def _get_gitlab_response (url , auth , logger ):
2941 """
3042 :param url:
3143 :param auth:
3244 :param logging.Logger logger:
3345 :return:
3446 """
35- # GITLAB REST API CALL - ADDING TOKEN HEADER
36- if logger :
37- logger .info ("downloading script via Gitlab Rest API call: {}" .format (url ))
3847 if auth :
48+ logger .info ("Gitlab download from private repo with token..." )
3949 headers = {"PRIVATE-TOKEN" : auth .password }
4050 return requests .get (url , stream = True , verify = False , headers = headers )
4151 else :
52+ logger .info ("Gitlab no auth download..." )
4253 return requests .get (url , stream = True , verify = False )
4354
4455 @staticmethod
4556 def _validate_response_status_code (response ):
4657 if not response .ok :
47- raise Exception ('Failed to download script file: ' + str (response .status_code ) + ' ' + response .reason +
58+ raise Exception ('Failed to download script file: ' + str (response .status_code ) + ' - ' + response .reason +
4859 '. Please make sure the URL is valid, and any required credentials are correct.' )
4960
5061 @staticmethod
@@ -57,20 +68,8 @@ def _invalidate_html(self, content):
5768
5869 def _invalidate_gitlab_login_page (self , response ):
5970 """
60-
6171 :param Response response: requests response object
6272 :return:
6373 """
6474 if self ._is_content_html (response .content ) and "users/sign_in" in response .url :
6575 raise Exception ('Authentication failed. Reached Gitlab Login. Gitlab Access Token required.' )
66-
67-
68- if __name__ == "__main__" :
69- TEST_URL = "http://192.168.85.62/api/v4/projects/2/repository/files/my_playbook.yml/raw?ref=master"
70- TEST_GITLAB_TOKEN = "UAAEwzj7qSZn3oHRjzbZ"
71- # TEST_GITLAB_TOKEN = "UAAEwzj7qSZn3oHRjzbZBROKEN"
72- auth = HttpAuth (username = "" , password = TEST_GITLAB_TOKEN )
73- service = HttpRequestService ()
74- response = service .get_response (TEST_URL , auth )
75- print (response .ok )
76- pass
0 commit comments