Skip to content

Commit 7a1f21b

Browse files
committed
v0.0.4
1 parent 43fe064 commit 7a1f21b

7 files changed

Lines changed: 71 additions & 60 deletions

File tree

python_paypal_api/auth/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from .access_token_client import AccessTokenClient
22
from .access_token_response import AccessTokenResponse
33
from .credentials import Credentials
4+
from .exceptions import AuthorizationError
45

56
__all__ = [
67
'AccessTokenResponse',
78
'AccessTokenClient',
89
'Credentials',
10+
'AuthorizationError'
911
]

python_paypal_api/auth/access_token_client.py

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import requests
2+
from requests.auth import HTTPBasicAuth
23
import hashlib
34
import logging
45
import confuse
5-
from python_paypal_api.base import BaseClient
6-
from python_paypal_api.base.enum import EndPoint
7-
from .credentials import Credentials
8-
from .access_token_response import AccessTokenResponse
9-
from .exceptions import AuthorizationError
106
import os
117
import logging
128
import json
13-
149
from datetime import datetime, timedelta
15-
16-
from requests.auth import HTTPBasicAuth
17-
10+
from cachetools import TTLCache
11+
from python_paypal_api.base import BaseClient
12+
from python_paypal_api.base.enum import EndPoint
13+
from python_paypal_api.auth.credentials import Credentials
14+
from python_paypal_api.auth.access_token_response import AccessTokenResponse
15+
from python_paypal_api.auth.exceptions import AuthorizationError
1816

1917
logging.basicConfig(
2018
level=logging.INFO,
2119
format="%(asctime)s:%(levelname)s:%(message)s"
2220
)
2321

22+
cache = TTLCache(maxsize=10, ttl=timedelta(seconds=32400), timer=datetime.now)
2423

2524
class AccessTokenClient(BaseClient):
2625

2726
grant_type = 'client_credentials'
2827
path = '/v1/oauth2/token'
2928

30-
def __init__(self, account='default', credentials=None, proxies=None, verify=True, timeout=None):
29+
def __init__(self, account='default', credentials=None, store_credentials=True, proxies=None, verify=True, timeout=None):
3130

3231
self.cred = Credentials(credentials)
32+
self.store_credentials = store_credentials
3333
self.host = EndPoint[self.cred.client_mode].value if self.cred.client_mode is not None else EndPoint["SANDBOX"].value
3434
self.timeout = timeout
3535
self.proxies = proxies
@@ -77,32 +77,56 @@ def create_cache_token(self, file:str):
7777
fout.write(json_object)
7878
return access_token
7979

80-
8180
def get_auth(self) -> AccessTokenResponse:
8281

83-
now_datetime = datetime.now()
8482

85-
config = confuse.Configuration('python-paypal-api')
86-
file = os.path.join(config.config_dir(), self._get_cache_key())
87-
try:
88-
89-
openfile = open(file, 'r')
90-
access_token = json.load(openfile)
91-
future_datetime = datetime.fromisoformat(access_token["expire_time"])
92-
# openfile.close()
93-
94-
except FileNotFoundError:
95-
96-
access_token = self.create_cache_token(file)
97-
future_datetime = now_datetime + timedelta(seconds=access_token["expires_in"])
98-
99-
if now_datetime > future_datetime:
100-
if(os.path.isfile(file)):
101-
os.remove(file)
102-
access_token = self.create_cache_token(file)
83+
# logging.info("self.store_credentials")
84+
# logging.info(self.store_credentials)
85+
# logging.info(self.get_file_auth())
86+
87+
if self.store_credentials:
88+
89+
90+
now_datetime = datetime.now()
91+
92+
config = confuse.Configuration('python-paypal-api')
93+
file = os.path.join(config.config_dir(), self._get_cache_key())
94+
try:
95+
96+
openfile = open(file, 'r')
97+
access_token = json.load(openfile)
98+
future_datetime = datetime.fromisoformat(access_token["expire_time"])
99+
openfile.close()
100+
101+
except FileNotFoundError:
102+
103+
access_token = self.create_cache_token(file)
104+
future_datetime = now_datetime + timedelta(seconds=access_token["expires_in"])
105+
106+
if now_datetime > future_datetime:
107+
if (os.path.isfile(file)):
108+
os.remove(file)
109+
access_token = self.create_cache_token(file)
110+
111+
else:
112+
pass
113+
103114

104115
else:
105-
pass
116+
117+
118+
cache_key = self._get_cache_key()
119+
try:
120+
# logging.info("cache")
121+
access_token = cache[cache_key]
122+
except KeyError:
123+
# logging.info("request")
124+
request_url = self.scheme + self.host + self.path
125+
access_token = self._request(request_url, self.data, self.headers)
126+
cache[cache_key] = access_token
127+
# return AccessTokenResponse(**access_token)
128+
129+
106130

107131
return AccessTokenResponse(**access_token)
108132

python_paypal_api/base/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
from .base_client import BaseClient
22
from .client import Client
33
from .helpers import PaypalEndpoint, PaypalEndpointParams
4-
from .exceptions import PaypalApiException, PaypalApiRequestException, PaypalTypeException
5-
from .exceptions import PaypalApiBadRequestException, PaypalApiForbiddenException
6-
from .exceptions import PaypalApiUnprocessableEntityException
7-
from .exceptions import PaypalApiResourceNotFound
4+
from .exceptions import PaypalApiException, PaypalTypeException
85
from .credential_provider import CredentialProvider, MissingCredentials
96
from .api_response import ApiResponse
107
from .utils import Utils
@@ -18,11 +15,7 @@
1815
'PaypalEndpointParams',
1916
'PaypalEndpoint',
2017
'PaypalApiException',
21-
'PaypalApiRequestException',
22-
'PaypalApiBadRequestException',
23-
'PaypalApiUnprocessableEntityException',
24-
'PaypalApiResourceNotFound',
25-
'PaypalApiForbiddenException'
18+
'PaypalTypeException',
2619
'CredentialProvider',
2720
'MissingCredentials',
2821
'Utils',

python_paypal_api/base/client.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,27 @@
55
from python_paypal_api.auth.credentials import Credentials
66
from python_paypal_api.auth import AccessTokenClient, AccessTokenResponse
77
from python_paypal_api.base.credential_provider import CredentialProvider
8-
from .api_response import ApiResponse
9-
from .base_client import BaseClient
8+
from python_paypal_api.base.api_response import ApiResponse
9+
from python_paypal_api.base.base_client import BaseClient
1010
from python_paypal_api.base.enum import EndPoint
11-
from .exceptions import get_exception_for_content, get_exception_for_code
1211
import os
13-
import requests
14-
from io import BytesIO
15-
import gzip
16-
from zipfile import ZipFile
17-
import zipfile
18-
from urllib.parse import urlparse, quote
1912

2013
log = logging.getLogger(__name__)
2114

2215
class Client(BaseClient):
2316

24-
access_token_client_class = AccessTokenClient
25-
grantless_scope = ''
26-
2717
def __init__(
2818
self,
2919
account='default',
3020
credentials=None,
21+
credential_providers=None,
22+
store_credentials=False,
3123
proxies=None,
3224
verify=True,
3325
timeout=None,
34-
debug=False,
35-
credential_providers=None
26+
debug=False
3627
):
3728

38-
super().__init__(account, credentials)
39-
4029
self.credentials = CredentialProvider(
4130
account,
4231
credentials,
@@ -46,9 +35,11 @@ def __init__(
4635
self.host = EndPoint[self.credentials.client_mode].value if self.credentials.client_mode is not None else EndPoint["SANDBOX"].value
4736
self.endpoint = self.scheme + self.host
4837
self.debug = debug
49-
self._auth = self.access_token_client_class(
38+
self.store_credentials = store_credentials
39+
self._auth = AccessTokenClient(
5040
account=account,
5141
credentials=self.credentials,
42+
store_credentials=self.store_credentials,
5243
proxies=proxies,
5344
verify=verify,
5445
timeout=timeout,
@@ -65,6 +56,7 @@ def headers(self):
6556
# 'Content-Type': 'application/json'
6657
}
6758

59+
6860
@property
6961
def auth(self) -> AccessTokenResponse:
7062
return self._auth.get_auth()

python_paypal_api/base/credential_provider.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ def load_credentials(self):
7676

7777
class CredentialProvider:
7878
credentials = None
79-
logger.info("CredentialProvider")
8079

8180
CREDENTIAL_PROVIDERS: Iterable[Type[BaseCredentialProvider]] = (
8281
FromCodeCredentialProvider,

python_paypal_api/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.3"
1+
__version__ = "0.0.4"

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
setup(
4545
name='python-paypal-api',
46-
version='0.0.3',
46+
version='0.0.4',
4747
python_requires='>=3.8',
4848
author='denisneuf',
4949
author_email='denisneuf@hotmail.com',
@@ -55,6 +55,7 @@
5555
install_requires = [
5656
'requests>=2.27.1,<2.29.0',
5757
'confuse>=1.7,<2.1',
58+
'cachetools~=5.3.0'
5859
],
5960
license="Apache License 2.0",
6061
classifiers=[

0 commit comments

Comments
 (0)