ตัวอย่างการกำหนดเวลา Shift
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ตัวอย่างต่อไปนี้จะแสดงวิธีเรียก API ด้วย Python requests
โดยใช้คีย์ API สำหรับการตรวจสอบสิทธิ์ หากต้องการใช้งาน ให้ทำดังนี้
- ติดตั้งไลบรารี Python
requests
จากบรรทัดคำสั่ง pip install
requests
- บันทึกโปรแกรม Python ต่อไปนี้ลงในคอมพิวเตอร์ โดยตั้งชื่อว่า
example.py
- บันทึกไฟล์ example_request.json ในไดเรกทอรีเดียวกันกับ
(นี่คือคำขอ JSON ตัวอย่าง)
- สร้างไฟล์
credentials.json
ในไดเรกทอรีเดียวกับโปรแกรมด้วย
{"key": "your_api_key"}
- เรียกใช้ตัวอย่างจากบรรทัดคำสั่ง
python example.py
# example.py
import json
import requests
def run_example():
"""Calls the OR API to solve a shift scheduling problem."""
# Endpoint for the workforce scheduling solver in the OR API.
end_point = "https://optimization.googleapis.com/v1/scheduling:solveShiftScheduling"
# Read the API Key from a JSON file with the format:
# {"key": "your_api_key"}
with open("credentials.json") as f:
credentials = json.load(f)
api_key = credentials["key"]
# Load the JSON file with the request.
with open("example_request.json", "r") as f:
json_request = json.load(f)
# Call the API post method.
response = requests.post(f"{end_point}?key={api_key}", json=json_request)
# Process the response.
if response.ok:
solution = json.loads(response.content)
with open("example_response.json", "w") as f:
json.dump(solution, f, indent=2)
print(solution)
else:
error = json.loads(response.content)["error"]
print(f'Status code {error["code"]}: {error["message"]}')
if __name__ == "__main__":
run_example()
วิธีตั้งกําหนดเวลา
กำหนดเวลาจะกำหนดเวลาสูงสุดที่การเรียกใช้ API ควรใช้
ผู้ใช้สามารถตั้งกำหนดเวลาสำหรับไคลเอ็นต์และเซิร์ฟเวอร์ ในบริบทของ OR API
กำหนดเวลาของเซิร์ฟเวอร์เป็นวิธีที่มีประโยชน์มากกว่า เนื่องจากจะช่วยให้เซิร์ฟเวอร์แบ็กเอนด์ทราบว่า
แต่จะต้องได้รับคำขอ เรียกใช้เครื่องมือแก้โจทย์ที่สำคัญ และส่ง
คำตอบ ในทางตรงกันข้าม กำหนดเวลาของไคลเอ็นต์มีประโยชน์ในการตั้งค่าเวลาสูงสุดที่แอปพลิเคชันไคลเอ็นต์ (แอปพลิเคชันที่เรียก OR API) จะรอการตอบกลับก่อนที่จะหมดเวลา
ข้อมูลโค้ดต่อไปนี้จะตั้งค่ากำหนดเวลาทั้งของไคลเอ็นต์และเซิร์ฟเวอร์ในส่วนหัวเซสชันของคำขอ กำหนดเวลาของไคลเอ็นต์ตั้งไว้ที่ 60 วินาที
กำหนดเวลาของเซิร์ฟเวอร์ควรน้อยกว่ากำหนดเวลาของไคลเอ็นต์ที่ควรใช้
ค่าใช้จ่ายในการสื่อสาร เราตั้งกำหนดเวลาของเซิร์ฟเวอร์ไว้ที่ 95% ของไคลเอ็นต์
กำหนดเวลาจริง แต่ก็อาจแตกต่างกันออกไปตามใบสมัคร โปรดทราบว่ามีการย้ายคีย์ API ไปยังส่วนหัวของเซสชันด้วยเพื่อให้การเรียกใช้ session.post(...)
สะอาดขึ้น
# Call the API post method.
session = requests.Session()
client_deadline_seconds = 60
server_deadline_seconds = 0.95 * client_deadline_seconds
session.headers = {
"Content-Type": "application/json",
"Connection": "keep-alive",
"Keep-Alive": f"timeout={client_deadline_seconds}, max=1",
"X-Server-Timeout": f"{server_deadline_seconds}",
"X-Goog-Api-Key": api_key,
}
response = session.post(end_point, json=json_request, timeout=client_deadline_seconds)
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2024-09-12 UTC
[null,null,["อัปเดตล่าสุด 2024-09-12 UTC"],[[["\u003cp\u003eThis page provides a Python example using the \u003ccode\u003erequests\u003c/code\u003e library to call the Optimization API's shift scheduling solver.\u003c/p\u003e\n"],["\u003cp\u003eUsers need to install the \u003ccode\u003erequests\u003c/code\u003e library, prepare an API key, and use the provided JSON request file to run the example.\u003c/p\u003e\n"],["\u003cp\u003eThe example demonstrates how to send a request to the API, handle the response, and save the solution to a file.\u003c/p\u003e\n"],["\u003cp\u003eThe page also explains how to set client and server deadlines to manage the request's execution time.\u003c/p\u003e\n"]]],["This document details using Python's `requests` library to call an API with authentication. Key steps include installing `requests`, saving example Python and JSON files, and creating a `credentials.json` file with the API key. The `example.py` script reads the API key and request data, makes a POST request to the API endpoint, and processes the response. It also explains setting client and server deadlines, demonstrated by defining both in the request session headers.\n"],null,["# Shift Scheduling Example\n\nThe following example showcases how to call the API with the Python `requests`\nlibrary, using an API key for authentication. To use it:\n\n- Install the Python `requests` library. From your command line: `pip install\n requests`.\n- Save the following Python program to your computer, naming it `example.py`.\n- Save the [example_request.json](/static/optimization/service/scheduling/shift_scheduling_request.json) file in the same directory as your program (this is a sample JSON [request](/optimization/service/reference/rest/v1/scheduling/solveShiftScheduling)).\n- Create a `credentials.json` file in the same directory as your program, with `{\"key\": \"your_api_key\"}`\n- Run the example from the command line: `python example.py`.\n\n\n # example.py\n import json\n import requests\n\n def run_example():\n \"\"\"Calls the OR API to solve a shift scheduling problem.\"\"\"\n \n # Endpoint for the workforce scheduling solver in the OR API.\n end_point = \"https://optimization.googleapis.com/v1/scheduling:solveShiftScheduling\"\n \n # Read the API Key from a JSON file with the format:\n # {\"key\": \"your_api_key\"}\n with open(\"credentials.json\") as f:\n credentials = json.load(f)\n api_key = credentials[\"key\"]\n\n # Load the JSON file with the request.\n with open(\"example_request.json\", \"r\") as f:\n json_request = json.load(f)\n\n # Call the API post method.\n response = requests.post(f\"{end_point}?key={api_key}\", json=json_request)\n\n # Process the response.\n if response.ok:\n solution = json.loads(response.content)\n with open(\"example_response.json\", \"w\") as f:\n json.dump(solution, f, indent=2)\n print(solution)\n else:\n error = json.loads(response.content)[\"error\"]\n print(f'Status code {error[\"code\"]}: {error[\"message\"]}')\n\n if __name__ == \"__main__\":\n run_example()\n\nHow to set deadlines?\n---------------------\n\nA deadline determines the maximum wall time that a call to the API should take.\nA user can set both client and server deadlines. In the context of the OR API, a\nserver deadline is the more useful one as it informs the backend server of how\nmuch time it has to receive a request, run the underlying solver, and return a\nresponse. In contrast, client deadlines are useful to set the maximum time that\nthe client application (i.e., the application calling the OR API) is going to\nwait for a response, before timing out.\n\nThe following code snippet sets both a client and a server deadline in the\nsession headers of the request. The client deadline is set to 60 seconds. The\nserver deadline should be less than the client deadline to account for\ncommunication overhead. Here we set the server deadline to be 95% of the client\ndeadline, but this can vary depending on the application. Notice that the API\nkey was also moved to the session headers to make the `session.post(...)` call\ncleaner. \n\n\n\n # Call the API post method.\n session = requests.Session()\n client_deadline_seconds = 60\n server_deadline_seconds = 0.95 * client_deadline_seconds\n session.headers = {\n \"Content-Type\": \"application/json\",\n \"Connection\": \"keep-alive\",\n \"Keep-Alive\": f\"timeout={client_deadline_seconds}, max=1\",\n \"X-Server-Timeout\": f\"{server_deadline_seconds}\",\n \"X-Goog-Api-Key\": api_key,\n }\n response = session.post(end_point, json=json_request, timeout=client_deadline_seconds)"]]