mercredi 30 août 2017

How to store a constant value I might want to change

I'm trying to log in to an online server.
This server keeps changing the API version, which I have to specify as one of the header parameters.
Currently I save it as a constant value and change it manually when they change it, but I think that's not a good way of doing things so I'm trying to make it happen programmatically.

This is the function that attempts to connect and catches the change of API version:

API_VERSION = 78   

def connect_to_server(self, session, url, headers, data):
    r = session.post(url, headers=headers, json=data)
    if r.status_code != 200:
        if r.status_code == 449:
            api_num_new = [int(s) for s in r.content.split() if s.isdigit()]
            print "Failed because of api version - Change API_VERSION to: {}".format(api_num_new[0])
            headers.update({"LKQD-Api-Version": api_num_new[0]})
            self.update_api_version(api_num_new[0])
            return self.connect_to_server(session, url, headers, data)

        raise IOError("Connecting failed! Status code: {}. Message: {}").format(str(r.status_code),str(r.content))
    response = r.json()
    if response[API_STATUS_KEY] != API_SUCCESS_VALUE:
        raise IOError(
            "Connecting to the server failed! Error: " + str(response[API_ERROR_KEY]))
    return session

def update_api_version(api_num_new):
    raise Exception("Not implemented")

My question is what would be the best method for dealing with this problem?
I've considered using a YAML file but was wondering if a more suitable solution exists.




Aucun commentaire:

Enregistrer un commentaire