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

feature: add util to get ProxyHandler and cache some recurring functions #358

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion qgis_deployment_toolbelt/utils/proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

# Standard library
import logging
from functools import lru_cache
from os import environ
from urllib.request import getproxies
from urllib.request import OpenerDirector, ProxyHandler, build_opener, getproxies

# package
from qgis_deployment_toolbelt.utils.url_helpers import check_str_is_url
Expand All @@ -29,8 +30,25 @@
# #############################################################################
# ########## Functions #############
# ##################################
@lru_cache
def get_proxy_handler() -> OpenerDirector:
"""Return URL opener with or without proxy handler.

Returns:
OpenerDirector: request handler supporting proxy settings
"""
# Handle network proxy
if get_proxy_settings() is not None:
proxy_handler = ProxyHandler(get_proxy_settings()) # Create a proxy handler
opener = build_opener(proxy_handler) # Create an opener that will use the proxy
else:
proxy_handler = ProxyHandler() # Create a proxy handler
opener = build_opener(proxy_handler) # Create an opener that will use the proxy

return opener


@lru_cache
def get_proxy_settings() -> dict | None:
"""Retrieves network proxy settings from operating system configuration or
environment variables.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils_proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_proxy_settings(self):
self.assertIsNone(get_proxy_settings())

# using generic - only http
get_proxy_settings.cache_clear()
environ["HTTP_PROXY"] = "http://proxy.example.com:3128"
self.assertIsInstance(get_proxy_settings(), dict)
self.assertEqual(
Expand All @@ -41,6 +42,7 @@ def test_proxy_settings(self):
environ.pop("HTTP_PROXY") # clean up

# using generic - only https
get_proxy_settings.cache_clear()
environ["HTTPS_PROXY"] = "https://proxy.example.com:3128"
self.assertIsInstance(get_proxy_settings(), dict)
self.assertEqual(
Expand All @@ -51,6 +53,7 @@ def test_proxy_settings(self):
environ.pop("HTTPS_PROXY") # clean up

# using generic - both http and https
get_proxy_settings.cache_clear()
environ["HTTP_PROXY"] = "http://proxy.example.com:3128"
environ["HTTPS_PROXY"] = "https://proxy.example.com:3128"
self.assertIsInstance(get_proxy_settings(), dict)
Expand All @@ -65,6 +68,7 @@ def test_proxy_settings(self):
environ.pop("HTTPS_PROXY") # clean up

# using custom QDT
get_proxy_settings.cache_clear()
environ["QDT_PROXY_HTTP"] = "http://user:p8ùX45@proxy.example.com:3128"
self.assertIsInstance(get_proxy_settings(), dict)
self.assertEqual(
Expand Down