def jenkins_request_with_headers(self, jenkins_server, req, add_crumb=True): """ We need to get the headers in addition to the body answer to get the location from them This function is just a copy of the one present in python-jenkins library with just the return call changed :param jenkins_server: The server to query :param req: The request to execute :param add_crumb: Boolean to indicate if it should add crumb to the request :return: """ try: # if jenkins_server.auth: # req.add_header('Authorization', jenkins_server.auth) # if add_crumb: # jenkins_server.maybe_add_crumb(req) base64string = base64.b64encode( '%s:%s' % (self.connection.login, self.connection.password)) self.log.info("base64string -> %s", base64string) req.add_header("Authorization", "Basic %s" % base64string) response = urlopen(req, timeout=jenkins_server.timeout) response_body = response.read() response_headers = response.info() if response_body is None: raise jenkins.EmptyResponseException( "Error communicating with server[%s]: " "empty response" % jenkins_server.server) return { 'body': response_body.decode('utf-8'), 'headers': response_headers } except HTTPError as e: # Jenkins's funky authentication means its nigh impossible to # distinguish errors. if e.code in [401, 403, 500]: # six.moves.urllib.error.HTTPError provides a 'reason' # attribute for all python version except for ver 2.6 # Falling back to HTTPError.msg since it contains the # same info as reason raise JenkinsException( 'Error in request. ' + 'Possibly authentication failed [%s]: %s' % (e.code, e.msg)) elif e.code == 404: raise jenkins.NotFoundException( 'Requested item could not be found') else: raise except socket.timeout as e: raise jenkins.TimeoutException('Error in request: %s' % e) except URLError as e: # python 2.6 compatibility to ensure same exception raised # since URLError wraps a socket timeout on python 2.6. if str(e.reason) == "timed out": raise jenkins.TimeoutException('Error in request: %s' % e.reason) raise JenkinsException('Error in request: %s' % e.reason)
def jenkins_request_with_headers(jenkins_server, req): """ We need to get the headers in addition to the body answer to get the location from them This function uses jenkins_request method from python-jenkins library with just the return call changed :param jenkins_server: The server to query :param req: The request to execute :return: Dict containing the response body (key body) and the headers coming along (headers) """ try: response = jenkins_server.jenkins_request(req) response_body = response.content response_headers = response.headers if response_body is None: raise jenkins.EmptyResponseException( "Error communicating with server[%s]: " "empty response" % jenkins_server.server) return { 'body': response_body.decode('utf-8'), 'headers': response_headers } except HTTPError as e: # Jenkins's funky authentication means its nigh impossible to # distinguish errors. if e.code in [401, 403, 500]: # six.moves.urllib.error.HTTPError provides a 'reason' # attribute for all python version except for ver 2.6 # Falling back to HTTPError.msg since it contains the # same info as reason raise JenkinsException('Error in request. ' + 'Possibly authentication failed [%s]: %s' % (e.code, e.msg)) elif e.code == 404: raise jenkins.NotFoundException( 'Requested item could not be found') else: raise except socket.timeout as e: raise jenkins.TimeoutException('Error in request: %s' % e) except URLError as e: # python 2.6 compatibility to ensure same exception raised # since URLError wraps a socket timeout on python 2.6. if str(e.reason) == "timed out": raise jenkins.TimeoutException('Error in request: %s' % e.reason) raise JenkinsException('Error in request: %s' % e.reason)
def jenkins_request_with_headers(jenkins_server, req): """ We need to get the headers in addition to the body answer to get the location from them This function uses jenkins_request method from python-jenkins library with just the return call changed :param jenkins_server: The server to query :param req: The request to execute :return: Dict containing the response body (key body) and the headers coming along (headers) """ try: response = jenkins_server.jenkins_request(req) response_body = response.content response_headers = response.headers if response_body is None: raise jenkins.EmptyResponseException( "Error communicating with server[%s]: " "empty response" % jenkins_server.server) return {'body': response_body.decode('utf-8'), 'headers': response_headers} except HTTPError as e: # Jenkins's funky authentication means its nigh impossible to distinguish errors. if e.code in [401, 403, 500]: raise JenkinsException( 'Error in request. Possibly authentication failed [%s]: %s' % (e.code, e.msg) ) elif e.code == 404: raise jenkins.NotFoundException('Requested item could not be found') else: raise except socket.timeout as e: raise jenkins.TimeoutException('Error in request: %s' % e) except URLError as e: raise JenkinsException('Error in request: %s' % e.reason)
def jenkins_open(self, req, add_crumb=True): '''Utility routine for opening an HTTP request to a Jenkins server. Extended over the original method to allow connections to SF with a cookie. ''' self.opener.addheaders = [('Cookie', 'auth_pubtkt=%s' % self.cookie)] try: if add_crumb: self.maybe_add_crumb(req) response = self.opener.open(req, timeout=self.timeout).read() if response is None: raise jenkins.EmptyResponseException( "Error communicating with server[%s]: " "empty response" % self.server) return response.decode('utf-8') except HTTPError as e: # Jenkins's funky authentication means its nigh impossible to # distinguish errors. if e.code in [401, 403, 500]: # six.moves.urllib.error.HTTPError provides a 'reason' # attribute for all python version except for ver 2.6 # Falling back to HTTPError.msg since it contains the # same info as reason raise jenkins.JenkinsException( 'Error in request. ' + 'Possibly authentication failed [%s]: %s' % (e.code, e.msg)) elif e.code == 404: raise jenkins.NotFoundException( 'Requested item could not be found') else: raise except socket.timeout as e: raise jenkins.TimeoutException('Error in request: %s' % (e)) except URLError as e: # python 2.6 compatibility to ensure same exception raised # since URLError wraps a socket timeout on python 2.6. if str(e.reason) == "timed out": raise jenkins.TimeoutException('Error in request: %s' % (e.reason)) raise jenkins.JenkinsException('Error in request: %s' % (e.reason))