Features: 1) Add fallback logic for business_identificator lookup in order and user attributes; 2) Implement raise_for_status for FNS API requests to handle HTTP errors effectively;

Fixes: 1) Remove unused Django `settings` import;

Extra: 1) Minor reorganization of variable assignments for clarity.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-10-02 14:26:21 +03:00
parent d1ee8d6f97
commit fa97a582d3

View file

@ -4,7 +4,6 @@ from typing import Optional
import requests import requests
from constance import config from constance import config
from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.db import transaction from django.db import transaction
@ -90,7 +89,15 @@ class AmoCRM:
if type(order.attributes) is not dict: if type(order.attributes) is not dict:
raise ValueError("order.attributes must be a dict") raise ValueError("order.attributes must be a dict")
if not order.attributes.get("business_identificator"): business_identificator = (
order.attributes.get("business_identificator")
or order.attributes.get("businessIdentificator")
or order.user.attributes.get("business_identificator")
or order.user.attributes.get("businessIdentificator")
or ""
)
if not business_identificator:
return ( return (
order.user.get_full_name() order.user.get_full_name()
if order.user if order.user
@ -101,10 +108,10 @@ class AmoCRM:
) )
) )
try: try:
business_identificator = order.attributes.get("business_identificator")
r = requests.get( r = requests.get(
f"https://api-fns.ru/api/egr?req={business_identificator}&key={self.fns_api_key}", timeout=15 f"https://api-fns.ru/api/egr?req={business_identificator}&key={self.fns_api_key}", timeout=15
) )
r.raise_for_status()
body = r.json() body = r.json()
except requests.exceptions.RequestException as rex: except requests.exceptions.RequestException as rex:
logger.error(f"Unable to get company info with FNS: {rex}") logger.error(f"Unable to get company info with FNS: {rex}")