'How do I pass SSL Context into a Python Client that Uses the Autogenerated OpenAPI Python Client Library

I have a Python3 OpenAPI client application that gives me the following error: "unable to get local issuer certificate".

This application uses the autogenerated python client libraries provided by the OpenAPI generator, and it connects to my HTTPS Node Express Service (stubs were also generated by the OpenAPI generator).

I am using version 5.4.0 of the OpenAPI Generator

I replicated the error with the following code snippet:

from urllib import request
resp = request.urlopen("https://sdbie-sargrad.chgme.com:8081/api-docs") 
html = resp.read()

I then fixed the error with the following update to this simple code snippet:

from urllib import request
import ssl
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_REQUIRED 
context.load_verify_locations("/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem")
resp = request.urlopen("https://sdbie-sargrad.chgme.com:8081/api-docs", context=context) 
html = resp.read()

My OpenAPI code looks like the following:

from api.vlc_api_1_5_0.openapi_client.model.video import Video
from api.vlc_api_1_5_0.openapi_client.exceptions import ApiException
from api.vlc_api_1_5_0.openapi_client.exceptions import ApiTypeError
from api.vlc_api_1_5_0.openapi_client.configuration import Configuration
from api.vlc_api_1_5_0.openapi_client.api import video_api
from api.vlc_api_1_5_0.openapi_client.api_client import ApiClient

self.configuration = Configuration(host)
with ApiClient(self.configuration) as api_client:
    vid_api = video_api.VideoApi(api_client)
    try:
        inv = vid_api.get_video_inventory()
    except ApiException as e:
        self.logger.error("Exception when calling VideoApi->get_video_inventory: %s\n" % e)

As an aside the "petstore api" looks very much like the structure of the api that you see me using above.

How do I pass the appropriate SSL context (as seen in the fixed snippet above) into the constructor of the "VideoApi" that is autogenerated by the openapi generator?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source