Skip to content

Commit

Permalink
Refactor sync method
Browse files Browse the repository at this point in the history
  • Loading branch information
serra committed Oct 9, 2024
1 parent e9edac4 commit 3f4d39f
Showing 1 changed file with 54 additions and 34 deletions.
88 changes: 54 additions & 34 deletions src/floridayvine/floriday/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}


def get_access_token():
def _get_access_token():
response = requests.request("POST", AUTH_URL, headers=headers, data=payload)
response.raise_for_status()
return response.json().get("access_token")
Expand All @@ -41,7 +41,7 @@ def get_access_token():


def get_organizations():
access_token = get_access_token()
access_token = _get_access_token()
url = f"{BASE_URL}/auth/key"

headers = {
Expand All @@ -55,24 +55,49 @@ def get_organizations():
return response.json()


def sync_organizations(start_seq_number=0, limit_result=5):
api = OrganizationsApi(_clt)
def sync_entities(
api,
entity_type,
get_max_sequence,
get_by_sequence,
persist_entity,
start_seq_number=0,
limit_result=5,
):
my_sequence = start_seq_number
max_seq_nr = api.get_organizations_max_sequence()
max_seq_nr = get_max_sequence()

print(f"Syncing organizations from {my_sequence} to {max_seq_nr} ...")
print(f"Syncing {entity_type} from {my_sequence} to {max_seq_nr} ...")

while my_sequence < max_seq_nr:
orgs_sync_result = api.get_organizations_by_sequence_number(
sync_result = get_by_sequence(
sequence_number=my_sequence, limit_result=limit_result
)
max_seq_nr = orgs_sync_result.maximum_sequence_number
for org in orgs_sync_result.results:
print(f"Seq nr {org.sequence_number}: Persisting {org.name} ...")
persist("organizations", org.organization_id, org.to_dict())
my_sequence = orgs_sync_result.maximum_sequence_number
time.sleep(0.5)
print("Done syncing organizations")
max_seq_nr = sync_result.maximum_sequence_number
for entity in sync_result.results:
print(f"Seq nr {entity.sequence_number}: Persisting {persist_entity(entity)} ...")
my_sequence = sync_result.maximum_sequence_number
time.sleep(0.1)

print(f"Done syncing {entity_type}")


def sync_organizations(start_seq_number=0, limit_result=50):
api = OrganizationsApi(_clt)

def persist_org(org):
persist("organizations", org.organization_id, org.to_dict())
return org.name

sync_entities(
api,
"organizations",
api.get_organizations_max_sequence,
api.get_organizations_by_sequence_number,
persist_org,
start_seq_number,
limit_result,
)


def get_trade_items():
Expand All @@ -87,24 +112,19 @@ def get_direct_sales():
return items


def sync_trade_items(start_seq_number=0, limit_result=5):
def sync_trade_items(start_seq_number=0, limit_result=50):
api = TradeItemsApi(_clt)
my_sequence = start_seq_number
max_seq_nr = api.get_trade_items_max_sequence()

print(f"Syncing trade items from {my_sequence} to {max_seq_nr} ...")

while my_sequence < max_seq_nr:
trade_items_sync_result = api.get_trade_items_by_sequence_number(
sequence_number=my_sequence, limit_result=limit_result
)
max_seq_nr = trade_items_sync_result.maximum_sequence_number
for item in trade_items_sync_result.results:
print(
f"Seq nr {item.sequence_number}: Persisting {item.trade_item_name} ..."
)
persist("trade_items", item.trade_item_id, item.to_dict())
my_sequence = trade_items_sync_result.maximum_sequence_number
time.sleep(0.5)

print("Done syncing trade items")

def persist_item(item):
persist("trade_items", item.trade_item_id, item.to_dict())
return item.trade_item_name

sync_entities(
api,
"trade items",
api.get_trade_items_max_sequence,
api.get_trade_items_by_sequence_number,
persist_item,
start_seq_number,
limit_result,
)

0 comments on commit 3f4d39f

Please sign in to comment.