Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection refused errors #1

Open
khix32 opened this issue Jul 27, 2023 · 1 comment
Open

connection refused errors #1

khix32 opened this issue Jul 27, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@khix32
Copy link

khix32 commented Jul 27, 2023

PROBLEM SUMMARY
Session class is not being used in requests session. it's wrapping the request which results in the connection pool being exhausted

Updated section that works:
`import json
import requests
from datetime import datetime
from pydantic import BaseModel
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from packaging.version import Version

class Session:
"""
This class is responsible for holding the appropriate headers to authenticate each
request. It also removes all null values from all data sent to TPP.
"""

def __init__(
    self,
    headers: dict,
    proxies: dict = None,
    certificate_path: str = None,
    key_file_path: str = None,
    verify_ssl: bool = False,
    tpp_version: "Version" = None,
    connection_timeout: float = None,
    read_timeout: float = None,
):
    # noinspection PyUnresolvedReferences
    self.requests = requests
    self.requests.packages.urllib3.disable_warnings()
    self.session = self.requests.Session()
    self.request_kwargs = {
        "headers": headers,
        "verify": verify_ssl,
        "timeout": (connection_timeout, read_timeout),
    }
    if proxies:
        self.request_kwargs["proxies"] = proxies
    if bool(certificate_path) != bool(key_file_path):
        raise ValueError(
            "The path to both the certificate file and the key file must be "
            "given for certificate authentication."
        )
    if certificate_path and key_file_path:
        self.request_kwargs["cert"] = (certificate_path, key_file_path)
    self.tpp_version = tpp_version

def update_headers(self, new_headers):
    self.request_kwargs["headers"].update(new_headers)

def delete(self, url: str, params: dict = None):
    """
    Sends a DELETE request to the given URL.

    Args:
        url: URL endpoint
        params: Dictionary of parameters to append to the URL

    Returns:
        Returns the raw response returned by the server.
    """
    return self.session.delete(
        url, params=self._sanitize(obj=params), **self.request_kwargs
    )

def get(self, url: str, params: dict = None):
    """
    Sends a GET request to the given URL with the given URL parameters.

    Args:
        url: URL endpoint
        params: Dictionary of parameters to append to the URL

    Returns:
        Returns the raw response returned by the server.
    """
    return self.session.get(
        url=url, params=self._sanitize(obj=params), **self.request_kwargs
    )

def post(self, url: str, data: dict):
    """
    Sends a POST request to the given URL with the given data.

    Args:
        url: URL endpoint
        data: Dictionary of data

    Returns:
        Returns the raw response returned by the server.
    """
    return self.session.post(
        url=url, data=self._to_json(obj=data), **self.request_kwargs
    )

def put(self, url: str, data: dict):
    """
    Sends a PUT request to the given URL with the given data.

    Args:
        url: URL endpoint
        data: Dictionary of data

    Returns:
        Returns the raw response returned by the server.
    """
    return self.session.put(
        url=url, data=self._to_json(obj=data), **self.request_kwargs
    )

def _to_json(self, obj):
    sanitized_obj = self._sanitize(obj)
    return json.dumps(sanitized_obj, default=self._json_default)

@staticmethod
def _json_default(obj):
    if isinstance(obj, datetime):
        return obj.isoformat()
    return str(obj)

@staticmethod
def _validate_value(obj):
    if isinstance(obj, datetime):
        return obj.isoformat()
    return obj

def _sanitize(self, obj):
    if isinstance(obj, BaseModel):
        obj = obj.dict(by_alias=True, exclude_none=True)
    if isinstance(obj, dict):
        new_values = {}
        for k, v in obj.items():
            if v is None:
                continue
            new_k = self._validate_value(k)
            new_v = self._sanitize(v)
            new_values[new_k] = new_v
        return new_values
    elif isinstance(obj, (list, tuple, set)):
        return [self._sanitize(item) for item in obj if item is not None]
    else:
        return self._validate_value(obj)

`

@khix32 khix32 added the bug Something isn't working label Jul 27, 2023
@HELGAHR HELGAHR self-assigned this Sep 15, 2023
@HELGAHR
Copy link
Collaborator

HELGAHR commented Sep 15, 2023

@khix32 I have made the requested change in the updateSessionHandler branch. You can test out the fix with a pip install: pip install git+https://github.com/Venafi/pyVenafi.git@updateSessionHandler. I simply swapped out self.requests = requests for self.session = requests.Session(). Please verify that this fixes your issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants