Keep the same scheme (protocol) as was used in Jenkins constructor in all requests
authorPetr Kocandrle <pkocandr@redhat.com>
Wed, 24 Jul 2013 12:14:11 +0000 (14:14 +0200)
committersalimfadhley <sal@stodge.org>
Tue, 1 Oct 2013 22:51:38 +0000 (23:51 +0100)
Some Jenkins instances are configured to allow reading the data over HTTP scheme, while for manipulating with
data (e.g. create or delete job) HTTPS scheme is required. Allow user to specify preferred URL scheme (by
specifying Jenkins URL) and use this scheme also manipulations with jobs.

This was introduced in 4f29f6742410736016b568002d8569f19d786841 (version 0.1.13) but lost again in
79934f0b5ca44a1d4ecbaf534eb1add8e343193f.

jenkinsapi/jenkins.py
jenkinsapi/utils/requester.py

index 1b4facb..80371ac 100644 (file)
@@ -32,7 +32,7 @@ class Jenkins(JenkinsBase):
         """
         self.username = username
         self.password = password
-        self.requester = requester or Requester(username, password)
+        self.requester = requester or Requester(username, password, baseurl=baseurl)
         JenkinsBase.__init__(self, baseurl)
 
     def _clone(self):
index 8f2131e..d77ea5c 100644 (file)
@@ -1,4 +1,5 @@
 import requests
+import urlparse
 from jenkinsapi.exceptions import JenkinsAPIException
 # import logging
 
@@ -26,10 +27,11 @@ class Requester(object):
 
     VALID_STATUS_CODES = [200,]
 
-    def __init__(self, username=None, password=None, ssl_verify=True):
+    def __init__(self, username=None, password=None, ssl_verify=True, baseurl=None):
         if username:
             assert password, 'Cannot set a username without a password!'
 
+        self.base_scheme = urlparse.urlsplit(baseurl).scheme if baseurl else None
         self.username = username
         self.password = password
         self.ssl_verify = ssl_verify
@@ -57,13 +59,24 @@ class Requester(object):
             requestKwargs['data'] = data
         return requestKwargs
 
+    def _update_url_scheme(self, url):
+        """
+        Updates scheme of given url to the one used in Jenkins baseurl.
+        """
+        if self.base_scheme and not url.startswith("%s://" % self.base_scheme):
+            url_split = urlparse.urlsplit(url)
+            url = urlparse.urlunsplit([self.base_scheme, url_split.netloc, url_split.path, url_split.query,
+                                       url_split.fragment])
+        return url
+
     def get_url(self, url, params=None, headers=None):
         requestKwargs = self.get_request_dict(url, params, None, headers)
+        url = self._update_url_scheme(url)
         return requests.get(url, **requestKwargs)
 
-
     def post_url(self, url, params=None, data=None, headers=None):
         requestKwargs = self.get_request_dict(url, params, data, headers)
+        url = self._update_url_scheme(url)
         return requests.post(url, **requestKwargs)
 
     def post_xml_and_confirm_status(self, url, params=None, data=None, valid=None):