Add support for htdigest auth when using CORE webservices API
This commit is contained in:
		
							parent
							
								
									757fb50a96
								
							
						
					
					
						commit
						d58426c073
					
				
					 1 changed files with 35 additions and 19 deletions
				
			
		| 
						 | 
					@ -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.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    :param str url: URL to the CORE webservices API,
 | 
				
			||||||
 | 
					       e.g. ``'http://localhost/fannie/ws/'``
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    :param bool verify: How to handle certificate validation for HTTPS
 | 
				
			||||||
 | 
					       URLs.  This value is passed as-is to the ``requests`` library,
 | 
				
			||||||
 | 
					       so see those docs for more info.  The default value for this is
 | 
				
			||||||
 | 
					       ``True`` because the assumption is that security should be on
 | 
				
			||||||
 | 
					       by default.  Set it to ``False`` in order to disable validation
 | 
				
			||||||
 | 
					       entirely, e.g. for self-signed certs.  (This may also be needed
 | 
				
			||||||
 | 
					       for basic HTTP URLs?)  Other values may be possible also; again
 | 
				
			||||||
 | 
					       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):
 | 
					    def __init__(
 | 
				
			||||||
        """
 | 
					            self,
 | 
				
			||||||
        Constructor for the API client.
 | 
					            url,
 | 
				
			||||||
 | 
					            verify=True,
 | 
				
			||||||
        :param str url: URL to the CORE webservices API,
 | 
					            htdigest_username=None,
 | 
				
			||||||
           e.g. ``'http://localhost/fannie/ws/'``
 | 
					            htdigest_password=None,
 | 
				
			||||||
 | 
					    ):
 | 
				
			||||||
        :param bool verify: How to handle certificate validation for HTTPS
 | 
					 | 
				
			||||||
           URLs.  This value is passed as-is to the ``requests`` library, so
 | 
					 | 
				
			||||||
           see those docs for more info.  The default value for this is
 | 
					 | 
				
			||||||
           ``True`` because the assumption is that security should be on by
 | 
					 | 
				
			||||||
           default.  Set it to ``False`` in order to disable validation
 | 
					 | 
				
			||||||
           entirely, e.g. for self-signed certs.  (This may also be needed for
 | 
					 | 
				
			||||||
           basic HTTP URLs?)  Other values may be possible also; again see the
 | 
					 | 
				
			||||||
           ``requests`` docs for more info.
 | 
					 | 
				
			||||||
        """
 | 
					 | 
				
			||||||
        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,8 +104,8 @@ 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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue