Add support for htdigest auth when using CORE webservices API

This commit is contained in:
Lance Edgar 2023-05-22 21:34:46 -05:00
parent 757fb50a96
commit d58426c073

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# pyCOREPOS -- Python Interface to CORE POS # pyCOREPOS -- Python Interface to CORE POS
# Copyright © 2018-2021 Lance Edgar # Copyright © 2018-2023 Lance Edgar
# #
# This file is part of pyCOREPOS. # This file is part of pyCOREPOS.
# #
@ -28,6 +28,7 @@ import json
import logging import logging
import requests import requests
from requests.auth import HTTPDigestAuth
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -48,27 +49,42 @@ class CoreAPIError(Exception):
class CoreWebAPI(object): class CoreWebAPI(object):
""" """
Client implementation for the CORE webservices API. Client implementation for the CORE webservices API.
"""
def __init__(self, url, verify=True):
"""
Constructor for the API client.
:param str url: URL to the CORE webservices API, :param str url: URL to the CORE webservices API,
e.g. ``'http://localhost/fannie/ws/'`` e.g. ``'http://localhost/fannie/ws/'``
:param bool verify: How to handle certificate validation for HTTPS :param bool verify: How to handle certificate validation for HTTPS
URLs. This value is passed as-is to the ``requests`` library, so URLs. This value is passed as-is to the ``requests`` library,
see those docs for more info. The default value for this is so see those docs for more info. The default value for this is
``True`` because the assumption is that security should be on by ``True`` because the assumption is that security should be on
default. Set it to ``False`` in order to disable validation by default. Set it to ``False`` in order to disable validation
entirely, e.g. for self-signed certs. (This may also be needed for entirely, e.g. for self-signed certs. (This may also be needed
basic HTTP URLs?) Other values may be possible also; again see the for basic HTTP URLs?) Other values may be possible also; again
``requests`` docs for more info. see the ``requests`` docs for more info.
:param htdigest_username: Username for htdigest authentication, if
applicable.
:param htdigest_password: Password for htdigest authentication, if
applicable.
""" """
def __init__(
self,
url,
verify=True,
htdigest_username=None,
htdigest_password=None,
):
self.url = url self.url = url
self.verify = verify self.verify = verify
self.session = requests.Session()
if htdigest_username and htdigest_password:
self.session.auth = HTTPDigestAuth(htdigest_username,
htdigest_password)
def post(self, params, method=None): def post(self, params, method=None):
""" """
Issue a POST request to the API, with the given ``params``. If not Issue a POST request to the API, with the given ``params``. If not
@ -88,7 +104,7 @@ class CoreWebAPI(object):
'id': 1, 'id': 1,
} }
response = requests.post(self.url, data=json.dumps(payload), response = self.session.post(self.url, data=json.dumps(payload),
verify=self.verify) verify=self.verify)
response.raise_for_status() response.raise_for_status()
return response return response