camisatoshi-wordpress-reports/camisatoshi_wordpress_reports/controllers.py

50 lines
1.3 KiB
Python
Raw Normal View History

from pathlib import Path
2023-08-03 09:29:11 +02:00
import datetime
from typing import List, Dict
from dotenv import dotenv_values
from woocommerce import API
API_CONFIG = dotenv_values(
dotenv_path=Path.home() / Path(".camisatoshi-wordpress-reports/.env")
)
2023-08-02 20:09:28 +02:00
WC_API = API(
url=API_CONFIG["URL"],
consumer_key=API_CONFIG["CONSUMER_KEY"],
consumer_secret=API_CONFIG["CONSUMER_SECRET"],
version=API_CONFIG["VERSION"],
)
2023-08-02 19:56:59 +02:00
2023-08-02 19:49:36 +02:00
def check_health():
print(f"Connecting to the configured woocomerce at {API_CONFIG['URL']}")
2023-08-02 19:56:59 +02:00
try:
2023-08-02 20:09:28 +02:00
api_reported_version = WC_API.get("").json()["namespace"]
2023-08-02 19:56:59 +02:00
except:
raise ConnectionError(
"There was an issue connecting to the woocomerce API."
)
print(f"Informed version of the API: {API_CONFIG['VERSION']}")
print(f"Version reported by the API itself: {api_reported_version}")
print("Connection successful. The API is reachable.")
2023-08-02 20:09:28 +02:00
2023-08-03 09:29:11 +02:00
def generate_um_report(start_date: str, end_date: str):
orders_in_date_range = WC_API.get(
endpoint="orders",
params={
"after": datetime.datetime.strptime(
start_date, "%Y-%m-%d"
).isoformat(),
"before": datetime.datetime.strptime(
end_date, "%Y-%m-%d"
).isoformat(),
"per_page": 100,
"status": "processing,completed",
},
2023-08-02 20:09:28 +02:00
)