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:
parent
d1ee8d6f97
commit
fa97a582d3
1 changed files with 10 additions and 3 deletions
|
|
@ -4,7 +4,6 @@ from typing import Optional
|
|||
|
||||
import requests
|
||||
from constance import config
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.db import transaction
|
||||
|
||||
|
|
@ -90,7 +89,15 @@ class AmoCRM:
|
|||
if type(order.attributes) is not 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 (
|
||||
order.user.get_full_name()
|
||||
if order.user
|
||||
|
|
@ -101,10 +108,10 @@ class AmoCRM:
|
|||
)
|
||||
)
|
||||
try:
|
||||
business_identificator = order.attributes.get("business_identificator")
|
||||
r = requests.get(
|
||||
f"https://api-fns.ru/api/egr?req={business_identificator}&key={self.fns_api_key}", timeout=15
|
||||
)
|
||||
r.raise_for_status()
|
||||
body = r.json()
|
||||
except requests.exceptions.RequestException as rex:
|
||||
logger.error(f"Unable to get company info with FNS: {rex}")
|
||||
|
|
|
|||
Loading…
Reference in a new issue