ตัวอย่างการกำหนดเวลา 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)