-
Notifications
You must be signed in to change notification settings - Fork 0
/
discontinue_items.py
354 lines (297 loc) · 10.7 KB
/
discontinue_items.py
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import argparse
import csv
import getpass
import logging
import math
import sys
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass
from typing import Dict, List, Optional
try:
import requests
except ImportError:
print("The 'requests' library is not installed.\n")
print("To install it, run the following command:\n")
print(" pip3 install requests\n")
print(
"If you are using a virtual environment, make sure it is activated before running the command."
)
sys.exit(1)
# Constants
BASE_URL = "https://api.trackandtrace.tools"
PAGE_SIZE = 500 # Adjustable page size based on API limits
MAX_RETRIES = 5 # Maximum number of retry attempts
RETRY_DELAY = 5 # Delay in seconds between retries
MAX_WORKERS = 5 # Number of threads to use for parallel requests
TIMEOUT_S = 20
SUBMIT = "true"
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
@dataclass
class Credentials:
hostname: str
username: str
password: str
otp: Optional[str] = None
def parse_arguments() -> argparse.Namespace:
"""
Parse command line arguments.
"""
parser = argparse.ArgumentParser(
description="Discontinue items from CSV"
"This script authenticates with the provided credentials and discontinues a list of items provided in a csv."
)
parser.add_argument(
"--hostname",
required=True,
help="The hostname of the Track and Trace Tools API (e.g., mo.metrc.com). Example: --hostname=mo.metrc.com",
)
parser.add_argument(
"--username",
required=True,
help="Username for authentication with the Track and Trace Tools API. Example: [email protected]",
)
parser.add_argument(
"--csv_path",
default="discontinue_items.csv",
help="Path to the input CSV file containing items to discontinue. Defaults to 'discontinue_items.csv'. Example: --csv_path=path/to/file.csv",
)
return parser.parse_args()
def make_request_with_retries(
*,
session: requests.Session,
url: str,
headers: Dict,
params: Dict,
max_retries: int = MAX_RETRIES,
retry_delay: int = RETRY_DELAY,
) -> Dict:
"""
Makes an HTTP GET request with a retry strategy.
"""
retries = 0
while retries < max_retries:
try:
response = session.get(
url=url, headers=headers, params=params, timeout=TIMEOUT_S
)
response.raise_for_status()
return response.json()
except requests.exceptions.Timeout:
logger.error("Request timed out. Retrying...")
except requests.exceptions.HTTPError as e:
logger.error(f"HTTP error: {e.response.status_code}, {e.response.text}")
except requests.exceptions.RequestException as e:
logger.error(f"An error occurred during the request: {str(e)}")
retries += 1
logger.info(f"Retrying ({retries}/{max_retries}) in {retry_delay} seconds...")
time.sleep(retry_delay)
raise Exception(f"Failed to complete request after {max_retries} retries.")
def fetch_page(
*,
session: requests.Session,
page: int,
headers: Dict,
license_number: str,
) -> Dict:
"""
Fetch a single page of items.
"""
logger.info(f"Fetching page {page}...")
url = f"{BASE_URL}/v2/items"
params = {
"licenseNumber": license_number,
"page": page,
"pageSize": PAGE_SIZE,
}
return make_request_with_retries(
session=session, url=url, headers=headers, params=params
)
def load_items(
*,
session: requests.Session,
headers: Dict,
license_number: str,
):
"""
Generate a CSV report of inactive packages for a given license number.
"""
page = 1
total_loaded = 0
items = []
count_response = make_request_with_retries(
session=session,
url=f"{BASE_URL}/v2/items",
headers=headers,
params={
"licenseNumber": license_number,
"page": 1,
"pageSize": 5,
},
)
max_pages = math.ceil(int(count_response["total"]) / PAGE_SIZE)
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
future_to_page = {
executor.submit(
fetch_page,
session=session,
page=page,
headers=headers,
license_number=license_number,
): page
for page in range(1, max_pages + 1)
}
for future in as_completed(future_to_page):
page = future_to_page[future]
try:
response_payload = future.result()
if response_payload and response_payload.get("data"):
items.extend(response_payload["data"])
total_loaded += len(response_payload["data"])
logger.info(
f"Loaded {len(response_payload['data'])} items from page {page}, total loaded so far: {total_loaded}"
)
else:
logger.info(f"No data returned for page {page}.")
except Exception as e:
logger.error(f"Failed to fetch page {page}: {str(e)}")
return items
def obtain_access_token(
*, session: requests.Session, credentials: Credentials
) -> Optional[str]:
"""
Obtain access token using provided credentials.
"""
logger.info("Obtaining access token...")
url = f"{BASE_URL}/v2/auth/credentials"
data = {
"hostname": credentials.hostname,
"username": credentials.username,
"password": credentials.password,
}
if credentials.otp:
data["otp"] = credentials.otp
response = session.post(url=url, json=data, timeout=10)
response.raise_for_status()
access_token = response.json().get("accessToken")
if not access_token:
logger.error(
"Failed to obtain access token. Please check your credentials and try again."
)
return None
return access_token
def retrieve_licenses(*, session: requests.Session, headers: Dict) -> List[Dict]:
"""
Retrieve available licenses.
"""
logger.info("Retrieving licenses...")
url = f"{BASE_URL}/v2/licenses"
response = session.get(url=url, headers=headers, timeout=10)
response.raise_for_status()
return response.json()
def select_license(*, licenses: List[Dict]) -> Optional[Dict]:
"""
Allow the user to select a license from the available options.
"""
print("Available Licenses:")
for idx, license in enumerate(licenses):
print(f"{idx + 1}. {license['licenseNumber']} - {license['licenseName']}")
try:
selected_index = int(input("Select a license by number: ")) - 1
if 0 <= selected_index < len(licenses):
selected_license = licenses[selected_index]
print(f"Selected License: {selected_license['licenseName']}")
return selected_license
else:
logger.error("Invalid license selection.")
except ValueError:
logger.error("Invalid input. Please enter a number.")
return None
def load_item_names_from_csv(*, csv_path: str) -> List[str]:
"""
Load item names from the first column of a csv file
"""
item_names: List[str] = []
try:
with open(csv_path, mode="r", newline="") as file:
csv_reader = csv.reader(file)
for row in csv_reader:
if not row: # Ensure the row is not empty
continue
item_names.append(row[0])
except FileNotFoundError:
logger.error(f"The file '{csv_path}' was not found. Please ensure the file exists in the script directory.")
raise
except Exception as e:
logger.error(f"An error occurred while processing the CSV file: {e}")
raise
return item_names
def discontinue_item(*, item_id: str, current_license: dict, headers: dict):
"""
Discontinue an item.
"""
url = f"{BASE_URL}/v2/items/discontinue"
params = {"licenseNumber": current_license["licenseNumber"], "submit": SUBMIT}
response = requests.post(url=url, headers=headers, params=params, json={"id": int(item_id)})
response.raise_for_status()
logger.info(f"Discontinued item {item_id}")
def main():
"""
Main function to run the script.
"""
args = parse_arguments()
hostname = args.hostname
username = args.username
csv_path = args.csv_path
item_names = load_item_names_from_csv(csv_path=csv_path)
print(item_names)
password = getpass.getpass(prompt=f"Password for {hostname}/{username}: ")
otp = None
if hostname == "mi.metrc.com":
otp = getpass.getpass(prompt="OTP: ")
credentials = Credentials(
hostname=hostname, username=username, password=password, otp=otp
)
with requests.Session() as session:
try:
access_token = obtain_access_token(session=session, credentials=credentials)
if not access_token:
return
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
licenses = retrieve_licenses(session=session, headers=headers)
if not licenses:
logger.info("No licenses found.")
return
current_license = select_license(licenses=licenses)
if not current_license:
return
items = load_items(
session=session,
headers=headers,
license_number=current_license["licenseNumber"],
)
for item_name in item_names:
item = next((item for item in items if item['name'] == item_name), None)
if item is None:
logger.error(f"Failed to match item with name {item_name}")
continue
discontinue_item(item_id=item["id"], current_license=current_license, headers=headers)
except requests.exceptions.Timeout:
logger.error(
"Request timed out. Please check your internet connection and try again."
)
except requests.exceptions.HTTPError as e:
logger.error(f"HTTP error: {e.response.status_code}, {e.response.text}")
except requests.exceptions.RequestException as e:
logger.error(f"An error occurred during the request: {str(e)}")
except Exception as e:
logger.error(f"An unexpected error occurred: {str(e)}")
if __name__ == "__main__":
main()