Skip to content

Commit 8cad048

Browse files
committed
feat: simplify Configuration class, use inline types
1 parent 580bb53 commit 8cad048

File tree

2 files changed

+40
-146
lines changed

2 files changed

+40
-146
lines changed

fingerprint_pro_server_api_sdk/configuration.py

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
import logging
1515
import multiprocessing
1616
import sys
17-
import urllib3
1817
import http.client as httplib
1918

19+
from typing import Dict
2020

21-
class Configuration(object):
21+
22+
class Configuration:
2223
"""NOTE: This class is auto generated by the swagger code generator program.
2324
2425
Ref: https://github.com/swagger-api/swagger-codegen
@@ -27,7 +28,7 @@ class Configuration(object):
2728

2829
_default = None
2930

30-
def __init__(self, api_key, region="us"):
31+
def __init__(self, api_key: str, region: str = "us"):
3132
"""Constructor"""
3233
if self._default:
3334
for key in self._default.__dict__.keys():
@@ -41,20 +42,10 @@ def __init__(self, api_key, region="us"):
4142

4243
# Authentication Settings
4344
# dict to store API key(s)
44-
self.api_key = {}
45-
self.api_key['Auth-API-Key'] = api_key
46-
# dict to store API prefix (e.g. Bearer)
47-
self.api_key_prefix = {}
48-
# function to refresh API key if expired
49-
self.refresh_api_key_hook = None
50-
# Username for HTTP basic authentication
51-
self.username = ""
52-
# Password for HTTP basic authentication
53-
self.password = ""
45+
self.api_key = api_key
5446
# Logging Settings
55-
self.logger = {}
56-
self.logger["package_logger"] = logging.getLogger("fingerprint_pro_server_api_sdk")
57-
self.logger["urllib3_logger"] = logging.getLogger("urllib3")
47+
self.logger = {"package_logger": logging.getLogger("fingerprint_pro_server_api_sdk"),
48+
"urllib3_logger": logging.getLogger("urllib3")}
5849
# Log format
5950
self.logger_format = '%(asctime)s %(levelname)s %(message)s'
6051
# Log stream handler
@@ -99,26 +90,24 @@ def set_default(cls, default):
9990
cls._default = default
10091

10192
@property
102-
def logger_file(self):
93+
def logger_file(self) -> str:
10394
"""The logger file.
10495
10596
If the logger_file is None, then add stream handler and remove file
10697
handler. Otherwise, add file handler and remove stream handler.
10798
108-
:param value: The logger_file path.
109-
:type: str
99+
:return: The logger_file path.
110100
"""
111101
return self.__logger_file
112102

113103
@logger_file.setter
114-
def logger_file(self, value):
104+
def logger_file(self, value: str):
115105
"""The logger file.
116106
117107
If the logger_file is None, then add stream handler and remove file
118108
handler. Otherwise, add file handler and remove stream handler.
119109
120110
:param value: The logger_file path.
121-
:type: str
122111
"""
123112
self.__logger_file = value
124113
if self.__logger_file:
@@ -141,20 +130,18 @@ def logger_file(self, value):
141130
logger.removeHandler(self.logger_file_handler)
142131

143132
@property
144-
def debug(self):
133+
def debug(self) -> bool:
145134
"""Debug status
146135
147136
:param value: The debug status, True or False.
148-
:type: bool
149137
"""
150138
return self.__debug
151139

152140
@debug.setter
153-
def debug(self, value):
141+
def debug(self, value: bool):
154142
"""Debug status
155143
156144
:param value: The debug status, True or False.
157-
:type: bool
158145
"""
159146
self.__debug = value
160147
if self.__debug:
@@ -172,56 +159,27 @@ def debug(self, value):
172159
httplib.HTTPConnection.debuglevel = 0
173160

174161
@property
175-
def logger_format(self):
162+
def logger_format(self) -> str:
176163
"""The logger format.
177164
178165
The logger_formatter will be updated when sets logger_format.
179166
180-
:param value: The format string.
181-
:type: str
167+
:return: The format string.
182168
"""
183169
return self.__logger_format
184170

185171
@logger_format.setter
186-
def logger_format(self, value):
172+
def logger_format(self, value: str):
187173
"""The logger format.
188174
189175
The logger_formatter will be updated when sets logger_format.
190176
191177
:param value: The format string.
192-
:type: str
193178
"""
194179
self.__logger_format = value
195180
self.logger_formatter = logging.Formatter(self.__logger_format)
196181

197-
def get_api_key_with_prefix(self, identifier):
198-
"""Gets API key (with prefix if set).
199-
200-
:param identifier: The identifier of apiKey.
201-
:return: The token for api key authentication.
202-
"""
203-
204-
if self.refresh_api_key_hook:
205-
self.refresh_api_key_hook(self)
206-
207-
key = self.api_key.get(identifier)
208-
if key:
209-
prefix = self.api_key_prefix.get(identifier)
210-
if prefix:
211-
return "%s %s" % (prefix, key)
212-
else:
213-
return key
214-
215-
def get_basic_auth_token(self):
216-
"""Gets HTTP basic authentication header (string).
217-
218-
:return: The token for basic HTTP authentication.
219-
"""
220-
return urllib3.util.make_headers(
221-
basic_auth=self.username + ':' + self.password
222-
).get('authorization')
223-
224-
def auth_settings(self):
182+
def auth_settings(self) -> Dict[str, Dict[str, str]]:
225183
"""Gets Auth Settings dict for api client.
226184
227185
:return: The Auth Settings information dict.
@@ -232,18 +190,11 @@ def auth_settings(self):
232190
'type': 'api_key',
233191
'in': 'header',
234192
'key': 'Auth-API-Key',
235-
'value': self.get_api_key_with_prefix('Auth-API-Key')
236-
},
237-
'ApiKeyQuery':
238-
{
239-
'type': 'api_key',
240-
'in': 'query',
241-
'key': 'api_key',
242-
'value': self.get_api_key_with_prefix('api_key')
193+
'value': self.api_key
243194
},
244195
}
245196

246-
def get_host(self, region):
197+
def get_host(self, region: str) -> str:
247198
return {
248199
"us": "https://api.fpjs.io",
249200
"eu": "https://eu.api.fpjs.io",

template/configuration.mustache

Lines changed: 22 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import copy
66
import logging
77
import multiprocessing
88
import sys
9-
import urllib3
109
import http.client as httplib
1110

11+
from typing import Dict
1212

13-
class Configuration(object):
13+
14+
class Configuration:
1415
"""NOTE: This class is auto generated by the swagger code generator program.
1516

1617
Ref: https://github.com/swagger-api/swagger-codegen
@@ -19,7 +20,7 @@ class Configuration(object):
1920

2021
_default = None
2122

22-
def __init__(self, api_key, region="us"):
23+
def __init__(self, api_key: str, region: str = "us"):
2324
"""Constructor"""
2425
if self._default:
2526
for key in self._default.__dict__.keys():
@@ -33,24 +34,14 @@ class Configuration(object):
3334

3435
# Authentication Settings
3536
# dict to store API key(s)
36-
self.api_key = {}
37-
self.api_key['Auth-API-Key'] = api_key
38-
# dict to store API prefix (e.g. Bearer)
39-
self.api_key_prefix = {}
40-
# function to refresh API key if expired
41-
self.refresh_api_key_hook = None
42-
# Username for HTTP basic authentication
43-
self.username = ""
44-
# Password for HTTP basic authentication
45-
self.password = ""
37+
self.api_key = api_key
4638
{{#authMethods}}{{#isOAuth}}
4739
# access token for OAuth
4840
self.access_token = ""
4941
{{/isOAuth}}{{/authMethods}}
5042
# Logging Settings
51-
self.logger = {}
52-
self.logger["package_logger"] = logging.getLogger("{{packageName}}")
53-
self.logger["urllib3_logger"] = logging.getLogger("urllib3")
43+
self.logger = {"package_logger": logging.getLogger("{{packageName}}"),
44+
"urllib3_logger": logging.getLogger("urllib3")}
5445
# Log format
5546
self.logger_format = '%(asctime)s %(levelname)s %(message)s'
5647
# Log stream handler
@@ -95,26 +86,24 @@ class Configuration(object):
9586
cls._default = default
9687

9788
@property
98-
def logger_file(self):
89+
def logger_file(self) -> str:
9990
"""The logger file.
10091

10192
If the logger_file is None, then add stream handler and remove file
10293
handler. Otherwise, add file handler and remove stream handler.
10394

104-
:param value: The logger_file path.
105-
:type: str
95+
:return: The logger_file path.
10696
"""
10797
return self.__logger_file
10898

10999
@logger_file.setter
110-
def logger_file(self, value):
100+
def logger_file(self, value: str):
111101
"""The logger file.
112102

113103
If the logger_file is None, then add stream handler and remove file
114104
handler. Otherwise, add file handler and remove stream handler.
115105

116106
:param value: The logger_file path.
117-
:type: str
118107
"""
119108
self.__logger_file = value
120109
if self.__logger_file:
@@ -137,20 +126,18 @@ class Configuration(object):
137126
logger.removeHandler(self.logger_file_handler)
138127

139128
@property
140-
def debug(self):
129+
def debug(self) -> bool:
141130
"""Debug status
142131

143132
:param value: The debug status, True or False.
144-
:type: bool
145133
"""
146134
return self.__debug
147135

148136
@debug.setter
149-
def debug(self, value):
137+
def debug(self, value: bool):
150138
"""Debug status
151139

152140
:param value: The debug status, True or False.
153-
:type: bool
154141
"""
155142
self.__debug = value
156143
if self.__debug:
@@ -168,91 +155,47 @@ class Configuration(object):
168155
httplib.HTTPConnection.debuglevel = 0
169156

170157
@property
171-
def logger_format(self):
158+
def logger_format(self) -> str:
172159
"""The logger format.
173160

174161
The logger_formatter will be updated when sets logger_format.
175162

176-
:param value: The format string.
177-
:type: str
163+
:return: The format string.
178164
"""
179165
return self.__logger_format
180166

181167
@logger_format.setter
182-
def logger_format(self, value):
168+
def logger_format(self, value: str):
183169
"""The logger format.
184170

185171
The logger_formatter will be updated when sets logger_format.
186172

187173
:param value: The format string.
188-
:type: str
189174
"""
190175
self.__logger_format = value
191176
self.logger_formatter = logging.Formatter(self.__logger_format)
192177

193-
def get_api_key_with_prefix(self, identifier):
194-
"""Gets API key (with prefix if set).
195-
196-
:param identifier: The identifier of apiKey.
197-
:return: The token for api key authentication.
198-
"""
199-
200-
if self.refresh_api_key_hook:
201-
self.refresh_api_key_hook(self)
202-
203-
key = self.api_key.get(identifier)
204-
if key:
205-
prefix = self.api_key_prefix.get(identifier)
206-
if prefix:
207-
return "%s %s" % (prefix, key)
208-
else:
209-
return key
210-
211-
def get_basic_auth_token(self):
212-
"""Gets HTTP basic authentication header (string).
213-
214-
:return: The token for basic HTTP authentication.
215-
"""
216-
return urllib3.util.make_headers(
217-
basic_auth=self.username + ':' + self.password
218-
).get('authorization')
219-
220-
def auth_settings(self):
178+
def auth_settings(self) -> Dict[str, Dict[str, str]]:
221179
"""Gets Auth Settings dict for api client.
222180

223181
:return: The Auth Settings information dict.
224182
"""
225183
return {
226184
{{#authMethods}}
227185
{{#isApiKey}}
186+
{{#isKeyInHeader}}
228187
'{{name}}':
229188
{
230189
'type': 'api_key',
231-
'in': {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
232-
'key': '{{keyParamName}}',
233-
'value': self.get_api_key_with_prefix('{{keyParamName}}')
234-
},
235-
{{/isApiKey}}
236-
{{#isBasic}}
237-
'{{name}}':
238-
{
239-
'type': 'basic',
240-
'in': 'header',
241-
'key': 'Authorization',
242-
'value': self.get_basic_auth_token()
243-
},
244-
{{/isBasic}}{{#isOAuth}}
245-
'{{name}}':
246-
{
247-
'type': 'oauth2',
248190
'in': 'header',
249-
'key': 'Authorization',
250-
'value': 'Bearer ' + self.access_token
191+
'key': '{{keyParamName}}',
192+
'value': self.api_key
251193
},
252-
{{/isOAuth}}{{/authMethods}}
194+
{{/isKeyInHeader}}
195+
{{/isApiKey}}{{/authMethods}}
253196
}
254197

255-
def get_host(self, region):
198+
def get_host(self, region: str) -> str:
256199
return {
257200
"us": "https://api.fpjs.io",
258201
"eu": "https://eu.api.fpjs.io",

0 commit comments

Comments
 (0)