Skip to content

Commit d0346ca

Browse files
committed
IDEV-2204: Move all decorators to a separate file
1 parent 8a6618b commit d0346ca

File tree

3 files changed

+60
-60
lines changed

3 files changed

+60
-60
lines changed

domaintools/api.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,15 @@
2424
Results,
2525
FeedsResults,
2626
)
27+
from domaintools.decorators import api_endpoint, auto_patch_docstrings
2728
from domaintools.filters import (
2829
filter_by_riskscore,
2930
filter_by_expire_date,
3031
filter_by_date_updated_after,
3132
filter_by_field,
3233
DTResultFilter,
3334
)
34-
from domaintools.utils import (
35-
api_endpoint,
36-
auto_patch_docstrings,
37-
validate_feeds_parameters,
38-
)
35+
from domaintools.utils import validate_feeds_parameters
3936

4037

4138
AVAILABLE_KEY_SIGN_HASHES = ["sha1", "sha256"]

domaintools/decorators.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import functools
2+
3+
from typing import List, Union
4+
5+
from domaintools.docstring_patcher import DocstringPatcher
6+
7+
8+
def api_endpoint(spec_name: str, path: str, methods: Union[str, List[str]]):
9+
"""
10+
Decorator to tag a method as an API endpoint.
11+
12+
Args:
13+
spec_name: The key for the spec in api_instance.specs
14+
path: The API path (e.g., "/users")
15+
methods: A single method ("get") or list of methods (["get", "post"])
16+
that this function handles.
17+
"""
18+
19+
def decorator(func):
20+
func._api_spec_name = spec_name
21+
func._api_path = path
22+
23+
# Always store the methods as a list
24+
if isinstance(methods, str):
25+
func._api_methods = [methods]
26+
else:
27+
func._api_methods = methods
28+
29+
@functools.wraps(func)
30+
def wrapper(self, *args, **kwargs):
31+
return func(*args, **kwargs)
32+
33+
# Copy all tags to the wrapper
34+
wrapper._api_spec_name = func._api_spec_name
35+
wrapper._api_path = func._api_path
36+
wrapper._api_methods = func._api_methods
37+
return wrapper
38+
39+
return decorator
40+
41+
42+
def auto_patch_docstrings(cls):
43+
original_init = cls.__init__
44+
45+
@functools.wraps(original_init)
46+
def new_init(self, *args, **kwargs):
47+
original_init(self, *args, **kwargs)
48+
try:
49+
# We instantiate our patcher and run it
50+
patcher = DocstringPatcher()
51+
patcher.patch(self)
52+
except Exception as e:
53+
print(f"Auto-patching failed: {e}")
54+
55+
cls.__init__ = new_init
56+
57+
return cls

domaintools/utils.py

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import functools
21
import re
32

43
from datetime import datetime
5-
from typing import List, Optional, Union
4+
from typing import Optional
65

76
from domaintools.constants import Endpoint, OutputFormat
8-
from domaintools.docstring_patcher import DocstringPatcher
97

108

119
def get_domain_age(create_date):
@@ -187,55 +185,3 @@ def validate_feeds_parameters(params):
187185
endpoint = params.get("endpoint")
188186
if endpoint == Endpoint.DOWNLOAD.value and format == OutputFormat.CSV.value:
189187
raise ValueError(f"{format} format is not available in {Endpoint.DOWNLOAD.value} API.")
190-
191-
192-
def api_endpoint(spec_name: str, path: str, methods: Union[str, List[str]]):
193-
"""
194-
Decorator to tag a method as an API endpoint.
195-
196-
Args:
197-
spec_name: The key for the spec in api_instance.specs
198-
path: The API path (e.g., "/users")
199-
methods: A single method ("get") or list of methods (["get", "post"])
200-
that this function handles.
201-
"""
202-
203-
def decorator(func):
204-
func._api_spec_name = spec_name
205-
func._api_path = path
206-
207-
# Always store the methods as a list
208-
if isinstance(methods, str):
209-
func._api_methods = [methods]
210-
else:
211-
func._api_methods = methods
212-
213-
@functools.wraps(func)
214-
def wrapper(self, *args, **kwargs):
215-
return func(*args, **kwargs)
216-
217-
# Copy all tags to the wrapper
218-
wrapper._api_spec_name = func._api_spec_name
219-
wrapper._api_path = func._api_path
220-
wrapper._api_methods = func._api_methods
221-
return wrapper
222-
223-
return decorator
224-
225-
226-
def auto_patch_docstrings(cls):
227-
original_init = cls.__init__
228-
229-
@functools.wraps(original_init)
230-
def new_init(self, *args, **kwargs):
231-
original_init(self, *args, **kwargs)
232-
try:
233-
# We instantiate our patcher and run it
234-
patcher = DocstringPatcher()
235-
patcher.patch(self)
236-
except Exception as e:
237-
print(f"Auto-patching failed: {e}")
238-
239-
cls.__init__ = new_init
240-
241-
return cls

0 commit comments

Comments
 (0)