diff options
| author | Ade Lee <alee@redhat.com> | 2017-04-03 12:43:05 -0400 |
|---|---|---|
| committer | Ade Lee <alee@redhat.com> | 2017-04-03 13:12:37 -0400 |
| commit | 5dfd6e1c3cc38b5fbfdc4e96476934219f53e13f (patch) | |
| tree | 721fbefdbf09c4e98dea52d59a517e0e37fe8e54 /base/common/python/pki/client.py | |
| parent | 2d77ca150ee17238f4b137e3987a69e888141d51 (diff) | |
| download | pki-5dfd6e1c3cc38b5fbfdc4e96476934219f53e13f.tar.gz pki-5dfd6e1c3cc38b5fbfdc4e96476934219f53e13f.tar.xz pki-5dfd6e1c3cc38b5fbfdc4e96476934219f53e13f.zip | |
Added python info client
Add python client code to read from the InfoResource class and get
the server version. As the PKIConnection in the python client
currently requires a subsystem, it is difficult to add an infoclient
to an existing KRAClient (or any other client).
To get around this, I modified the PKIConnection to allow using the
rootURI.
Change-Id: Ided75f45f741e2ba3fc86acec715d24b829c8a97
Diffstat (limited to 'base/common/python/pki/client.py')
| -rw-r--r-- | base/common/python/pki/client.py | 51 |
1 files changed, 40 insertions, 11 deletions
diff --git a/base/common/python/pki/client.py b/base/common/python/pki/client.py index 90ca4fefb..805d0fadb 100644 --- a/base/common/python/pki/client.py +++ b/base/common/python/pki/client.py @@ -78,9 +78,8 @@ class PKIConnection: self.port = port self.subsystem = subsystem - self.serverURI = self.protocol + '://' + \ - self.hostname + ':' + self.port + '/' + \ - self.subsystem + self.rootURI = self.protocol + '://' + self.hostname + ':' + self.port + self.serverURI = self.rootURI + '/' + self.subsystem self.session = requests.Session() self.session.trust_env = trust_env @@ -125,7 +124,8 @@ class PKIConnection: self.session.cert = pem_cert_path @catch_insecure_warning - def get(self, path, headers=None, params=None, payload=None): + def get(self, path, headers=None, params=None, payload=None, + use_root_uri=False): """ Uses python-requests to issue a GET request to the server. @@ -137,12 +137,19 @@ class PKIConnection: :type params: dict or bytes :param payload: data to be sent in the body of the request :type payload: dict, bytes, file-like object + :param use_root_uri: use root URI instead of subsystem URI as base + :type use_root_uri: boolean :returns: request.response -- response from the server :raises: Exception from python-requests in case the GET was not successful, or returns an error code. """ + if use_root_uri: + target_path = self.rootURI + path + else: + target_path = self.serverURI + path + r = self.session.get( - self.serverURI + path, + target_path, verify=False, headers=headers, params=params, @@ -151,7 +158,8 @@ class PKIConnection: return r @catch_insecure_warning - def post(self, path, payload, headers=None, params=None): + def post(self, path, payload, headers=None, params=None, + use_root_uri=False): """ Uses python-requests to issue a POST request to the server. @@ -163,12 +171,19 @@ class PKIConnection: :type headers: dict :param params: Query parameters for the POST request :type params: dict or bytes + :param use_root_uri: use root URI instead of subsystem URI as base + :type use_root_uri: boolean :returns: request.response -- response from the server :raises: Exception from python-requests in case the POST was not successful, or returns an error code. """ + if use_root_uri: + target_path = self.rootURI + path + else: + target_path = self.serverURI + path + r = self.session.post( - self.serverURI + path, + target_path, verify=False, data=payload, headers=headers, @@ -177,7 +192,7 @@ class PKIConnection: return r @catch_insecure_warning - def put(self, path, payload, headers=None): + def put(self, path, payload, headers=None, use_root_uri=False): """ Uses python-requests to issue a PUT request to the server. @@ -187,16 +202,23 @@ class PKIConnection: :type payload: dict, bytes, file-like object :param headers: headers for the PUT request :type headers: dict + :param use_root_uri: use root URI instead of subsystem URI as base + :type use_root_uri: boolean :returns: request.response -- response from the server :raises: Exception from python-requests in case the PUT was not successful, or returns an error code. """ - r = self.session.put(self.serverURI + path, payload, headers=headers) + if use_root_uri: + target_path = self.rootURI + path + else: + target_path = self.serverURI + path + + r = self.session.put(target_path, payload, headers=headers) r.raise_for_status() return r @catch_insecure_warning - def delete(self, path, headers=None): + def delete(self, path, headers=None, use_root_uri=False): """ Uses python-requests to issue a DEL request to the server. @@ -204,11 +226,18 @@ class PKIConnection: :type path: str :param headers: headers for the DEL request :type headers: dict + :param use_root_uri: use root URI instead of subsystem URI as base + :type use_root_uri: boolean :returns: request.response -- response from the server :raises: Exception from python-requests in case the DEL was not successful, or returns an error code. """ - r = self.session.delete(self.serverURI + path, headers=headers) + if use_root_uri: + target_path = self.rootURI + path + else: + target_path = self.serverURI + path + + r = self.session.delete(target_path, headers=headers) r.raise_for_status() return r |
