-
-
Notifications
You must be signed in to change notification settings - Fork 0
Enhancing user-facing API #21
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
Open
AHReccese
wants to merge
12
commits into
dev
Choose a base branch
from
refactoring
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4e3775e
expose factory classes
AHReccese c8abd02
add user facing enums
AHReccese 9978491
add deprecation warning to direct use of global adapters
AHReccese 0e0f5d8
add user facing API
AHReccese f63a3a8
add tests for user-interfacing API
AHReccese 11d4a47
`CHANGELOG.md` updated
AHReccese 1e42f8b
`README.md` updated
AHReccese 61131d2
add pytest config file
AHReccese 5966e22
`CHANGELOG.md` updated
AHReccese 856c0b8
`README.md` updated
AHReccese 0349c56
update docstrings
AHReccese 408072f
docstring updated
AHReccese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ipforce modules.""" | ||
| from .params import IPFORCE_VERSION | ||
| from .enums import IPVersion, IPForceMethod | ||
| from .api import IPForceAdapter, IPForceSession | ||
| from .adapters import IPv4TransportAdapter, IPv6TransportAdapter | ||
| from .adapters import IPv4LockAdapter, IPv6LockAdapter | ||
|
|
||
| __version__ = IPFORCE_VERSION | ||
|
|
||
| __all__ = [ | ||
| "IPVersion", "IPForceMethod", | ||
| "IPForceAdapter", "IPForceSession", | ||
| "IPv4TransportAdapter", "IPv6TransportAdapter", | ||
| "IPv4LockAdapter", "IPv6LockAdapter", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Unified public API for IPForce adapter and session creation.""" | ||
| import warnings | ||
|
|
||
| from requests import Session | ||
| from requests.adapters import HTTPAdapter | ||
|
|
||
| from .enums import IPVersion, IPForceMethod | ||
| from .adapters import ( | ||
| IPv4TransportAdapter, IPv6TransportAdapter, | ||
| IPv4LockAdapter, IPv6LockAdapter, | ||
| ) | ||
|
|
||
| _ADAPTER_REGISTRY = { | ||
| (IPVersion.V4, IPForceMethod.GLOBAL): IPv4TransportAdapter, | ||
| (IPVersion.V6, IPForceMethod.GLOBAL): IPv6TransportAdapter, | ||
| (IPVersion.V4, IPForceMethod.LOCK): IPv4LockAdapter, | ||
| (IPVersion.V6, IPForceMethod.LOCK): IPv6LockAdapter, | ||
| } | ||
|
|
||
|
|
||
| def IPForceAdapter( | ||
| ip_version: IPVersion, | ||
| method: IPForceMethod = IPForceMethod.LOCK, | ||
| ) -> HTTPAdapter: | ||
| """ | ||
| Create an HTTP adapter that forces a specific IP version. | ||
|
|
||
| :param ip_version: IPVersion.V4 or IPVersion.V6 | ||
| :param method: thread-safety strategy (default: LOCK) | ||
| :return: configured HTTPAdapter instance | ||
| :raises ValueError: if the (ip_version, method) combination is not registered | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this line |
||
| """ | ||
| adapter_cls = _ADAPTER_REGISTRY.get((ip_version, method)) | ||
| if adapter_cls is None: | ||
| raise ValueError("Unsupported combination: {v} + {m}".format(v=ip_version, m=method)) | ||
| with warnings.catch_warnings(): | ||
| warnings.simplefilter("ignore", DeprecationWarning) | ||
| return adapter_cls() | ||
|
|
||
|
|
||
| class IPForceSession(Session): | ||
| """A requests.Session pre-configured to force a specific IP version.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| ip_version: IPVersion, | ||
| method: IPForceMethod = IPForceMethod.LOCK, | ||
| ) -> None: | ||
| """ | ||
| Initialize the session with an IP-version-forced adapter. | ||
|
|
||
| :param ip_version: IPVersion.V4 or IPVersion.V6 | ||
| :param method: thread-safety strategy (default: LOCK) | ||
| """ | ||
| super().__init__() | ||
| adapter = IPForceAdapter(ip_version, method) | ||
| self.mount('http://', adapter) | ||
| self.mount('https://', adapter) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this file and move all of these enums to |
||
| """IPForce enumerations for IP version and resolution method selection.""" | ||
| from enum import Enum | ||
|
|
||
|
|
||
| class IPVersion(Enum): | ||
| """IP protocol version to enforce for DNS resolution.""" | ||
|
|
||
| V4 = "ipv4" | ||
| V6 = "ipv6" | ||
|
|
||
|
|
||
| class IPForceMethod(Enum): | ||
| """Thread-safety strategy for address family enforcement.""" | ||
|
|
||
| GLOBAL = "global" | ||
| LOCK = "lock" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [pytest] | ||
| filterwarnings = | ||
| ignore:.*is deprecated, use IP.*:DeprecationWarning |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| """Tests for the unified IPForceAdapter / IPForceSession API.""" | ||
| import socket | ||
| import warnings | ||
| import unittest | ||
|
|
||
| from requests.adapters import HTTPAdapter | ||
|
|
||
| from ipforce import ( | ||
| IPVersion, IPForceMethod, | ||
| IPForceAdapter, IPForceSession, | ||
| IPv4TransportAdapter, IPv6TransportAdapter, | ||
| ) | ||
| from ipforce.adapters import _BaseLockAdapter | ||
|
|
||
|
|
||
| class TestIPForceAdapterFactory(unittest.TestCase): | ||
| """Test that IPForceAdapter returns correct adapter types.""" | ||
|
|
||
| def test_v4_lock(self): | ||
| adapter = IPForceAdapter(IPVersion.V4, IPForceMethod.LOCK) | ||
| self.assertIsInstance(adapter, _BaseLockAdapter) | ||
| self.assertEqual(adapter._family, socket.AF_INET) | ||
|
|
||
| def test_v6_lock(self): | ||
| adapter = IPForceAdapter(IPVersion.V6, IPForceMethod.LOCK) | ||
| self.assertIsInstance(adapter, _BaseLockAdapter) | ||
| self.assertEqual(adapter._family, socket.AF_INET6) | ||
|
|
||
| def test_v4_global(self): | ||
| adapter = IPForceAdapter(IPVersion.V4, IPForceMethod.GLOBAL) | ||
| self.assertIsInstance(adapter, HTTPAdapter) | ||
|
|
||
| def test_v6_global(self): | ||
| adapter = IPForceAdapter(IPVersion.V6, IPForceMethod.GLOBAL) | ||
| self.assertIsInstance(adapter, HTTPAdapter) | ||
|
|
||
| def test_default_method_is_lock(self): | ||
| adapter = IPForceAdapter(IPVersion.V4) | ||
| self.assertIsInstance(adapter, _BaseLockAdapter) | ||
|
|
||
| def test_invalid_combination_raises(self): | ||
| with self.assertRaises((ValueError, KeyError)): | ||
| IPForceAdapter(IPVersion.V4, "not_a_method") | ||
|
|
||
|
|
||
| class TestIPForceSession(unittest.TestCase): | ||
| """Test IPForceSession class.""" | ||
|
|
||
| def test_v4_session_mounts_lock_adapter(self): | ||
| with IPForceSession(IPVersion.V4) as session: | ||
| adapter = session.get_adapter('https://example.com') | ||
| self.assertIsInstance(adapter, _BaseLockAdapter) | ||
|
|
||
| def test_v6_session_mounts_lock_adapter(self): | ||
| with IPForceSession(IPVersion.V6) as session: | ||
| adapter = session.get_adapter('https://example.com') | ||
| self.assertIsInstance(adapter, _BaseLockAdapter) | ||
| self.assertEqual(adapter._family, socket.AF_INET6) | ||
|
|
||
| def test_session_with_global_method(self): | ||
| with IPForceSession(IPVersion.V4, method=IPForceMethod.GLOBAL) as session: | ||
| adapter = session.get_adapter('https://example.com') | ||
| self.assertIsInstance(adapter, HTTPAdapter) | ||
|
|
||
| def test_session_context_manager(self): | ||
| with IPForceSession(IPVersion.V4) as session: | ||
| self.assertIsInstance(session, IPForceSession) | ||
|
|
||
|
|
||
| class TestDeprecationWarnings(unittest.TestCase): | ||
| """Old v0.1 classes emit DeprecationWarning; new API does not.""" | ||
|
|
||
| def test_ipv4_transport_adapter_warns(self): | ||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| IPv4TransportAdapter() | ||
| self.assertEqual(len(w), 1) | ||
| self.assertTrue(issubclass(w[0].category, DeprecationWarning)) | ||
| self.assertIn("IPForceAdapter", str(w[0].message)) | ||
|
|
||
| def test_ipv6_transport_adapter_warns(self): | ||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| IPv6TransportAdapter() | ||
| self.assertEqual(len(w), 1) | ||
| self.assertTrue(issubclass(w[0].category, DeprecationWarning)) | ||
|
|
||
| def test_new_api_does_not_warn(self): | ||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| IPForceAdapter(IPVersion.V4, IPForceMethod.LOCK) | ||
| IPForceAdapter(IPVersion.V4, IPForceMethod.GLOBAL) | ||
| session = IPForceSession(IPVersion.V4) | ||
| session.close() | ||
| dep_warnings = [x for x in w if issubclass(x.category, DeprecationWarning)] | ||
| self.assertEqual(len(dep_warnings), 0) | ||
|
|
||
|
|
||
| class TestEnums(unittest.TestCase): | ||
| """Test enum values.""" | ||
|
|
||
| def test_ip_version_values(self): | ||
| self.assertEqual(IPVersion.V4.value, "ipv4") | ||
| self.assertEqual(IPVersion.V6.value, "ipv6") | ||
|
|
||
| def test_method_values(self): | ||
| self.assertEqual(IPForceMethod.GLOBAL.value, "global") | ||
| self.assertEqual(IPForceMethod.LOCK.value, "lock") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this section