diff --git a/kontur_focus/focus.py b/kontur_focus/focus.py index a60386a..39ce27d 100644 --- a/kontur_focus/focus.py +++ b/kontur_focus/focus.py @@ -41,21 +41,25 @@ class Focus(Request): :rtype: str """ response = self.get('/excerpt') - current_datetime = datetime.now().strftime('%d-%m-%Y_%H-%M') - filename = f'Выписка_{self.inn}_{current_datetime}.pdf' + + if response.status_code != 404: + current_datetime = datetime.now().strftime('%d-%m-%Y_%H-%M') + filename = f'Выписка_{self.inn}_{current_datetime}.pdf' - if not path: - file_path = os.path.join(self._basedir, filename) + if not path: + file_path = os.path.join(self._basedir, filename) + else: + file_path = os.path.join(path, filename) + + try: + with open(file_path, mode='wb') as file: + file.write(response.content) + + return {'success': True, 'filename': filename, 'path': file_path} + except Exception as e: + return {'success': False, 'message': e} else: - file_path = os.path.join(path, filename) - - try: - with open(file_path, mode='wb') as file: - file.write(response.content) - - return {'success': True, 'filename': filename, 'path': file_path} - except Exception as e: - return {'success': False, 'message': e} + return {'success': False, 'message': 'Not found'} def express_report(self, pdf: bool = True, path: str = None) -> dict: """Экспресс-отчет по контрагенту diff --git a/kontur_focus/req.py b/kontur_focus/req.py index 16fa146..53e0f4c 100644 --- a/kontur_focus/req.py +++ b/kontur_focus/req.py @@ -36,8 +36,8 @@ class Request: try: response = requests.get(url=full_url, params=payload) - if response.headers['Content-Type'] == 'application/json' or \ - response.headers['Content-Type'] == 'application/json; charset=utf-8': + if ('Content-Type' in response.headers and response.headers['Content-Type'] == 'application/json') or \ + ('Content-Type' in response.headers and response.headers['Content-Type'] == 'application/json; charset=utf-8'): return response.json() else: return response