api 1.5
This commit is contained in:
@@ -3,13 +3,9 @@ import xml.etree.ElementTree as ET
|
||||
from base64 import b64encode
|
||||
from server.backend.schemas.pydantic import settings
|
||||
import pandas as pd
|
||||
from server.backend.api.session import get_session
|
||||
|
||||
auth_str = f"{settings.USERNAME}:{settings.PASSWORD}"
|
||||
b64_auth_str = b64encode(auth_str.encode("utf-8")).decode("utf-8")
|
||||
|
||||
session = requests.Session()
|
||||
session.headers.update({
|
||||
"Authorization": f"Basic {b64_auth_str}",
|
||||
session = get_session({
|
||||
"Accept": "application/xml",
|
||||
})
|
||||
|
||||
|
||||
@@ -3,22 +3,19 @@ import xml.etree.ElementTree as ET
|
||||
from base64 import b64encode
|
||||
from server.backend.schemas.pydantic import settings
|
||||
import pandas as pd
|
||||
from server.backend.api.session import get_session
|
||||
|
||||
auth_str = f"{settings.USERNAME}:{settings.PASSWORD}"
|
||||
b64_auth_str = b64encode(auth_str.encode("utf-8")).decode("utf-8")
|
||||
|
||||
session = requests.Session()
|
||||
session.headers.update({
|
||||
"Authorization": f"Basic {b64_auth_str}",
|
||||
session = get_session({
|
||||
"Accept": "application/xml",
|
||||
})
|
||||
|
||||
def fetch_contragents():
|
||||
|
||||
def fetch_companies():
|
||||
response = session.get(settings.URL_CONTRACTORS, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response.text
|
||||
|
||||
def parse_contragents(xml: str):
|
||||
def parse_companies(xml: str):
|
||||
try:
|
||||
NS = {
|
||||
"atom": "http://www.w3.org/2005/Atom",
|
||||
@@ -45,6 +42,6 @@ def parse_contragents(xml: str):
|
||||
except ET.ParseError:
|
||||
raise
|
||||
def contractor():
|
||||
xml_data = fetch_contragents()
|
||||
root = parse_contragents(xml_data)
|
||||
xml_data = fetch_companies()
|
||||
root = parse_companies(xml_data)
|
||||
root.to_excel("./excel_files/contractors.xlsx")
|
||||
@@ -4,21 +4,18 @@ from base64 import b64encode
|
||||
from server.backend.schemas.pydantic import settings
|
||||
from functools import lru_cache
|
||||
import pandas as pd
|
||||
auth_str = f"{settings.USERNAME}:{settings.PASSWORD}"
|
||||
b64_auth_str = b64encode(auth_str.encode("utf-8")).decode("utf-8")
|
||||
from server.backend.api.session import get_session
|
||||
|
||||
session = requests.Session()
|
||||
session.headers.update({
|
||||
"Authorization": f"Basic {b64_auth_str}",
|
||||
session = get_session({
|
||||
"Accept": "application/xml",
|
||||
})
|
||||
|
||||
def fetch_contragents():
|
||||
def fetch_nomenclature():
|
||||
response = session.get(settings.URL_NOMENCLATURE, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response.text
|
||||
|
||||
def parse_contragents(xml: str):
|
||||
def parse_nomenclature(xml: str):
|
||||
try:
|
||||
NS = {
|
||||
"atom": "http://www.w3.org/2005/Atom",
|
||||
@@ -45,8 +42,8 @@ def parse_contragents(xml: str):
|
||||
raise
|
||||
@lru_cache(maxsize=1)
|
||||
def nomenclature(flag=False):
|
||||
xml_data = fetch_contragents()
|
||||
root = parse_contragents(xml_data)
|
||||
xml_data = fetch_nomenclature()
|
||||
root = parse_nomenclature(xml_data)
|
||||
if flag:
|
||||
root.to_excel("./excel_files/nomenclature.xlsx")
|
||||
return root
|
||||
|
||||
@@ -1,20 +1,30 @@
|
||||
import requests
|
||||
import json
|
||||
from base64 import b64encode
|
||||
from server.backend.schemas.pydantic import settings
|
||||
import pandas as pd
|
||||
auth_str = f"{settings.USERNAME}:{settings.PASSWORD}"
|
||||
b64_auth_str = b64encode(auth_str.encode("utf-8")).decode("utf-8")
|
||||
from server.backend.schemas.pydantic import settings, CreateDocumentParams
|
||||
from server.backend.api.session import get_session
|
||||
|
||||
session = requests.Session()
|
||||
session.headers.update({
|
||||
"Authorization": f"Basic {b64_auth_str}",
|
||||
session = get_session({
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
"Accept": "application/json",
|
||||
})
|
||||
|
||||
def fetch_contragents():
|
||||
response = session.post(settings.URL_REPORT, data=json.dumps(data))
|
||||
response.raise_for_status()
|
||||
return response.status_code
|
||||
def create_document(params: CreateDocumentParams) -> int:
|
||||
data = {
|
||||
"Дата": params.DATE,
|
||||
#"ВидОперации": params.OPERATION,
|
||||
"Организация_Key": settings.COMPANY,
|
||||
"Контрагент_Key": params.CONTRACTOR,
|
||||
"Склад_Key": settings.STORE,
|
||||
"ДоговорКонтрагента_Key": params.CONTRACT,
|
||||
"ДокументБезНДС": "false",
|
||||
"СуммаВключаетНДС": "true",
|
||||
"СчетУчетаРасчетовЗаПосредническиеУслуги_Key": params.ACCOUNT_INTERMEDIARY_SERVICES,
|
||||
"СчетУчетаРасчетовПоАвансамПолученным_Key": params.ACCOUNT_ADVANCES_RECEIVED,
|
||||
"СчетУчетаРасчетовПоАвансамВыданным_Key": params.ACCOUNT_ADVANCES_ISSUED,
|
||||
"СчетУчетаРасчетовСКонтрагентом_Key": params.ACCOUNT_WITH_COUNTERPARTY,
|
||||
}
|
||||
|
||||
response = session.post(settings.URL_REALISATION, json=data)
|
||||
response.raise_for_status()
|
||||
return response.status_code
|
||||
@@ -1,20 +1,30 @@
|
||||
import requests
|
||||
import json
|
||||
from base64 import b64encode
|
||||
from server.backend.schemas.pydantic import settings
|
||||
import pandas as pd
|
||||
auth_str = f"{settings.USERNAME}:{settings.PASSWORD}"
|
||||
b64_auth_str = b64encode(auth_str.encode("utf-8")).decode("utf-8")
|
||||
from server.backend.schemas.pydantic import settings, CreateDocumentParams
|
||||
from server.backend.api.session import get_session
|
||||
|
||||
session = requests.Session()
|
||||
session.headers.update({
|
||||
"Authorization": f"Basic {b64_auth_str}",
|
||||
session = get_session({
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
"Accept": "application/json",
|
||||
})
|
||||
|
||||
def fetch_contragents():
|
||||
response = session.post(settings.URL_REPORT, data=json.dumps(data))
|
||||
response.raise_for_status()
|
||||
return response.status_code
|
||||
def create_document(params: CreateDocumentParams) -> int:
|
||||
data = {
|
||||
"Дата": params.DATE,
|
||||
"ВидОперации": params.OPERATION,
|
||||
"Организация_Key": settings.COMPANY,
|
||||
"Контрагент_Key": params.CONTRACTOR,
|
||||
"Склад_Key": settings.STORE,
|
||||
"ДоговорКонтрагента_Key": params.CONTRACT,
|
||||
"ДокументБезНДС": "false",
|
||||
"СуммаВключаетНДС": "true",
|
||||
"СчетУчетаРасчетовЗаПосредническиеУслуги_Key": params.ACCOUNT_INTERMEDIARY_SERVICES,
|
||||
"СчетУчетаРасчетовПоАвансамПолученным_Key": params.ACCOUNT_ADVANCES_RECEIVED,
|
||||
"СчетУчетаРасчетовПоАвансамВыданным_Key": params.ACCOUNT_ADVANCES_ISSUED,
|
||||
"СчетУчетаРасчетовСКонтрагентом_Key": params.ACCOUNT_WITH_COUNTERPARTY,
|
||||
}
|
||||
|
||||
response = session.post(settings.URL_REPORT, json=data)
|
||||
response.raise_for_status()
|
||||
return response.status_code
|
||||
20
server/backend/api/session.py
Normal file
20
server/backend/api/session.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import requests
|
||||
from base64 import b64encode
|
||||
from server.backend.schemas.pydantic import settings
|
||||
def get_session(extra_headers=None):
|
||||
"""
|
||||
Создаёт и возвращает requests.Session с базовой авторизацией.
|
||||
Можно менять username, password и добавлять дополнительные заголовки.
|
||||
"""
|
||||
session = requests.Session()
|
||||
auth_str = f"{settings.USERNAME}:{settings.PASSWORD}"
|
||||
b64_auth_str = b64encode(auth_str.encode("utf-8")).decode("utf-8")
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Basic {b64_auth_str}",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
if extra_headers:
|
||||
headers.update(extra_headers)
|
||||
session.headers.update(headers)
|
||||
return session
|
||||
@@ -3,22 +3,19 @@ import xml.etree.ElementTree as ET
|
||||
from base64 import b64encode
|
||||
from server.backend.schemas.pydantic import settings
|
||||
import pandas as pd
|
||||
from server.backend.api.session import get_session
|
||||
|
||||
auth_str = f"{settings.USERNAME}:{settings.PASSWORD}"
|
||||
b64_auth_str = b64encode(auth_str.encode("utf-8")).decode("utf-8")
|
||||
|
||||
session = requests.Session()
|
||||
session.headers.update({
|
||||
"Authorization": f"Basic {b64_auth_str}",
|
||||
session = get_session({
|
||||
"Accept": "application/xml",
|
||||
})
|
||||
|
||||
def fetch_contragents():
|
||||
|
||||
def fetch_stores():
|
||||
response = session.get(settings.URL_STORAGES, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response.text
|
||||
|
||||
def parse_contragents(xml: str):
|
||||
def parse_stores(xml: str):
|
||||
try:
|
||||
NS = {
|
||||
"atom": "http://www.w3.org/2005/Atom",
|
||||
@@ -41,6 +38,6 @@ def parse_contragents(xml: str):
|
||||
except ET.ParseError:
|
||||
raise
|
||||
def storages():
|
||||
xml_data = fetch_contragents()
|
||||
root = parse_contragents(xml_data)
|
||||
xml_data = fetch_stores()
|
||||
root = parse_stores(xml_data)
|
||||
root.to_excel("./excel_files/storages.xlsx")
|
||||
Reference in New Issue
Block a user