67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
import json
|
|
|
|
import get_sq_env
|
|
|
|
SQUARE_TOKEN = get_sq_env.SQUARE_TOKEN
|
|
LEGO_CAT_ID = get_sq_env.LEGO_CAT_ID
|
|
ENVIRONMENT = get_sq_env.environment
|
|
|
|
from square import Square
|
|
from square.environment import SquareEnvironment
|
|
|
|
|
|
def get_square_client():
|
|
"""Initialize and return a Square client."""
|
|
if not SQUARE_TOKEN:
|
|
raise ValueError("SQUARE_TOKEN environment variable is not set.")
|
|
square = Square(token=SQUARE_TOKEN, environment=ENVIRONMENT) # type: ignore
|
|
return square
|
|
|
|
|
|
client = get_square_client()
|
|
|
|
|
|
def get_itm(item_id: str):
|
|
"""Fetch an item from Square by its ID."""
|
|
result = client.catalog.search_items(text_filter=item_id, limit=1)
|
|
result_json = json.loads(result.model_dump_json())
|
|
ids = {
|
|
"item_id": result_json["items"][0]["id"],
|
|
"vari_id": result_json["items"][0]["item_data"]["variations"][0][
|
|
"item_variation_data"
|
|
]["item_id"],
|
|
}
|
|
item = {
|
|
"set_name": result_json["items"][0]["item_data"]["name"],
|
|
"set_price": result_json["items"][0]["item_data"]["variations"][0][
|
|
"item_variation_data"
|
|
]["price_money"],
|
|
"set_barcode": result_json["items"][0]["item_data"]["variations"][0][
|
|
"item_variation_data"
|
|
]["sku"],
|
|
}
|
|
return [ids, item]
|
|
|
|
|
|
def cat2id(cat_id: str):
|
|
"""Return a list of item IDs within the specified category ID."""
|
|
result = client.catalog.search(
|
|
object_types=["ITEM"],
|
|
query={
|
|
"exact_query": {
|
|
"attribute_name": "category_id",
|
|
"attribute_value": cat_id,
|
|
}
|
|
},
|
|
)
|
|
result_json = json.loads(result.model_dump_json())
|
|
item_ids = [obj["id"] for obj in result_json.get("objects", [])]
|
|
return item_ids
|
|
|
|
|
|
def test() -> None:
|
|
# all_lego_sets = cat2id(SQUARE_LEGO_CAT_ID) # type: ignore
|
|
# result = get_itm("71433")
|
|
# print(result)
|
|
pass
|