Write a python program to retrieve all the products from the API Server and print the total number of products currently stored in the server.Hint: the json response will be of the following example format:{ "message": "success", "data": [ { "id": 85, "productName": "Araliya Basmathi Rice", "description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "category": "Rice", "brand": "CIC", "expiredDate": "2023.05.04", "manufacturedDate": "2022.02.20", "batchNumber": 324567, "unitPrice": 1020, "quantity": 200, "createdDate": "2022.02.24" }, { "id": 86, "productName": "Araliya Basmathi Rice", "description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "category": "Rice", "brand": "CIC", "expiredDate": "2023.05.04", "manufacturedDate": "2022.02.20", "batchNumber": 324567, "unitPrice": 1020, "quantity": 200, "createdDate": "2022.02.24" },......}Answer:(penalty regime: 0 %)12345678910111213141516171819202122232425import requestsimport json# Define the URLurl = "http://host1.open.uom.lk:8080"# Define the headersheaders = {'Content-Type': 'application/json'}# Define the product datadata = { "productName": "Araliya Basmathi Rice", "description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "category": "Rice", "brand": "CIC", "expiredDate": "2023.05.04", "manufacturedDate": "2022.02.20", "batchNumber": 324567, "unitPrice": 1020, "quantity": 200, "createdDate": "2022.02.24"}# Convert the data to JSON formatjson_data = json.dumps(data)
Question
Write a python program to retrieve all the products from the API Server and print the total number of products currently stored in the server.Hint: the json response will be of the following example format:{ "message": "success", "data": [ { "id": 85, "productName": "Araliya Basmathi Rice", "description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "category": "Rice", "brand": "CIC", "expiredDate": "2023.05.04", "manufacturedDate": "2022.02.20", "batchNumber": 324567, "unitPrice": 1020, "quantity": 200, "createdDate": "2022.02.24" }, { "id": 86, "productName": "Araliya Basmathi Rice", "description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "category": "Rice", "brand": "CIC", "expiredDate": "2023.05.04", "manufacturedDate": "2022.02.20", "batchNumber": 324567, "unitPrice": 1020, "quantity": 200, "createdDate": "2022.02.24" },......}Answer:(penalty regime: 0 %)12345678910111213141516171819202122232425import requestsimport json# Define the URLurl = "http://host1.open.uom.lk:8080"# Define the headersheaders = {'Content-Type': 'application/json'}# Define the product datadata = { "productName": "Araliya Basmathi Rice", "description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "category": "Rice", "brand": "CIC", "expiredDate": "2023.05.04", "manufacturedDate": "2022.02.20", "batchNumber": 324567, "unitPrice": 1020, "quantity": 200, "createdDate": "2022.02.24"}# Convert the data to JSON formatjson_data = json.dumps(data)
Solution
Here is a Python program that retrieves all the products from the API server and prints the total number of products currently stored in the server:
import requests
import json
# Define the URL
url = "http://host1.open.uom.lk:8080/api/products"
# Send a GET request to the server
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Load the response data into a JSON format
data = json.loads(response.text)
# Get the list of products from the data
products = data['data']
# Print the total number of products
print("Total number of products: ", len(products))
else:
print("Failed to retrieve data from the server.")
This program first sends a GET request to the server to retrieve all the products. If the request is successful, it loads the response data into a JSON format. It then retrieves the list of products from the data and prints the total number of products. If the request is not successful, it prints an error message.
Similar Questions
Suppose you are writing a python web client to access an API of an online supermarket. Given below are the API details.Base URL= http://host1.open.uom.lk:8080Write a python program to retrieve all the products from the API Server and print the total number of products currently stored in the server.Hint: the json response will be of the following example format:{ "message": "success", "data": [ { "id": 85, "productName": "Araliya Basmathi Rice", "description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "category": "Rice", "brand": "CIC", "expiredDate": "2023.05.04", "manufacturedDate": "2022.02.20", "batchNumber": 324567, "unitPrice": 1020, "quantity": 200, "createdDate": "2022.02.24" }, { "id": 86, "productName": "Araliya Basmathi Rice", "description": "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "category": "Rice", "brand": "CIC", "expiredDate": "2023.05.04", "manufacturedDate": "2022.02.20", "batchNumber": 324567, "unitPrice": 1020, "quantity": 200, "createdDate": "2022.02.24" },......}
Suppose you are writing a python web client to access an API of an online supermarket. Given below are the API details.Base URL= http://host1.open.uom.lk:8080Write a python program to create a new product with the following information in JSON format. Print the response code of the request.{ "productName":"Araliya Basmathi Rice", "description":"White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "category":"Rice", "brand":"CIC", "expiredDate":"2023.05.04", "manufacturedDate":"2022.02.20", "batchNumber":324567, "unitPrice":1020, "quantity":200, "createdDate":"2022.02.24"}Answer:(penalty regime: 0 %)
Products Cart ObjectGiven an input of products in the below format (Name Quantity Price)InputPlain TextCopy["Rice", "Dal", "Salt"][2, 3, 1][60, 50, 20]Create an object with the key data which is an array of objects with the format {name: "Rice", quantity: 2, price: 60}The object must have a method called total which calculates the total values of items (multiplying quantity of each with its price)Sample output for the above case 290
You are tasked with managing an inventory of products in a retail store. The inventory contains various products, each with a name(key) and price(value). Use dictionary to create the inventory. Write a Python program to find the total value of products that strictly exceed a certain threshold value. Further, display the name of products whose price is less than or equal to the threshold value (Each product name should be printed in a separate line). Develop code by using user-defined function whose argument is a dictionary. DO NOT USE BUILT-IN FUNCTIONS IN CALCULATING THE RESULTS.Accept the number of products in the inventory from user, say N.Read product name and value from user for N number of products.Read threshold value from user.Print the total value of products above threshold.
ou need to run a query that will count all of the records in the products table. The products table lists the productID, productName, and productDescription. The productID field is the primary key. There are no other constraints on any of the fields. What query or queries should you execute? (Choose all that apply.)1 pointSELECT count() * FROM productsSELECT count(DISTINCT productName) FROM productsSELECT count(productID) FROM productsSELECT count(*) FROM productsCoursera Honor Code Learn more
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.