0

I have a piece of code that will call the URL(JS) and return the result.

I want the HTTP call should be secured using https, however the destination URL certificate CA is not existing by default with python request module.

so I intended to use certifi module , written a function that will check the certificate issuer of the URL against certifi pem file if it doesn't exist it will add the downloaded CA certificate to the certifi module pem file.

Above said function will be called before actual request call, so in case of certificate missing the function will add the cert and call the same parent function to make the successful request call, so that the result is registered.

Here the sudo code,

class GnibUrlGenerator:
  def __init__(self):
     (......)
  def validate_params(self):
     (......)
  def check_cert_issuer_exists(self, certcryptoobj):
     (.....)
  def add_interca_cert(self):
     (.....)
  def getavailable_appointment(self):
    try:
        self.validate_params()
        # print (self.CabundleObj)
        self.check_cert_issuer_exists(self.CertCryptoObj)
        return requests.get(self.url + '(getApps4DT)?', params=(
                            self.page, ('dt', self.date), ('cat', self.cat), ('sbcat', self.sbcat),
                            ('typ', self.type)), headers=self.headers, verify=True)
    except (CategoryValueError, TypeValueError) as err:
        print(err)
    except RequestException as err:
        print(err, 'So adding IntermediateCA chain to CA store.')
        self.add_interca_cert()
        self.getavailable_appointment()
        #print('call')



   url = GnibUrlGenerator  
   a = url.getavailable_appointment()
   print(a.url, '\n', a.headers, '\n', a.text, '\n', a.status_code)

err:
AttributeError: 'NoneType' object has no attribute 'url'

The issue that encountered here, when I do run the script first time as expected it will find the certificate missing and caught with an exception as ' RequestException' and run the statements in except block.

except RequestException as err:
            print(err, 'So adding IntermediateCA chain to CA store.')
            self.add_interca_cert()
            self.getavailable_appointment()
            #print('call')

so it means to add the missing certificate and doing the same function call again.

Though the first class function call is a success, it failed to get the successful request object in the instance created out of it.

But always the next run returns the results.

How to rewrite this so that I can get rid of this problem.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

0 Answers0