userspace:json-rpc_python_requests
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| userspace:json-rpc_python_requests [2018/03/16 17:55] – created schuhj | userspace:json-rpc_python_requests [2021/08/23 10:37] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| **More error handling and concern for security is needed before use in production environments! ** | **More error handling and concern for security is needed before use in production environments! ** | ||
| - | ===== The Module ===== | + | ===== The Module |
| <code python> | <code python> | ||
| Line 16: | Line 16: | ||
| import base64 | import base64 | ||
| import os.path | import os.path | ||
| - | # ignore warnings about missing ssl cert field | + | |
| + | # ignore warnings about missing ssl cert field subjectAltName (you may remove this and ln 102) | ||
| from requests.packages.urllib3.exceptions import SubjectAltNameWarning | from requests.packages.urllib3.exceptions import SubjectAltNameWarning | ||
| Line 23: | Line 24: | ||
| class OpsiError(Exception): | class OpsiError(Exception): | ||
| - | """ | ||
| - | |||
| - | Attributes: | ||
| - | expression -- the RPC Call | ||
| - | class -- the Error class | ||
| - | message -- the Error message | ||
| - | """ | ||
| def __init__(self, | def __init__(self, | ||
| + | """ | ||
| + | |||
| + | :param expression: the RPC Call | ||
| + | :param opsierrorclass: | ||
| + | :param message: the Error message | ||
| + | """ | ||
| self.expression = expression | self.expression = expression | ||
| self.opsierrorclass = opsierrorclass | self.opsierrorclass = opsierrorclass | ||
| Line 38: | Line 38: | ||
| class OpsiConnection: | class OpsiConnection: | ||
| + | |||
| def __init__(self, | def __init__(self, | ||
| # 0 is not a valid id | # 0 is not a valid id | ||
| + | """ | ||
| + | |||
| + | :param url: Opsiserver URL | ||
| + | :param authfile: base64 encoded UTF-8 String containing username password | ||
| + | :param auth: username password as tuple | ||
| + | :param certfile: Opsiserver SSL Certificate | ||
| + | :param legal_methods_path: | ||
| + | """ | ||
| self.id = 0 | self.id = 0 | ||
| self.server = url | self.server = url | ||
| Line 49: | Line 58: | ||
| elif not auth: | elif not auth: | ||
| # get auth from file | # get auth from file | ||
| - | # authfile expected to contain base64 encoded UTF-8 String (username password) | ||
| # The authfile has to be kept in an secure environment! | # The authfile has to be kept in an secure environment! | ||
| with open(authfile, | with open(authfile, | ||
| Line 64: | Line 72: | ||
| raise FileNotFoundError(legal_methods_path) | raise FileNotFoundError(legal_methods_path) | ||
| else: | else: | ||
| - | # getPossibleMethods_listOfHashes | + | # getPossibleMethods_listOfHashes |
| opsicon_logger.debug(" | opsicon_logger.debug(" | ||
| self.id += 1 | self.id += 1 | ||
| Line 79: | Line 87: | ||
| def raw_request(self, | def raw_request(self, | ||
| + | """ | ||
| + | |||
| + | :param payload: json string to send to the server | ||
| + | """ | ||
| self.__rpc_request(self.session, | self.__rpc_request(self.session, | ||
| Line 99: | Line 111: | ||
| # avoid proxy issues | # avoid proxy issues | ||
| session.trust_env = False | session.trust_env = False | ||
| - | # ignore warnings about missing ssl cert field | + | # ignore warnings about missing ssl cert field subjectAltName |
| requests.packages.urllib3.disable_warnings(SubjectAltNameWarning) | requests.packages.urllib3.disable_warnings(SubjectAltNameWarning) | ||
| # supply cert file | # supply cert file | ||
| Line 113: | Line 125: | ||
| r.raise_for_status() | r.raise_for_status() | ||
| except requests.exceptions.Timeout as e: | except requests.exceptions.Timeout as e: | ||
| - | # TODO: Maybe set up retry loop | ||
| raise e | raise e | ||
| except requests.exceptions.RequestException as e: | except requests.exceptions.RequestException as e: | ||
| - | # TODO: Maybe think about better handling | ||
| raise e | raise e | ||
| if r.json()[" | if r.json()[" | ||
| raise OpsiError(payload, | raise OpsiError(payload, | ||
| - | return r | + | return r.json()[' |
| </ | </ | ||
| ===== Example ===== | ===== Example ===== | ||
| <code python> | <code python> | ||
| + | import json | ||
| + | |||
| from opsirpc import OpsiConnection as OpsiServerCon | from opsirpc import OpsiConnection as OpsiServerCon | ||
| - | from opsirpc import OpsiError | + | |
| def print_name_example(response): | def print_name_example(response): | ||
| - | for o in response.json()[' | + | for o in response: |
| print(o[' | print(o[' | ||
| + | |||
| server_connection = OpsiServerCon(url = ' | server_connection = OpsiServerCon(url = ' | ||
| authfile = " | authfile = " | ||
| - | certfile = " | + | certfile = " |
| - | | + | |
| - | r = server_connection.product_getObjects(licenseRequired = True) | + | r = server_connection.product_getObjects(licenseRequired=True) |
| - | print_name_example(r) | + | |
| - | r = server_connection.product_getObjects(' | + | |
| print_name_example(r) | print_name_example(r) | ||
| + | |||
| + | print(server_connection.product_getObjects(' | ||
| + | print(' | ||
| + | in server_connection.getPossibleMethods_listOfHashes()])) | ||
| </ | </ | ||
| + | |||
| + | [[https:// | ||
| + | |||
userspace/json-rpc_python_requests.1521219350.txt.gz · Last modified: (external edit)
