Skip to content

Commit a4d9525

Browse files
committed
refactor: rearrange imports and add missing blank lines
More info: - Surround top-level function and class definitions with two blank lines: https://www.python.org/dev/peps/pep-0008/#blank-lines - Imports grouping and ordering: https://www.python.org/dev/peps/pep-0008/#imports
1 parent 0568556 commit a4d9525

25 files changed

+73
-29
lines changed

ibm_cloud_sdk_core/api_exception.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16-
from typing import Optional
16+
1717
from http import HTTPStatus
18+
from typing import Optional
19+
1820
from requests import Response
1921

2022

ibm_cloud_sdk_core/authenticators/authenticator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from abc import ABC, abstractmethod
1818

19+
1920
class Authenticator(ABC):
2021
"""This interface defines the common methods and constants associated with an Authenticator implementation."""
2122
@abstractmethod

ibm_cloud_sdk_core/authenticators/bearer_token_authenticator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from .authenticator import Authenticator
2020

21+
2122
class BearerTokenAuthenticator(Authenticator):
2223
"""The BearerTokenAuthenticator will add a user-supplied bearer token
2324
to requests.

ibm_cloud_sdk_core/authenticators/iam_authenticator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from ..token_managers.iam_token_manager import IAMTokenManager
2121
from ..utils import has_bad_first_or_last_char
2222

23+
2324
class IAMAuthenticator(IAMRequestBasedAuthenticator):
2425
"""The IAMAuthenticator utilizes an apikey, or client_id and client_secret pair to
2526
obtain a suitable bearer token, and adds it to requests.

ibm_cloud_sdk_core/authenticators/iam_request_based_authenticator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from .authenticator import Authenticator
2222

23+
2324
class IAMRequestBasedAuthenticator(Authenticator):
2425
"""The IAMRequestBasedAuthenticator class contains code that is common to all authenticators
2526
that need to interact with the IAM tokens service to obtain an access token.

ibm_cloud_sdk_core/authenticators/no_auth_authenticator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from .authenticator import Authenticator
1818

19+
1920
class NoAuthAuthenticator(Authenticator):
2021
"""Performs no authentication."""
2122
authentication_type = 'noAuth'

ibm_cloud_sdk_core/base_service.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,27 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
import logging
18-
from http.cookiejar import CookieJar
17+
import gzip
1918
import json as json_import
20-
from os.path import basename
19+
import logging
2120
import platform
2221
import sys
23-
import gzip
22+
from http.cookiejar import CookieJar
23+
from os.path import basename
2424
from typing import Dict, List, Optional, Tuple, Union
25+
from urllib3.util.retry import Retry
2526

2627
import requests
2728
from requests.adapters import HTTPAdapter
2829
from requests.structures import CaseInsensitiveDict
29-
from urllib3.util.retry import Retry
30+
3031
from ibm_cloud_sdk_core.authenticators import Authenticator
31-
from .version import __version__
32-
from .utils import (has_bad_first_or_last_char, remove_null_values,
33-
cleanup_values, read_external_sources, strip_extra_slashes)
34-
from .detailed_response import DetailedResponse
3532
from .api_exception import ApiException
33+
from .detailed_response import DetailedResponse
3634
from .token_managers.token_manager import TokenManager
35+
from .utils import (has_bad_first_or_last_char, remove_null_values,
36+
cleanup_values, read_external_sources, strip_extra_slashes)
37+
from .version import __version__
3738

3839
# Uncomment this to enable http debugging
3940
# import http.client as http_client

ibm_cloud_sdk_core/detailed_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import json
1718
from typing import Dict, Optional
1819

19-
import json
2020
import requests
2121

22+
2223
class DetailedResponse:
2324
"""Custom class for detailed response returned from APIs.
2425

ibm_cloud_sdk_core/get_authenticator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
CloudPakForDataAuthenticator, IAMAuthenticator, NoAuthAuthenticator)
1919
from .utils import read_external_sources
2020

21+
2122
def get_authenticator_from_environment(service_name: str) -> Authenticator:
2223
"""Look for external configuration of authenticator.
2324
@@ -38,6 +39,7 @@ def get_authenticator_from_environment(service_name: str) -> Authenticator:
3839
authenticator = __construct_authenticator(config)
3940
return authenticator
4041

42+
4143
def __construct_authenticator(config: dict) -> Authenticator:
4244
auth_type = config.get('AUTH_TYPE').lower() if config.get('AUTH_TYPE') else 'iam'
4345
authenticator = None

ibm_cloud_sdk_core/token_managers/jwt_token_manager.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919

2020
import jwt
2121
import requests
22+
2223
from .token_manager import TokenManager
2324
from ..api_exception import ApiException
2425

2526

26-
# pylint: disable=too-many-instance-attributes
27-
28-
2927
class JWTTokenManager(TokenManager, ABC):
3028
"""An abstract class to contain functionality for parsing, storing, and requesting JWT tokens.
3129

ibm_cloud_sdk_core/token_managers/token_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
# limitations under the License.
1616

1717

18+
import time
1819
from abc import ABC, abstractmethod
1920
from threading import Lock
2021

21-
import time
2222
import requests
2323

2424
from ..api_exception import ApiException
2525

2626

2727
# pylint: disable=too-many-instance-attributes
28-
2928
class TokenManager(ABC):
3029
"""An abstract class to contain functionality for parsing, storing, and requesting tokens.
3130

ibm_cloud_sdk_core/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def string_to_datetime_list(string_list: List[str]) -> List[datetime.datetime]:
134134
datetime_list.append(string_to_datetime(string_val))
135135
return datetime_list
136136

137+
137138
def datetime_to_string_list(datetime_list: List[datetime.datetime]) -> List[str]:
138139
"""Convert a list of datetime objects to a list of strings.
139140
@@ -151,6 +152,7 @@ def datetime_to_string_list(datetime_list: List[datetime.datetime]) -> List[str]
151152
string_list.append(datetime_to_string(datetime_val))
152153
return string_list
153154

155+
154156
def date_to_string(val: datetime.date) -> str:
155157
"""Convert a date object to string.
156158
@@ -176,6 +178,7 @@ def string_to_date(string: str) -> datetime.date:
176178
"""
177179
return date_parser.parse(string).date()
178180

181+
179182
def get_query_param(url_str: str, param: str) -> str:
180183
"""Return a query parameter value from url_str
181184
@@ -200,6 +203,7 @@ def get_query_param(url_str: str, param: str) -> str:
200203
values = query.get(param)
201204
return values[0] if values else None
202205

206+
203207
def convert_model(val: any) -> dict:
204208
"""Convert a model object into an equivalent dict.
205209

test/test_api_exception.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# coding=utf-8
22
import json
3-
import responses
3+
44
import requests
5+
import responses
6+
57
from ibm_cloud_sdk_core import ApiException
68

9+
710
@responses.activate
811
def test_api_exception():
912
"""Test APIException class"""

test/test_base_service.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
# coding=utf-8
22
# pylint: disable=missing-docstring,protected-access,too-few-public-methods
3+
import gzip
34
import json
4-
import time
55
import os
6+
import tempfile
7+
import time
68
from shutil import copyfile
79
from typing import Optional
8-
import gzip
9-
import tempfile
10+
from urllib3.exceptions import ConnectTimeoutError, MaxRetryError
11+
12+
import jwt
1013
import pytest
1114
import responses
1215
import requests
13-
import jwt
14-
from urllib3.exceptions import ConnectTimeoutError, MaxRetryError
15-
from ibm_cloud_sdk_core import BaseService, DetailedResponse
16+
1617
from ibm_cloud_sdk_core import ApiException
18+
from ibm_cloud_sdk_core import BaseService, DetailedResponse
1719
from ibm_cloud_sdk_core import CP4DTokenManager
20+
from ibm_cloud_sdk_core import get_authenticator_from_environment
1821
from ibm_cloud_sdk_core.authenticators import (IAMAuthenticator, NoAuthAuthenticator, Authenticator,
1922
BasicAuthenticator, CloudPakForDataAuthenticator)
20-
from ibm_cloud_sdk_core import get_authenticator_from_environment
2123
from ibm_cloud_sdk_core.utils import strip_extra_slashes
2224

2325

test/test_basic_authenticator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# pylint: disable=missing-docstring
22
import pytest
3+
34
from ibm_cloud_sdk_core.authenticators import BasicAuthenticator
45

6+
57
def test_basic_authenticator():
68
authenticator = BasicAuthenticator('my_username', 'my_password')
79
assert authenticator is not None
@@ -12,6 +14,7 @@ def test_basic_authenticator():
1214
authenticator.authenticate(request)
1315
assert request['headers']['Authorization'] == 'Basic bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQ='
1416

17+
1518
def test_basic_authenticator_validate_failed():
1619
with pytest.raises(ValueError) as err:
1720
BasicAuthenticator('my_username', None)

test/test_bearer_authenticator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from ibm_cloud_sdk_core.authenticators import BearerTokenAuthenticator
55

6+
67
def test_bearer_authenticator():
78
authenticator = BearerTokenAuthenticator('my_bearer_token')
89
assert authenticator is not None
@@ -15,6 +16,7 @@ def test_bearer_authenticator():
1516
authenticator.authenticate(request)
1617
assert request['headers']['Authorization'] == 'Bearer james bond'
1718

19+
1820
def test_bearer_validate_failed():
1921
with pytest.raises(ValueError) as err:
2022
BearerTokenAuthenticator(None)

test/test_cp4d_authenticator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# pylint: disable=missing-docstring
22
import json
33

4+
import jwt
45
import pytest
56
import responses
6-
import jwt
77

88
from ibm_cloud_sdk_core.authenticators import CloudPakForDataAuthenticator
99

test/test_cp4d_token_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import json
33
import time
44

5-
import responses
65
import jwt
6+
import responses
7+
78
from ibm_cloud_sdk_core import CP4DTokenManager
89

10+
911
@responses.activate
1012
def test_request_token():
1113
url = "https://test"

test/test_detailed_response.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
import responses
66
import requests
7+
78
from ibm_cloud_sdk_core import DetailedResponse
89

10+
911
def clean(val):
1012
"""Eliminate all whitespace and convert single to double quotes"""
1113
return val.translate(str.maketrans('', '', ' \n\t\r')).replace("'", "\"")
1214

15+
1316
@responses.activate
1417
def test_detailed_response_dict():
1518
responses.add(responses.GET,
@@ -31,6 +34,7 @@ def test_detailed_response_dict():
3134
#assert clean(detailed_response.get_headers().__str__()) in response_str
3235
assert clean(detailed_response.get_status_code().__str__()) in response_str
3336

37+
3438
@responses.activate
3539
def test_detailed_response_list():
3640
responses.add(responses.GET,

test/test_iam_authenticator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# pylint: disable=missing-docstring
22
import json
33

4+
import jwt
45
import pytest
56
import responses
6-
import jwt
77

88
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
99

test/test_iam_token_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import os
33
import time
44

5-
import responses
65
import jwt
76
import pytest
7+
import responses
88

99
from ibm_cloud_sdk_core import IAMTokenManager, ApiException, get_authenticator_from_environment
1010

test/test_jwt_token_manager.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import time
33
import threading
44
from typing import Optional
5+
56
import jwt
67
import pytest
78

89
from ibm_cloud_sdk_core import JWTTokenManager, DetailedResponse
910

11+
1012
class JWTTokenManagerMockImpl(JWTTokenManager):
1113
def __init__(self, url: Optional[str] = None, access_token: Optional[str] = None) -> None:
1214
self.url = url
@@ -45,9 +47,11 @@ def request_token(self) -> DetailedResponse:
4547
time.sleep(0.5)
4648
return response
4749

50+
4851
def _get_current_time() -> int:
4952
return int(time.time())
5053

54+
5155
def test_get_token():
5256
url = "https://iam.cloud.ibm.com/identity/token"
5357
token_manager = JWTTokenManagerMockImpl(url)
@@ -70,6 +74,7 @@ def test_get_token():
7074
assert token != "old_dummy"
7175
assert token_manager.request_count == 2
7276

77+
7378
def test_paced_get_token():
7479
url = "https://iam.cloud.ibm.com/identity/token"
7580
token_manager = JWTTokenManagerMockImpl(url)
@@ -82,6 +87,7 @@ def test_paced_get_token():
8287
thread.join()
8388
assert token_manager.request_count == 1
8489

90+
8591
def test_is_token_expired():
8692
token_manager = JWTTokenManagerMockImpl(None, access_token=None)
8793
assert token_manager._is_token_expired() is True
@@ -90,11 +96,13 @@ def test_is_token_expired():
9096
token_manager.expire_time = _get_current_time() - 3600
9197
assert token_manager._is_token_expired()
9298

99+
93100
def test_abstract_class_instantiation():
94101
with pytest.raises(TypeError) as err:
95102
JWTTokenManager(None)
96103
assert str(err.value).startswith("Can't instantiate abstract class JWTTokenManager with abstract")
97104

105+
98106
def test_disable_ssl_verification():
99107
token_manager = JWTTokenManagerMockImpl('https://iam.cloud.ibm.com/identity/token')
100108
token_manager.set_disable_ssl_verification(True)

0 commit comments

Comments
 (0)