-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_util.py
More file actions
32 lines (25 loc) · 1006 Bytes
/
api_util.py
File metadata and controls
32 lines (25 loc) · 1006 Bytes
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
import requests
import json
def delete_product(api_url, product_name):
url = f"{api_url}/delete/{product_name}"
response = requests.delete(url)
if response.status_code == 200:
print(f"Product '{product_name}' deleted successfully.")
elif response.status_code == 404:
print(f"Product '{product_name}' not found.")
else:
print(f"Failed to delete product. Status code: {response.status_code}")
print("Response:", response.text)
def add_product(url, product_data):
url += "add"
headers = {'Content-Type': 'application/json'}
response = requests.post(url, data=json.dumps(product_data), headers=headers)
try:
response_data = response.json()
except json.decoder.JSONDecodeError:
response_data = response.text
if response.status_code == 201:
print("Product added successfully.")
else:
print("Failed to add product. Status code:", response.status_code)
print("Response:", response_data)