Created
December 6, 2021 14:20
-
-
Save infamousjoeg/3efe1297c60487cea9a023af758cd722 to your computer and use it in GitHub Desktop.
Python CCP REST Call Example - No 3rd Party Modules - Thanks @JimmyJamCABD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""""Example classes to use CCP in python""" | |
import ssl | |
import http | |
class CertificateHandler: | |
"""This class handles the certificates for you. | |
In python its pretty easy but here it is""" | |
def __init__(self,cafile=None): | |
self.root = None | |
self.context = ssl.create_default_context() | |
if cafile: | |
self.context = ssl.create_default_context(cafile=cafile) | |
def set_cert_and_key(self, path_to_cert: str, path_to_key: str): | |
"""This sets the cert and key file for he request | |
:param str path_to_cert: path to the cert | |
:param str path_to_key: path to the key | |
""" | |
self.context.load_cert_chain(certfile=path_to_cert,keyfile=path_to_key) | |
class CentralCredentialProvider: | |
"""This class handles the CCP requests. | |
you can set the port, host, ssl context""" | |
def __init__(self, host: str, app_id: str, handler: CertificateHandler = None, port = None): | |
self.host = host | |
self.context = handler.context | |
self.port = port | |
self.url = "/AIMWebService/api/Accounts?AppID={0}".format(app_id) | |
def set_port(self, port): | |
"""Sets port""" | |
self.port = port | |
def get_object_string(self, safe: str, query_params: dict): | |
"""Queries the CCP with valueas provided | |
:param str safe: safe where object is located | |
:param dict query_params: dict containing key value pairs of the parameters for the query | |
:returns str response: str response from request | |
""" | |
query_string = "{0}&Safe={1}".format(self.url, safe) | |
for k in query_params: | |
query_string +="&{key}={value}".format(key=k, value=query_params[k]) | |
if self.port is None: | |
connection = http.client.HTTPSConnection(self.host, context=self.context) | |
else: | |
connection = http.client.HTTPSConnection(self.host,port=self.port, context=self.context) | |
connection.request(method="GET", url=query_string, headers={}) | |
response = connection.getresponse().read().decode("utf-8") | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment