i have two functions in python
class JENKINS_JOB_INFO():
def __init__(self):
parser = argparse.ArgumentParser(description='xxxx. e.g., script.py -j jenkins_url -u user -a api')
parser.add_argument('-j', '--address', dest='address', default="", required=True, action="store")
parser.add_argument('-u', '--user', dest='user', default="xxxx", required=True, action="store")
parser.add_argument('-t', '--api_token', dest='api_token', required=True, action="store")
parsers = parser.parse_args()
self.base_url = parsers.address.strip()
self.user = parsers.user.strip()
self.api_token = parsers.api_token.strip()
def main(self):
logger.info("call the function")
self.get_jobs_state()
def get_jobs_state(self):
get_jenkins_json_data(params)
def get_jenkins_json_data(self, params, base_url):
url = urljoin(self.base_url, str(params.params))
r = requests.get(url, auth=HTTPBasicAuth(self.user, self.api_token), verify=False)
i have a parameter params defined in my function get_jobs_stateand i want to pass this param to my other function get_jenkins_json_data so that the complete url inside function get_jenkins_json_data joins to https:<jenkins>/api/json?pretty=true&tree=jobs[name,color]
But when i run my code the url is not correct and the value of params inside the function is <__main__.CLASS_NAME instance at 0x7f9adf4c0ab8>
here base_url is a parameter that i am passing to my script.
How can i get rid of this error?
3 Answers 3
Just write params.params instead of params.
The way you do it is extremely confusing because in get_jenkins_josn_data, self will be the params and params will be the base_url. I would advise you no to do that in the future. If you want to send some parameters to the function, send the minimal amount of information that the function needs. Here, for example, you could have sent self.params instead of the whole self. This way you wouldn't encounter this error and the code would be much more readable.
I suggest you to rewrite this function this way.
Comments
In your function get_jobs_state you are passing in self as the argument for the params argument to the get_jenkins_json_data, and self in this case is an instance of a class.
Try something like this:
class Jenkins:
def __init__(self, domain):
self.user = "user"
self.api_token = "api_token"
self.base_domain = domain
def get_jobs_state(self):
query = "api/json?pretty=true&tree=jobs[name,color]"
return self.get_jenkins_json_data(query)
def get_jenkins_json_data(self, query):
url = urljoin(self.base_domain, str(query))
r = requests.get(url, auth=HTTPBasicAuth(self.user, self.api_token), verify=False)
return r
3 Comments
So your solution is a bit confusing. You shouldn't pass the self to the get_jenkins_json_data method. The python will do that for you automatically. You should check out the data model for how instance methods work. I would refactor your code like this:
def get_jobs_state(self):
params = "api/json?pretty=true&tree=jobs[name,color]"
self.get_jenkins_json_data(params)
def get_jenkins_json_data(self, params):
url = urljoin(self.base_url, params)
r = requests.get(url, auth=HTTPBasicAuth(self.user, self.api_token), verify=False)
...
get_jenkins_json_datais givenparamsyet in arguments forurljoin,self.paramsis used, over theparamsgiven fromget_jobs_state?self.paramsor justparamsinurljoin, i have to declareself.params = []indef __init__(self):. If i dont do that my script fails withERRORAttributeError: CLASSNAME instance has no attribute 'params'self.get_jenkins_json_data(self, params)on purpose? Because this way theselfwill be theparamsandparamswill be thebase_url.selfkeyword, so i dont know if callingself.get_jenkins_json_data(self, params)is not correct