Vardiya Planlama Örneği

Aşağıdaki örnek, kimlik doğrulama için API anahtarı kullanılarak Python requests kitaplığıyla API'nin nasıl çağrılacağını gösterir. Bunu kullanmak için:

  • Python requests kitaplığını yükleyin. Komut satırınızdan: pip install requests.
  • Aşağıdaki Python programını bilgisayarınıza example.py olarak adlandırarak kaydedin.
  • example_request.json dosyasını programınızla aynı dizine kaydedin (bu örnek bir JSON isteği örneğidir).
  • {"key": "your_api_key"} kullanarak, programınızla aynı dizinde bir credentials.json dosyası oluşturun
  • Örneği komut satırından çalıştırın: 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()

Teslim tarihleri nasıl belirlenir?

Son tarih, API'ye yapılan bir çağrının geçmesi gereken maksimum duvar süresini belirler. Kullanıcı hem istemci hem de sunucu son tarihlerini ayarlayabilir. OR API'si bağlamında sunucu son tarihi, arka uç sunucusuna istek almak, temel çözücüyü çalıştırmak ve yanıt döndürmek için ne kadar süre gerektiği konusunda bilgi verdiğinden daha yararlıdır. Buna karşılık istemci son tarihleri, istemci uygulamasının (ör. OR API'yi çağıran uygulamanın) zaman aşımına uğramadan önce yanıt bekleyeceği maksimum süreyi ayarlamak için yararlıdır.

Aşağıdaki kod snippet'i, isteğin oturum üst bilgilerinde hem istemci hem de sunucu son tarihini ayarlar. İstemci son tarihi 60 saniye olarak ayarlanmış. Sunucu son tarihi, iletişim ek yükünü hesaba katmak için istemcinin son tarihinden daha kısa olmalıdır. Burada, sunucu son tarihini istemci son tarihinin% 95'i olacak şekilde belirledik, ancak bu süre uygulamaya bağlı olarak değişebilir. API anahtarının, session.post(...) çağrısını daha temiz hale getirmek için oturum başlıklarına da taşındığına dikkat edin.



# 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)