This project is a practical example of how to use the Valerdat API to synchronize purchase orders with an external system. The script connects to the API, fetches new orders since the last sync, processes them, and stores them in a local system simulated with JSON files.
The goal is to demonstrate a common workflow for system integration:
- Authentication: Obtain an access token to make secure requests.
- Check for Updates: Query the Valerdat API for new or updated orders since the last check.
- Fetch Details: Retrieve the complete information for those new orders.
- Local Sync: Compare the received data with the data that already exists in the local system and decide whether to add a new record or update an existing one.
- Persistence: Save the synchronization state to keep track of which orders have already been processed.
- Secure Authentication: Token management for API access.
- Incremental Sync: Only processes new orders from the last run, optimizing performance.
- Data Handling: Converts JSON data from the API into typed Python objects (
dataclasses) for more robust and clear handling. - Business Logic: Implements logic to add new orders or replace data if an order is updated.
- Activity Logging: All important operations (add, replace, no new orders) are logged in the
sync_valerdat.logfile. - Continuous Execution: The script runs in an infinite loop to maintain real-time synchronization.
- Python 3.7 or higher.
- The
requestslibrary for making HTTP requests.
-
Install dependencies:
pip install requests
-
Create local data files: In the project root, create the following two JSON files. These simulate your system's database.
-
orders.json: Will store the detailed information of the synchronized orders.{ "orders": [] } -
uuid.json: Will save the identifiers (UUIDs) of the already processed orders and the date of the last synchronization.{ "uuid": [], "last_update": null }Note: You can set a start date in
last_update(format"YYYY-MM-DD") for the script to start fetching orders from that day. If it isnull, it will start from the current day.
-
To start the synchronization process, simply run the main.py file:
python main.pyThe script will start running in an infinite loop, checking for new orders every 10 seconds. All relevant activity will be logged in the sync_valerdat.log file.
customer, username, password) are hardcoded in main.py for simplicity. In a production environment, it is crucial to manage them securely, for example, using environment variables.
This file is crucial for monitoring the script's behavior. Each line in the log represents an important event and follows a standard format:
YYYY-MM-DD HH:MM:SS,ms - LEVEL - Message
- INFO: [INFO] No new orders to add in local system: The check was performed, but no new orders were found.
- INFO: [+] Adding new order to local system: A new order has been detected and added to the local system (
orders.json). - INFO: [*] Replacing order in local system: An update has been detected in an existing order, and it has been replaced in the local system.
Reviewing this file is the best way to verify that the synchronization is working as expected.
main.py: The entry point. It initializes logging, creates an instance ofSyncOrders, and runs the synchronization process periodically.sync_orders.py: Contains theSyncOrdersclass, which orchestrates all the synchronization logic. It reads the local state (uuid.json), queries the API for updates, and decides how to update the local data (orders.json).api_valerdat.py: Handles all communication with the Valerdat API. It contains theApiValerdatclass that manages authentication and defines methods to get order IDs (get_orders_ids) and order details (get_order).dto.py: Defines the data transfer objects (dataclasses) likeOrderIdsResponseDtoandOrderResponseDto. This allows working with structured and typed objects instead of generic dictionaries, making the code more readable and less error-prone.