Skip to content

Commit 008f1a3

Browse files
authored
Cache properties and methods (#25)
1 parent 14d1fe4 commit 008f1a3

21 files changed

+138
-98
lines changed
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
1+
from functools import cached_property
2+
13
from ..core.bases import JSONResponse
24

35

46
class CompanyEnrichmentResponse(JSONResponse):
57
"""Company enrichment service response."""
68

7-
@property
9+
@cached_property
810
def name(self) -> str | None:
911
"""The name of the company."""
1012
return self._get_response_field("name")
1113

12-
@property
14+
@cached_property
1315
def domain(self) -> str | None:
1416
"""The domain the company website is hosted on."""
1517
return self._get_response_field("domain")
1618

17-
@property
19+
@cached_property
1820
def year_founded(self) -> int | None:
1921
"""The year the company was founded."""
2022
return self._get_response_field("year_founded")
2123

22-
@property
24+
@cached_property
2325
def industry(self) -> str | None:
2426
"""The industry the company is operating in."""
2527
return self._get_response_field("industry")
2628

27-
@property
29+
@cached_property
2830
def employees_count(self) -> str | None:
2931
"""The approximate number of employees of the company."""
3032
return self._get_response_field("employees_count")
3133

32-
@property
34+
@cached_property
3335
def locality(self) -> str | None:
3436
"""The city or region the company headquarter is based in."""
3537
return self._get_response_field("locality")
3638

37-
@property
39+
@cached_property
3840
def country(self) -> str | None:
3941
"""The country the company is based in."""
4042
return self._get_response_field("country")
4143

42-
@property
44+
@cached_property
4345
def linkedin_url(self) -> str | None:
4446
"""The LinkedIn URL of the company."""
4547
return self._get_response_field("linkedin_url")

src/abstract_api/core/bases/base_service.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import lru_cache
12
from io import BytesIO
23
from typing import TYPE_CHECKING, Any, Final, Generic, Type, TypeVar
34

@@ -36,7 +37,8 @@ def __init__(self, api_key: str) -> None:
3637
"""
3738
self._api_key: str = api_key
3839

39-
def _service_url(self, action: str = "") -> str:
40+
@lru_cache(maxsize=5)
41+
def __service_url(self, action: str = "") -> str:
4042
"""Builds and returns an API URL for a service using its subdomain.
4143
4244
Args:
@@ -80,7 +82,7 @@ def _service_request(
8082

8183
request_kwargs: dict[str, Any] = {
8284
"method": _method,
83-
"url": self._service_url(_action)
85+
"url": self.__service_url(_action)
8486
}
8587

8688
if _method == "get":

src/abstract_api/email_validation/email_validation_response.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import cached_property
12
from typing import Any
23

34
import requests
@@ -43,12 +44,12 @@ def __init__(
4344
"""Initializes a new EmailValidationResponse."""
4445
super().__init__(response, RESPONSE_FIELDS)
4546

46-
@property
47+
@cached_property
4748
def email(self) -> str | None:
4849
"""The value for “email” that was entered into the request."""
4950
return self._get_response_field("email")
5051

51-
@property
52+
@cached_property
5253
def autocorrect(self) -> str | None:
5354
"""The auto corrected email.
5455
@@ -60,15 +61,15 @@ def autocorrect(self) -> str | None:
6061
"""
6162
return self._get_response_field("autocorrect")
6263

63-
@property
64+
@cached_property
6465
def deliverability(self) -> str | None:
6566
"""Abstract's evaluation of the deliverability of the email.
6667
6768
Possible values are: DELIVERABLE, UNDELIVERABLE, and UNKNOWN.
6869
"""
6970
return self._get_response_field("deliverability")
7071

71-
@property
72+
@cached_property
7273
def quality_score(self) -> float | None:
7374
"""Decimal score for quality and deliverability of the submitted email.
7475
@@ -77,7 +78,7 @@ def quality_score(self) -> float | None:
7778
"""
7879
return self._get_response_field("quality_score")
7980

80-
@property
81+
@cached_property
8182
def is_valid_format(self) -> bool | None:
8283
"""Whether the email follows the format of “address @ domain . TLD”.
8384
@@ -86,7 +87,7 @@ def is_valid_format(self) -> bool | None:
8687
"""
8788
return self._get_response_field("is_valid_format")
8889

89-
@property
90+
@cached_property
9091
def is_free_email(self) -> bool | None:
9192
"""Whether email's domain is a free to use email.
9293
@@ -95,7 +96,7 @@ def is_free_email(self) -> bool | None:
9596
"""
9697
return self._get_response_field("is_free_email")
9798

98-
@property
99+
@cached_property
99100
def is_disposable_email(self) -> bool | None:
100101
"""Whether the email is disposable.
101102
@@ -104,7 +105,7 @@ def is_disposable_email(self) -> bool | None:
104105
"""
105106
return self._get_response_field("is_disposable_email")
106107

107-
@property
108+
@cached_property
108109
def is_role_email(self) -> bool | None:
109110
"""Whether the email represents a role and not an individual.
110111
@@ -114,12 +115,12 @@ def is_role_email(self) -> bool | None:
114115
"""
115116
return self._get_response_field("is_role_email")
116117

117-
@property
118+
@cached_property
118119
def is_catchall_email(self) -> bool | None:
119120
"""Whether the domain is configured to catch all email."""
120121
return self._get_response_field("is_catchall_email")
121122

122-
@property
123+
@cached_property
123124
def is_mx_found(self) -> bool | None:
124125
"""Whether MX Records for the domain can be found.
125126
@@ -128,7 +129,7 @@ def is_mx_found(self) -> bool | None:
128129
"""
129130
return self._get_response_field("is_mx_found")
130131

131-
@property
132+
@cached_property
132133
def is_smtp_valid(self) -> bool | None:
133134
"""Whether the SMTP check of the email was successful.
134135

src/abstract_api/exchange_rates/_multiple_exchange_rates_response.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import cached_property
12
from typing import Any
23

34
from ..core.bases import JSONResponse
@@ -44,12 +45,12 @@ def _init_response_field(self, field: str, value: Any) -> None:
4445
value = tuple(exchange_rates)
4546
super()._init_response_field(field, value)
4647

47-
@property
48+
@cached_property
4849
def base(self) -> str | None:
4950
"""The base currency used to get the exchange rates."""
5051
return self._get_response_field("base")
5152

52-
@property
53+
@cached_property
5354
def exchange_rates(self) -> tuple[ExchangeRate] | None:
5455
"""Target currencies exchange rates versus the base currency."""
5556
return self._get_response_field("exchange_rates")
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from functools import cached_property
2+
13
import requests
24

35
from ..core.bases import JSONResponse
@@ -11,30 +13,30 @@ def __init__(self, response: requests.models.Response) -> None:
1113
"""Initializes a new ExchangeRateConversionResponse."""
1214
super().__init__(response, CONVERSION_RESPONSE_FIELDS)
1315

14-
@property
16+
@cached_property
1517
def base(self) -> str | None:
1618
"""The base currency used to get the exchange rates."""
1719
return self._get_response_field("base")
1820

19-
@property
21+
@cached_property
2022
def target(self) -> str | None:
2123
"""The target currency that the base_amount was converted into."""
2224
return self._get_response_field("target")
2325

24-
@property
26+
@cached_property
2527
def date(self) -> str | None:
2628
"""The date the currencies were pulled from.
2729
2830
This is per successful request.
2931
"""
3032
return self._get_response_field("date")
3133

32-
@property
34+
@cached_property
3335
def base_amount(self) -> str | None:
3436
"""The amount of the base currency from the request."""
3537
return self._get_response_field("base_amount")
3638

37-
@property
39+
@cached_property
3840
def converted_amount(self) -> str | None:
3941
"""The amount after conversion.
4042
@@ -43,12 +45,12 @@ def converted_amount(self) -> str | None:
4345
"""
4446
return self._get_response_field("converted_amount")
4547

46-
@property
48+
@cached_property
4749
def exchange_rate(self) -> str | None:
4850
"""The exchange rate used in conversion."""
4951
return self._get_response_field("exchange_rate")
5052

51-
@property
53+
@cached_property
5254
def last_updated(self) -> str | None:
5355
"""The Unix timestamp of when the returned data was last updated."""
5456
return self._get_response_field("last_updated")

src/abstract_api/exchange_rates/historical_exchange_rates_response.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from functools import cached_property
2+
13
import requests
24

35
from ._multiple_exchange_rates_response import MultipleExchangeRatesResponse
@@ -11,7 +13,7 @@ def __init__(self, response: requests.models.Response) -> None:
1113
"""Initializes a new HistoricalExchangeRatesResponse."""
1214
super().__init__(response, HISTORICAL_RESPONSE_FIELDS)
1315

14-
@property
16+
@cached_property
1517
def date(self) -> str | None:
1618
"""The date the currencies were pulled from.
1719

src/abstract_api/exchange_rates/live_exchange_rates_response.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from functools import cached_property
2+
13
import requests
24

35
from ._multiple_exchange_rates_response import MultipleExchangeRatesResponse
@@ -11,7 +13,7 @@ def __init__(self, response: requests.models.Response) -> None:
1113
"""Initializes a new LiveExchangeRatesResponse."""
1214
super().__init__(response, LIVE_RESPONSE_FIELDS)
1315

14-
@property
16+
@cached_property
1517
def last_updated(self) -> int | None:
1618
"""The Unix timestamp of when the returned data was last updated."""
1719
return self._get_response_field("last_updated")

src/abstract_api/holidays/holidays_response.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import cached_property
12
from typing import Any
23

34
import requests
@@ -134,13 +135,13 @@ def _init_response_field(
134135
holidays = []
135136
for c in value:
136137
holidays.append(Holiday(**c))
137-
self._holidays = tuple(holidays)
138+
self._holidays: tuple[Holiday, ...] = tuple(holidays)
138139

139140
def __init__(self, response: requests.models.Response) -> None:
140141
"""Initializes a new HolidaysResponse."""
141142
super().__init__(response, RESPONSE_FIELDS, list_response=True)
142143

143-
@property
144-
def holidays(self) -> tuple[Holiday]:
144+
@cached_property
145+
def holidays(self) -> tuple[Holiday, ...]:
145146
"""The returned holidays."""
146147
return self._get_response_field("holidays")

src/abstract_api/iban_validation/iban_validation_response.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from functools import cached_property
2+
13
import requests
24

35
from ..core.bases import JSONResponse
@@ -11,12 +13,12 @@ def __init__(self, response: requests.models.Response) -> None:
1113
"""Initializes a new IBANValidationResponse."""
1214
super().__init__(response, RESPONSE_FIELDS)
1315

14-
@property
16+
@cached_property
1517
def iban(self) -> str:
1618
"""The IBAN submitted for validation."""
1719
return self._get_response_field("iban")
1820

19-
@property
21+
@cached_property
2022
def is_valid(self) -> bool:
2123
"""Whether the IBAN submitted is valid."""
2224
return self._get_response_field("is_valid")
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from functools import cached_property
2+
13
import requests
24

35
from ..core.bases import JSONResponse
@@ -13,42 +15,42 @@ def __init__(
1315
"""Initializes a new ImageProcessingResponse."""
1416
super().__init__(response, RESPONSE_FIELDS)
1517

16-
@property
18+
@cached_property
1719
def original_size(self) -> str | None:
1820
"""The original size of the provided image, in bytes."""
1921
return self._get_response_field("original_size")
2022

21-
@property
23+
@cached_property
2224
def original_height(self) -> str | None:
2325
"""The original height of the provided image, in bytes."""
2426
return self._get_response_field("original_height")
2527

26-
@property
28+
@cached_property
2729
def original_width(self) -> str | None:
2830
"""The original width of the provided image, in bytes."""
2931
return self._get_response_field("original_width")
3032

31-
@property
33+
@cached_property
3234
def final_size(self) -> str | None:
3335
"""The final size of the processed image, in bytes."""
3436
return self._get_response_field("final_size")
3537

36-
@property
38+
@cached_property
3739
def bytes_saved(self) -> str | None:
3840
"""The number of bytes saved by optimizing the image, in bytes."""
3941
return self._get_response_field("bytes_saved")
4042

41-
@property
43+
@cached_property
4244
def final_height(self) -> str | None:
4345
"""The final height of the processed image, in bytes."""
4446
return self._get_response_field("final_height")
4547

46-
@property
48+
@cached_property
4749
def final_width(self) -> str | None:
4850
"""The final width of the processed image, in bytes."""
4951
return self._get_response_field("final_width")
5052

51-
@property
53+
@cached_property
5254
def url(self) -> str | None:
5355
"""The URL with the new processed image."""
5456
return self._get_response_field("url")

0 commit comments

Comments
 (0)