Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Created December 21, 2019 11:27
Show Gist options
  • Save wmakeev/db516a3aeada014845ba8fbca4a880bb to your computer and use it in GitHub Desktop.
Save wmakeev/db516a3aeada014845ba8fbca4a880bb to your computer and use it in GitHub Desktop.
Получение остатка товара МойСклад на Python #moysklad #python #example
import os
import requests
from urllib.parse import urlencode
def urljoin(*args):
return "/".join(map(lambda x: str(x).rstrip("/"), args))
def get_ms_filter(params_dict):
filter = []
for key, value in params_dict.items():
filter.append(f"{key}={value}")
return ";".join(filter)
def get_ms_url(
path,
endpoint="online.moysklad.ru",
api="remap",
api_version="1.2",
params={}
):
if path is None:
raise ValueError("path argument not defined")
ms_params_dict = params.copy()
ms_url = urljoin(endpoint, "api", api, api_version, path)
if len(ms_params_dict) != 0:
if "filter" in ms_params_dict:
ms_params_dict["filter"] = get_ms_filter(ms_params_dict["filter"])
ms_url = f"{ms_url}?{urlencode(ms_params_dict)}"
return "https://" + ms_url
LOGIN = os.environ["MOYSKLAD_LOGIN"]
PASSWORD = os.environ["MOYSKLAD_PASSWORD"]
auth = (LOGIN, PASSWORD)
headers = {
"Content-Type": "application/json",
# "Lognex-Pretty-Print-JSON": "true",
"charset": "UTF-8"
}
request_url = get_ms_url(
"report/stock/bystore",
params={
"limit": 10,
"filter": {
"store": get_ms_url("entity/store/eed5fae7-c949-4258-8e27-69f306d7166c"),
"stockMode": "nonEmpty"
}
}
)
print(f"request url - {request_url}\n")
resp = requests.get(request_url, auth=auth, headers=headers)
stock = resp.json()
print("stock row example - ", stock["rows"][0], "\n")
for product_stock in stock["rows"]:
product_href = product_stock["meta"]["href"]
print(f"product({product_href})")
for item in product_stock["stockByStore"]:
print(" store:", item["name"])
print(" stock:", item["stock"])
print("\n")
request url - https://online.moysklad.ru/api/remap/1.2/report/stock/bystore?limit=10&filter=store%3Dhttps%3A%2F%2Fonline.moysklad.ru%2Fapi%2Fremap%2F1.2%2Fentity%2Fstore%2Feed5fae7-c949-4258-8e27-69f306d7166c%3BstockMode%3DnonEmpty
stock row example - {'meta': {'href': 'https://online.moysklad.ru/api/remap/1.2/entity/product/77e068b5-448a-11e9-9ff4-315000053a67?expand=supplier', 'metadataHref': 'https://online.moysklad.ru/api/remap/1.2/entity/product/metadata', 'type': 'product', 'mediaType': 'application/json', 'uuidHref': 'https://online.moysklad.ru/app/#good/edit?id=77e0562b-448a-11e9-9ff4-315000053a5c'}, 'stockByStore': [{'meta': {'href': 'https://online.moysklad.ru/api/remap/1.2/entity/store/eed5fae7-c949-4258-8e27-69f306d7166c', 'metadataHref': 'https://online.moysklad.ru/api/remap/1.2/entity/store/metadata', 'type': 'store', 'mediaType': 'application/json', 'uuidHref': 'https://online.moysklad.ru/app/#warehouse/edit?id=eed5fae7-c949-4258-8e27-69f306d7166c'}, 'name': '00 Основной склад', 'stock': -1.0, 'reserve': 0.0, 'inTransit': 1.0}]}
product(https://online.moysklad.ru/api/remap/1.2/entity/product/77e068b5-448a-11e9-9ff4-315000053a67?expand=supplier)
store: 00 Основной склад
stock: -1.0
product(https://online.moysklad.ru/api/remap/1.2/entity/product/d13bf24d-d264-11e7-7a69-97110004a2d7?expand=supplier)
store: 00 Основной склад
stock: -1.0
product(https://online.moysklad.ru/api/remap/1.2/entity/product/d13ce97a-d264-11e7-7a69-97110004a2e7?expand=supplier)
store: 00 Основной склад
stock: -1.0
product(https://online.moysklad.ru/api/remap/1.2/entity/product/d13e3b8c-d264-11e7-7a69-97110004a2f7?expand=supplier)
store: 00 Основной склад
stock: -1.0
product(https://online.moysklad.ru/api/remap/1.2/entity/product/d13f6ba1-d264-11e7-7a69-97110004a307?expand=supplier)
store: 00 Основной склад
stock: -1.0
product(https://online.moysklad.ru/api/remap/1.2/entity/product/d140bb70-d264-11e7-7a69-97110004a317?expand=supplier)
store: 00 Основной склад
stock: -1.0
product(https://online.moysklad.ru/api/remap/1.2/entity/product/d143a948-d264-11e7-7a69-97110004a337?expand=supplier)
store: 00 Основной склад
stock: -1.0
product(https://online.moysklad.ru/api/remap/1.2/entity/product/2d7ffe07-fd48-11e4-7a07-673d00aa200d?expand=supplier)
store: 00 Основной склад
stock: 1.0
product(https://online.moysklad.ru/api/remap/1.2/entity/product/2dbc2b42-fd48-11e4-7a07-673d00aa21a1?expand=supplier)
store: 00 Основной склад
stock: 1.0
product(https://online.moysklad.ru/api/remap/1.2/entity/product/2dbde03a-fd48-11e4-7a07-673d00aa21ae?expand=supplier)
store: 00 Основной склад
stock: 1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment