amenezes / config-client

config-client package for spring cloud config and cloud foundry
https://config-client.amenezes.net/
Apache License 2.0
24 stars 18 forks source link

Add get plaintext file #30

Closed luiscoms closed 4 years ago

luiscoms commented 4 years ago

As described on official documentation here, it is possible to get plaintext files from config server.

This pull request adds a method to allow this.

Ie.:

# will call {address}/{app_name}/{profile}/{branch}/nginx.conf
content = client.get_plaintext_file("nginx.conf")
amenezes commented 4 years ago

Hello @luiscoms,

Could you share an example with me? I tried using the ConfigServer present in the docker-compose.yml file by sharing this files but the request did not work.

luiscoms commented 4 years ago

Hi @amenezes,

The above example should be work once file nginx.conf is present on config server.

import requests
requests.get("http://localhost:8888/app_name/profile/master/nginx.conf")
<Response [200]>
luiscoms commented 4 years ago

Additionally, I can request the application raw file with the following request:

import requests
requests.get("http://localhost:8888/app_name/profile/master/simpleweb000-development.yml")
<Response [200]>
amenezes commented 4 years ago

Ok @luiscoms now worked.

Some adjustments:

def get_config(self, **kwargs) -> None:
    response = self._request(self.url, **kwargs)
    self._config = response.json()

def get_file(self, name: str, **kwargs) -> str:
    uri = f"{self.address}/{self.app_name}/{self.profile}/{self.branch}/{name}"
    response = self._request(uri, **kwargs)
    return response.text

def _request(self, uri, **kwargs) -> requests.Response:
    try:
        response = requests.get(uri, **kwargs)
        response.raise_for_status()
    except requests.exceptions.HTTPError:
        raise RequestFailedException(
            f"Failed to request URI(path={uri}, code={response.status_code}"
        )
    except Exception:
        logging.error("Failed to establish connection with ConfigServer.")
        if self.fail_fast:
            logging.info("fail_fast enabled. Terminating process.")
            raise SystemExit(1)
        raise ConnectionError("fail_fast disabled.")
    return response
luiscoms commented 4 years ago

Sure, I have a question about exception handing. If request fail and fall in HTTPError like

except requests.exceptions.HTTPError:

The flag fail_fast was not considered

amenezes commented 4 years ago

You're right @luiscoms it's better change this flow.

Maybe removing except requests.exceptions.HTTPError: and change the messages from except Exception block. For something like:

def _request(self, uri, **kwargs) -> requests.Response:
    try:
        response = requests.get(uri, **kwargs)
        response.raise_for_status()
    #except requests.exceptions.HTTPError: # remove this block
    #    raise RequestFailedException(
    #        f"Failed to request URI(path={uri}, code={response.status_code}"
    #   )
    except Exception as ex:
        logging.error(f"Failed to request: {uri}")
        logging.error(ex)
        if self.fail_fast:
            logging.info("fail_fast enabled. Terminating process.")
            raise SystemExit(1)
        raise ConnectionError("fail_fast disabled.")
    return response
luiscoms commented 4 years ago

Hi @amenezes I moved the exception handing to get_config in order to consider fail fast only on get config