Skip to content

Commit 87a160e

Browse files
committedFeb 20, 2020
cleanup
1 parent bfd839f commit 87a160e

17 files changed

+147
-137
lines changed
 

‎README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
<a href='#travis'>
3333
<img src='https://img.shields.io/travis/com/rolzwy7/RegonAPI.svg' alt='Build status' />
3434
</a>
35-
35+
<a href='#pypi_downloads'>
36+
<img src='https://img.shields.io/pypi/dm/RegonAPI' alt='PyPi downloads' />
37+
</a>
3638
</p>
3739

3840
<p align="center">

‎RegonAPI/consts/__init__.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from RegonAPI.consts.voivodeships import VOIVODESHIPS
22
from RegonAPI.consts.api_codes import API_CODES
33

4-
from RegonAPI.consts.bir_version_maps import BIR_SETTINGS
5-
from RegonAPI.consts.bir_version_maps import BIR_VERSIONS
6-
from RegonAPI.consts.bir_version_maps import OPERATIONS
7-
from RegonAPI.consts.bir_version_maps import DANE_POBIERZ_PELNY_RAPORT_REPORT_NAMES
8-
from RegonAPI.consts.bir_version_maps import REPORTS
4+
from RegonAPI.consts.bir_version_maps import *
5+
6+
from RegonAPI.consts.dev_environment import API_KEY_TEST_ENV
7+
from RegonAPI.consts.dev_environment import WARNINGS as DEV_ENV_WARNINGS

‎RegonAPI/consts/bir_version_maps.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"PROD": {
88
"SERVICE_URL": "https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc",
9-
# Yes, it's the same WSDL address for test and production.
9+
# Yes, it's the same WSDL address for test and production in version 1.0
1010
"WSDL": "https://wyszukiwarkaregontest.stat.gov.pl/wsBIR/wsdl/UslugaBIRzewnPubl.xsd"
1111
}
1212
},
@@ -17,6 +17,7 @@
1717
},
1818
"PROD": {
1919
"SERVICE_URL": "https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc",
20+
# Sneaky correction in version 1.1
2021
"WSDL": "https://wyszukiwarkaregon.stat.gov.pl/wsBIR/wsdl/UslugaBIRzewnPubl-ver11-prod.wsdl"
2122
}
2223
}

‎RegonAPI/consts/dev_environment.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Test API key
2+
API_KEY_TEST_ENV = "abcde12345abcde12345"
3+
4+
# Warnings
5+
WARNINGS = {
6+
"WARN_IS_NOT_PRODUCTION": {
7+
"eng": """
8+
\n\r\tYou are using TEST environment.
9+
\r\tSet `is_production=True` while constructing
10+
\r\t`RegonAPI` instance to switch to production environment.
11+
12+
\r\tUsing test API key: '{API_KEY_TEST_ENV}'
13+
\r\tKey provided to `authenticate` method will be overridden.\n
14+
""".format(API_KEY_TEST_ENV=API_KEY_TEST_ENV),
15+
"pl": """
16+
\n\r\tUżywasz środowiska TESTOWEGO.
17+
\r\tUstaw `is_production=True` podczas konstrukcji
18+
\r\tinstancji klasy `RegonAPI` aby przejść do środowiska produkcyjnego.
19+
20+
\r\tUżywany jest klucz testowy: '{API_KEY_TEST_ENV}'
21+
\r\tKlucz przekazany do metody `authenticate` zostanie nadpisany.\n
22+
""".format(API_KEY_TEST_ENV=API_KEY_TEST_ENV),
23+
}
24+
}

‎RegonAPI/exceptions.py

-72
This file was deleted.

‎RegonAPI/exceptions/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from RegonAPI.exceptions.api import *
2+
from RegonAPI.exceptions.converters import *

‎RegonAPI/exceptions/api.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class ApiError(BaseException):
2+
def __str__(self):
3+
return repr(self.data)
4+
5+
6+
class ApiAuthenticationError(ApiError):
7+
8+
def __init__(self, data):
9+
self.data = 'Authentication failed with key: "{data}"'.format(
10+
data=data)
11+
12+
13+
class ApiCodeTranslationError(ApiError):
14+
15+
def __init__(self, data):
16+
self.data = 'Can\'t translate: {data}'.format(
17+
data=data)
18+
19+
20+
class ApiUnknownReportNameError(ApiError):
21+
22+
def __init__(self, data):
23+
self.data = 'Invailid report name: {data}'.format(
24+
data=data)
25+
26+
27+
class ApiInvalidBIRVersionProvided(ApiError): # TODO: test case
28+
29+
def __init__(self, data, choices):
30+
self.data = 'Invailid BIR version: {data} not in {choices}'.format(
31+
data=data, choices=choices
32+
)

‎RegonAPI/exceptions/converters.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class ConvertionTypeError(BaseException):
2+
3+
def __init__(self, data, what=''):
4+
self.data = 'Can\'t convert {what}: {data}'.format(
5+
data=data, what=what)
6+
7+
def __str__(self):
8+
return repr(self.data)
9+
10+
11+
class ConvertionLengthError(BaseException):
12+
13+
def __init__(self, data, what=''):
14+
self.data = 'Invalid {what} length: {data}'.format(
15+
data=data, what=what)
16+
17+
def __str__(self):
18+
return repr(self.data)
19+
20+
21+
class RegonConvertionError(ConvertionTypeError):
22+
23+
def __init__(self, data):
24+
super(RegonConvertionError, self).__init__(
25+
data=data, what='regon'
26+
)

‎RegonAPI/plugins/__init__.py

Whitespace-only changes.

‎RegonAPI/plugins/detailed.py

-8
This file was deleted.

‎RegonAPI/plugins/fetch_all.py

-4
This file was deleted.

‎RegonAPI/regon_api.py

+34-27
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,41 @@
22
RegonAPI main client class
33
"""
44

5+
# try:
6+
# # from urllib.parse import urlparse
7+
# except ImportError:
8+
# print('')
9+
# quit()
10+
511
import logging
612

713
from requests import Session
814
from zeep.transports import Transport
915
from zeep import Client
1016

1117
from RegonAPI.api_codes import t
12-
from RegonAPI.exceptions import ApiInvalidBIRVersionProvided
13-
from RegonAPI.exceptions import ApiAuthenticationError
18+
from RegonAPI.exceptions import (
19+
ApiInvalidBIRVersionProvided,
20+
ApiAuthenticationError
21+
)
1422
from RegonAPI.operations import RegonAPIOperations
15-
from RegonAPI.settings import logging_level
16-
from RegonAPI.settings import logging_file_handler_level
17-
from RegonAPI.settings import logging_stream_handler_level
18-
from RegonAPI.settings import BIR_VERSIONS
19-
from RegonAPI.settings import BIR_SETTINGS
20-
from RegonAPI.settings import REPORTS
23+
from RegonAPI.settings import (
24+
lang,
25+
BIR_VERSIONS,
26+
BIR_SETTINGS,
27+
REPORTS,
28+
DEV_ENV_WARNINGS,
29+
API_KEY_TEST_ENV,
30+
LOGGING_FORMAT
31+
)
2132

2233
# logger configuration
34+
ROOT_LOGGING_LEVEL = logging.ERROR
35+
LOCAL_LOGGING_LEVEL = logging.WARNING
36+
logging.basicConfig(format=LOGGING_FORMAT)
37+
logging.root.setLevel(ROOT_LOGGING_LEVEL)
2338
logger = logging.getLogger(__name__)
24-
logger.setLevel(logging_level)
25-
26-
# fh = logging.FileHandler('debug.log')
27-
# fh.setLevel(logging_file_handler_level)
28-
29-
sh = logging.StreamHandler()
30-
sh.setLevel(logging_stream_handler_level)
31-
32-
formatter = logging.Formatter(
33-
'[%(levelname)s][%(asctime)s] %(name)s %(funcName)s - %(message)s')
34-
# fh.setFormatter(formatter)
35-
sh.setFormatter(formatter)
36-
37-
# logger.addHandler(fh)
38-
logger.addHandler(sh)
39-
# logger configuration End
40-
39+
logger.setLevel(LOCAL_LOGGING_LEVEL)
4140

4241
# TODO: New class desc
4342
class RegonAPI(RegonAPIOperations):
@@ -70,7 +69,7 @@ class RegonAPI(RegonAPIOperations):
7069

7170
def __init__(
7271
self,
73-
bir_version="bir1",
72+
bir_version="bir1.1",
7473
is_production=False,
7574
service_namespace='{http://tempuri.org/}e3'
7675
):
@@ -84,6 +83,9 @@ def __init__(
8483

8584
# Set API environment
8685
api_env = "PROD" if is_production else "TEST"
86+
if not is_production:
87+
logger.warning(DEV_ENV_WARNINGS["WARN_IS_NOT_PRODUCTION"][lang])
88+
8789

8890
# Set WSDL & SERVICE_URL based on BIR version
8991
self.wsdl = BIR_SETTINGS[bir_version][api_env]["WSDL"]
@@ -95,6 +97,7 @@ def __init__(
9597
self.service = None
9698
self.key = None
9799
self.sid = None
100+
self.is_production = is_production
98101
self.service_namespace = service_namespace
99102
self._create_service()
100103

@@ -127,6 +130,9 @@ def authenticate(self, key, verify=True):
127130
ApiAuthenticationError
128131
If authentication was a failure
129132
"""
133+
if not self.is_production:
134+
key = API_KEY_TEST_ENV
135+
130136
logger.debug('key=%s verify=%s' % (key, verify))
131137
sid = self.service.Zaloguj(key)
132138
session = Session()
@@ -138,7 +144,7 @@ def authenticate(self, key, verify=True):
138144
self._create_service()
139145
if verify:
140146
if not self._check_session():
141-
logger.warning('Authentication failed')
147+
logger.error('Authentication failed')
142148
raise ApiAuthenticationError(key)
143149
else:
144150
logger.debug('Authenticated successfully')
@@ -229,6 +235,7 @@ def _create_service(self):
229235
self.service = self.client.create_service(
230236
self.service_namespace, self.service_url)
231237

238+
232239
def _check_session(self):
233240
"""Checks Regon API for confirmation of successfull authentication
234241

‎RegonAPI/settings.py

-19
This file was deleted.

‎RegonAPI/settings/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from RegonAPI.settings.settings import *

‎RegonAPI/settings/settings.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
import logging
3+
4+
from RegonAPI.consts import *
5+
6+
# Language - settings
7+
lang = 'eng'
8+
9+
# Logger - settings
10+
available_languages = ["pl", "eng"]
11+
12+
LOGGING_FORMAT = "\r%(asctime)s, %(levelname)-8s [`%(funcName)s` %(filename)s:%(lineno)d] %(message)s"

‎main.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from RegonAPI import RegonAPI
2+
3+
api = RegonAPI(bir_version="bir1.1", is_production=False)
4+
try:
5+
api.authenticate(key="")
6+
except Exception as e:
7+
raise

‎notes.txt ‎misc/notes.txt

File renamed without changes.

0 commit comments

Comments
 (0)
Please sign in to comment.