Skip to content

Commit 6c77b01

Browse files
authored
removed pkg_resources dependency and added unit tests for the fix (#64)
* removed pkg_resources dependency and added unit tests for the fix
1 parent d445a25 commit 6c77b01

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import unittest
2+
from unittest.mock import patch
3+
from importlib.metadata import version, PackageNotFoundError
4+
5+
from uid2_client.request_response_util import auth_headers
6+
7+
8+
class TestRequestResponseUtil(unittest.TestCase):
9+
10+
def test_auth_headers_packaged_mode(self):
11+
"""Test that the version is correctly retrieved in packaged mode."""
12+
try:
13+
# In a test environment, the package might not be fully installed.
14+
# We get the version directly if available.
15+
expected_version = version("uid2_client")
16+
except PackageNotFoundError:
17+
# If not found, we can't run this specific check, so we skip it.
18+
self.skipTest("uid2_client package not found, skipping packaged mode test.")
19+
20+
headers = auth_headers("test_auth_key")
21+
self.assertEqual(headers['Authorization'], 'Bearer test_auth_key')
22+
self.assertEqual(headers['X-UID2-Client-Version'], f"uid2-client-python-{expected_version}")
23+
24+
@patch('uid2_client.request_response_util.metadata.version')
25+
def test_auth_headers_non_packaged_mode(self, mock_version):
26+
"""Test that the version is set to non-packaged-mode when the package is not found."""
27+
mock_version.side_effect = PackageNotFoundError
28+
headers = auth_headers("test_auth_key")
29+
self.assertEqual(headers['Authorization'], 'Bearer test_auth_key')
30+
self.assertEqual(headers['X-UID2-Client-Version'], "uid2-client-python-non-packaged-mode")

uid2_client/request_response_util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
from datetime import datetime
44
from typing import Dict, Optional
55
from urllib import request
6-
7-
import pkg_resources
6+
from importlib import metadata
87

98
from uid2_client.encryption import _encrypt_gcm, _decrypt_gcm
109
from .envelope import Envelope
1110
from .uid2_response import Uid2Response
1211

1312
BINARY = 'application/octet-stream'
1413

14+
1515
def _make_url(base_url: str, path: str) -> str:
1616
return base_url + path
1717

18+
1819
def auth_headers(auth_key: str) -> Dict[str, str]:
1920
try:
20-
version = pkg_resources.get_distribution("uid2_client").version
21+
version = metadata.version("uid2_client")
2122
except Exception:
2223
version = "non-packaged-mode"
2324

0 commit comments

Comments
 (0)