averbis / averbis-python-api

Conveniently access the REST API of Averbis products using Python
Apache License 2.0
12 stars 4 forks source link

Client.ensure_available() should actually check if a client connection is possible (and authentication) #126

Open tkoller-averbis opened 2 years ago

tkoller-averbis commented 2 years ago

Is your feature request related to a problem? Please describe.

As the first step of my client logic, I typically establish a client connection with the server, which I would like to verify immediately via client.ensure_available(). Unfortunately, ensure_available only sends a get request to the URL without checking

  1. If the URL actually corresponds to a Health Discovery service
  2. If the provided API Token is valid at all (irrespective of user priviliges)

E.g.:

HD_URL = "https://www.google.com/"
API_TOKEN =  "Definitely not a valid API Token"
client = Client(HD_URL, api_token=API_TOKEN)
client.ensure_available()

does not throw an error and hence does not really provide utility in verifying that the client is available.

Describe the solution you'd like

Client.ensure_available should send a get request to URL + "/rest/v1/buildInfo" and ideally throw an error when either

  1. The server is unavailable
  2. The server is not a health discovery server
  3. The API Token is invalid

With a corresponding informative error message!

reckart commented 2 years ago

ensure_available() is used to wait for the server to respond at all - this happens even before obtaining an API token in some cases.

tkoller-averbis commented 2 years ago

which imho does not ensure the availability of the client as suggest by the name. The reason that I came across this is that I used the url http://localhost:8080/health-discovery/#/ (which is the url that is actually copied to the clipboard in the browser) instead of http://localhost:8080/health-discovery/ which lead to the semi-informative error

The server successfully returned a response, however, this response could not be converted to Json.: line 1 column 1 (char 0)

on the last line of

HD_URL = "https://health-discovery.averbis.com/health-discovery/#/"
API_TOKEN =  "Definitely not a valid API Token"
client = Client(HD_URL, api_token=API_TOKEN)
client.ensure_available()
project = client.get_project("MY_PROJECT_NAME")
project.get_pipeline("MY Pipeline").ensure_started()

Judging from the documentation, the Client constructor expects authentication credentials and a Client object without proper authentication is imho worthless.

reckart commented 2 years ago

Actually, you can use the client with a username/password instead of an API token - and if you do that, it obtains an API token. So you don't need to have an API token.

reckart commented 2 years ago

Of course, it is not necessarily a good idea to use username/pw because it re-generates an API token every time you do it so if you have two clients connecting concurrently to the same instance, you'd get into trouble - but it is very useful during automatic testing.