22
22
import requests
23
23
from requests .structures import CaseInsensitiveDict
24
24
from .version import __version__
25
- from .utils import has_bad_first_or_last_char , remove_null_values , cleanup_values
25
+ from .utils import has_bad_first_or_last_char , remove_null_values , cleanup_values , read_from_external_sources
26
26
from .detailed_response import DetailedResponse
27
27
from .api_exception import ApiException
28
28
from .authenticators import Authenticator , BasicAuthenticator , BearerTokenAuthenticator , CloudPakForDataAuthenticator , IamAuthenticator , NoauthAuthenticator
34
34
35
35
36
36
class BaseService (object ):
37
- DEFAULT_CREDENTIALS_FILE_NAME = 'ibm-credentials.env'
37
+
38
38
SDK_NAME = 'ibm-python-sdk-core'
39
39
40
40
def __init__ (self ,
41
- vcap_services_name ,
42
41
url ,
43
42
authenticator = None ,
44
43
disable_ssl_verification = False ,
45
44
display_name = None ):
46
45
"""
47
- It loads credentials with the following preference:
48
- 1) Credentials explicitly set in the authenticator
49
- 2) Credentials loaded from credentials file if present
50
- 3) Credentials loaded from VCAP_SERVICES environment variable
51
-
52
- :attr str vcap_services_name: The vcap service name
53
46
:attr str url: The url for service api calls
47
+ :attr Athenticator authenticator: The authenticator for authentication
54
48
:attr bool disable_ssl_verification: enables/ disabled ssl verification
55
- :attr str display_name the name used for mapping services in credential file
49
+ :attr str display_name the name used for mapping services in environment file
56
50
"""
57
51
self .url = url
58
52
self .http_config = {}
@@ -63,114 +57,19 @@ def __init__(self,
63
57
64
58
self ._set_user_agent_header (self ._build_user_agent ())
65
59
66
- # 1. Credentials are passed in constructor
67
60
if self .authenticator :
68
61
if not isinstance (self .authenticator , Authenticator ):
69
62
raise ValueError (
70
63
'authenticator should be of type Authenticator' )
71
64
72
- # 2. Credentials from credential file
73
- if display_name and not self .authenticator :
65
+ if display_name :
74
66
service_name = display_name .replace (' ' , '_' ).lower ()
75
- self ._load_from_credential_file (service_name )
76
-
77
- # 3. Credentials from VCAP
78
- if not self .authenticator :
79
- vcap_service_credentials = self ._load_from_vcap_services (
80
- vcap_services_name )
81
- if vcap_service_credentials is not None and isinstance (
82
- vcap_service_credentials , dict ):
83
- if vcap_service_credentials .get ('username' ) and vcap_service_credentials .get ('password' ): # cf
84
- vcap_service_credentials ['auth_type' ] = 'basic'
85
- elif vcap_service_credentials .get ('apikey' ): # rc
86
- vcap_service_credentials ['auth_type' ] = 'iam'
87
- self ._set_authenticator_properties (vcap_service_credentials )
88
- self ._set_service_properties (vcap_service_credentials )
89
-
90
- if not self .authenticator :
91
- self .authenticator = NoauthAuthenticator ()
92
-
93
- def _load_from_credential_file (self , service_name , separator = '=' ):
94
- """
95
- Initiates the credentials based on the credential file
67
+ config = read_from_external_sources (service_name )
68
+ if config .get ('url' ):
69
+ self .url = config .get ('url' )
70
+ if config .get ('disable_ssl' ):
71
+ self .disable_ssl_verification = config .get ('disable_ssl' )
96
72
97
- :param str service_name: The service name
98
- :param str separator: the separator for key value pair
99
- """
100
- # File path specified by an env variable
101
- credential_file_path = os .getenv ('IBM_CREDENTIALS_FILE' )
102
-
103
- # Home directory
104
- if credential_file_path is None :
105
- file_path = join (
106
- expanduser ('~' ), self .DEFAULT_CREDENTIALS_FILE_NAME )
107
- if isfile (file_path ):
108
- credential_file_path = file_path
109
-
110
- # Top-level of the project directory
111
- if credential_file_path is None :
112
- file_path = join (
113
- dirname (dirname (abspath (__file__ ))),
114
- self .DEFAULT_CREDENTIALS_FILE_NAME )
115
- if isfile (file_path ):
116
- credential_file_path = file_path
117
-
118
- properties = {}
119
- if credential_file_path is not None :
120
- with open (credential_file_path , 'r' ) as fp :
121
- for line in fp :
122
- key_val = line .strip ().split (separator )
123
- if len (key_val ) == 2 :
124
- key = key_val [0 ].lower ()
125
- value = key_val [1 ]
126
- if service_name in key :
127
- index = key .find ('_' )
128
- if index != - 1 :
129
- properties [key [index + 1 :]] = value
130
-
131
- if properties :
132
- self ._set_authenticator_properties (properties )
133
- self ._set_service_properties (properties )
134
-
135
- def _set_authenticator_properties (self , properties ):
136
- auth_type = properties .get ('auth_type' )
137
- if auth_type == 'basic' :
138
- self .authenticator = BasicAuthenticator (
139
- username = properties .get ('username' ),
140
- password = properties .get ('password' ))
141
- elif auth_type == 'bearerToken' :
142
- self .authenticator = BearerTokenAuthenticator (
143
- bearer_token = properties .get ('bearer_token' ))
144
- elif auth_type == 'cp4d' :
145
- self .authenticator = CloudPakForDataAuthenticator (
146
- username = properties .get ('username' ),
147
- password = properties .get ('password' ),
148
- url = properties .get ('auth_url' ),
149
- disable_ssl_verification = properties .get ('auth_disable_ssl' ))
150
- elif auth_type == 'iam' :
151
- self .authenticator = IamAuthenticator (
152
- apikey = properties .get ('apikey' ),
153
- url = properties .get ('auth_url' ),
154
- client_id = properties .get ('client_id' ),
155
- client_secret = properties .get ('client_secret' ),
156
- disable_ssl_verification = properties .get ('auth_disable_ssl' ))
157
- elif auth_type == 'noauth' :
158
- self .authenticator = NoauthAuthenticator ()
159
-
160
- def _set_service_properties (self , properties ):
161
- if 'url' in properties :
162
- self .url = properties .get ('url' )
163
- if 'disable_ssl' in properties :
164
- self .disable_ssl_verification = properties .get ('disable_ssl' )
165
-
166
- def _load_from_vcap_services (self , service_name ):
167
- vcap_services = os .getenv ('VCAP_SERVICES' )
168
- if vcap_services is not None :
169
- services = json_import .loads (vcap_services )
170
- if service_name in services :
171
- return services [service_name ][0 ]['credentials' ]
172
- else :
173
- return None
174
73
175
74
def _get_system_info (self ):
176
75
return '{0} {1} {2}' .format (
@@ -237,7 +136,6 @@ def send(self, request, **kwargs):
237
136
kwargs ['verify' ] = False
238
137
239
138
response = requests .request (** request , cookies = self .jar , ** kwargs )
240
- print (response .headers )
241
139
242
140
if 200 <= response .status_code <= 299 :
243
141
if response .status_code == 204 or request ['method' ] == 'HEAD' :
0 commit comments