-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
205 lines (160 loc) · 7.31 KB
/
example.py
File metadata and controls
205 lines (160 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import logging
from pprint import pprint
from floriday_supplier_client import (
TradeItemsApi,
OrganizationsApi,
)
from floriday_supplier_client.client import Floriday
from floriday_supplier_client.sync import sync_entities, EntitySynchronizer
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
def ex1_print_a_trade_item():
"""Example 1: Print a known trade item using the old API style."""
print("\n=== Example 1: Print a known trade item (old API style) ===\n")
# Old API style using ApiFactory directly
from floriday_supplier_client.api_factory import ApiFactory
factory = ApiFactory()
client = factory.get_api_client()
api_instance = TradeItemsApi(client)
api_response = api_instance.get_trade_items_summary(
trade_item_ids=["1987a15c-2c28-4ba6-89a1-3780e585b42c"]
)
pprint(api_response)
def ex2_print_a_trade_item_new_api():
"""Example 2: Print a known trade item using the new API style."""
print("\n=== Example 2: Print a known trade item (new API style) ===\n")
# New API style using the Floriday client
with Floriday() as client:
trade_items_api = client.get_api(TradeItemsApi)
api_response = trade_items_api.get_trade_items_summary(
trade_item_ids=["1987a15c-2c28-4ba6-89a1-3780e585b42c"]
)
pprint(api_response)
# Example persistence functions for different entity types
def persist_trade_item(item):
"""Example persistence function for trade items."""
print(
f"Processing trade item: {item.trade_item_id} - {getattr(item, 'trade_item_name', 'N/A')}"
)
return item.trade_item_id
def persist_organization(org):
"""Example persistence function for organizations."""
print(
f"Processing organization: {org.organization_id} - {getattr(org, 'name', 'N/A')}"
)
return org.organization_id
def persist_supply_line(supply_line):
"""Example persistence function for supply lines."""
print(
f"Processing supply line: {supply_line.supply_line_id} - {getattr(supply_line, 'status', 'N/A')}"
)
return supply_line.supply_line_id
def ex3_sync_trade_items():
"""Example 3: Synchronize trade items using the unified API."""
print("\n=== Example 3: Synchronize Trade Items ===\n")
with Floriday() as client:
# Get the trade items API
trade_items_api = client.get_api(TradeItemsApi)
# Synchronize trade items
result = sync_entities(
entity_type="trade_items",
fetch_entities_callback=trade_items_api.get_trade_items_by_sequence_number,
persist_entity_callback=persist_trade_item,
start_seq_number=0, # Start from the beginning
batch_size=10, # Process 10 items at a time
rate_limit_delay=0.5, # Wait 0.5 seconds between API calls
)
print(f"Trade Items Sync Result: {result}")
def ex4_sync_organizations_with_max_sequence():
"""Example 4: Synchronize organizations using max sequence number."""
print("\n=== Example 4: Synchronize Organizations with Max Sequence ===\n")
with Floriday() as client:
# Get the organizations API
organizations_api = client.get_api(OrganizationsApi)
# Get the maximum sequence number
max_seq = organizations_api.get_organizations_max_sequence()
print(f"Maximum sequence number for organizations: {max_seq}")
# This allows us to sync only the most recent organizations
start_seq = max_seq - 100
print(f"Starting synchronization from sequence number: {start_seq}")
# Synchronize organizations from the calculated starting point
result = sync_entities(
entity_type="organizations",
fetch_entities_callback=organizations_api.get_organizations_by_sequence_number,
persist_entity_callback=persist_organization,
start_seq_number=start_seq,
batch_size=10, # Process 10 items at a time
rate_limit_delay=0.5, # Wait 0.5 seconds between API calls
)
print(f"Organizations Sync Result: {result}")
def ex5_sync_supply_lines():
"""Example 5: Synchronize supply lines using the unified API."""
print("\n=== Example 5: Synchronize Supply Lines ===\n")
pass
def ex6_advanced_sync_with_context_manager():
"""Example 6: Advanced synchronization using EntitySynchronizer with context manager."""
print("\n=== Example 6: Advanced Synchronization with Context Manager ===\n")
with Floriday() as client:
# Get the trade items API
trade_items_api = client.get_api(TradeItemsApi)
# Create a synchronizer with advanced options
with EntitySynchronizer(
entity_type="trade_items",
fetch_entities_callback=trade_items_api.get_trade_items_by_sequence_number,
persist_entity_callback=persist_trade_item,
start_seq_number=0, # Start from the beginning
batch_size=20, # Process 20 items at a time
rate_limit_delay=0.2, # Wait 0.2 seconds between API calls
) as synchronizer:
# Configure event handlers
synchronizer.on_batch_start = lambda seq: print(
f"Starting batch at sequence {seq}"
)
synchronizer.on_batch_complete = lambda batch: print(
f"Completed batch with {len(batch)} items"
)
# Execute the sync
result = synchronizer.sync()
print(f"Advanced Sync Result: {result}")
def ex7_sync_multiple_entity_types():
"""Example 7: Synchronize multiple entity types in sequence."""
print("\n=== Example 7: Synchronize Multiple Entity Types ===\n")
with Floriday() as client:
# Synchronize trade items
trade_items_api = client.get_api(TradeItemsApi)
trade_items_result = sync_entities(
entity_type="trade_items",
fetch_entities_callback=trade_items_api.get_trade_items_by_sequence_number,
persist_entity_callback=persist_trade_item,
start_seq_number=0,
batch_size=10,
)
print(f"Trade Items Sync Result: {trade_items_result}")
# Synchronize organizations using max sequence number
organizations_api = client.get_api(OrganizationsApi)
# Get the maximum sequence number
max_seq = organizations_api.get_organizations_max_sequence()
print(f"Maximum sequence number for organizations: {max_seq}")
# Calculate a starting point
start_seq = int(max_seq - 100)
print(
f"Starting organizations synchronization from sequence number: {start_seq}"
)
organizations_result = sync_entities(
entity_type="organizations",
fetch_entities_callback=organizations_api.get_organizations_by_sequence_number,
persist_entity_callback=persist_organization,
start_seq_number=start_seq,
batch_size=10,
)
print(f"Organizations Sync Result: {organizations_result}")
if __name__ == "__main__":
# Choose which examples to run
ex1_print_a_trade_item()
ex2_print_a_trade_item_new_api()
ex3_sync_trade_items()
ex4_sync_organizations_with_max_sequence()
ex5_sync_supply_lines()
ex6_advanced_sync_with_context_manager()
ex7_sync_multiple_entity_types()