Kaynakların belirli sürümlerini alma

Her kaynağın, kaynak her defasında değişen bir sürüm alanı vardır değişiklikleri — etag alanı. Etiketler, HTTP'nin standart bir parçasıdır ve Takvim API'sinde iki durumda desteklenir:

  • O zamana kadar bu kaynağa başka bir yazma işlemi yapılmadığından emin olmak için kaynak değişikliklerinin uygulanması (koşullu değişiklik)
  • yalnızca kaynak değiştiyse kaynak verilerini almak için kaynak alımında (koşullu alma)

Koşullu değişiklik

Bir kaynağı, yalnızca o zamandan beri değişmediyse güncellemek veya silmek isterseniz bir If-Match üstbilgisi belirtebilirsiniz. önceki almadaki etag'in değeri. Bu, olası riskleri önlemek için kaynaklarda kaybolan değişiklikler. Müşteriler, verilerini değişiklikleri yeniden uygulamaktır.

Giriş (ve e etiketi) son alma işleminden sonra değişmediyse değişikliği başarılı olursa ve kaynağın yeni sürümü yeni etag ile geri döndü. Aksi takdirde, 412 (Önceden koşullandırma başarısız) yanıt kodu alırsınız.

Aşağıdaki örnek kod snippet'i, koşullu kalite güvencesi Java istemci kitaplığı.

  private static void run() throws IOException {
    // Create a test event.
    Event event = Utils.createTestEvent(client, "Test Event");
    System.out.println(String.format("Event created: %s", event.getHtmlLink()));

    // Pause while the user modifies the event in the Calendar UI.
    System.out.println("Modify the event's description and hit enter to continue.");
    System.in.read();

    // Modify the local copy of the event.
    event.setSummary("Updated Test Event");

    // Update the event, making sure that we don't overwrite other changes.
    int numAttempts = 0;
    boolean isUpdated = false;
    do {
      Calendar.Events.Update request = client.events().update("primary", event.getId(), event);
      request.setRequestHeaders(new HttpHeaders().setIfMatch(event.getEtag()));
      try {
        event = request.execute();
        isUpdated = true;
      } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == 412) {
          // A 412 status code, "Precondition failed", indicates that the etag values didn't
          // match, and the event was updated on the server since we last retrieved it. Use
          // {@link Calendar.Events.Get} to retrieve the latest version.
          Event latestEvent = client.events().get("primary", event.getId()).execute();

          // You may want to have more complex logic here to resolve conflicts. In this sample we're
          // simply overwriting the summary.
          latestEvent.setSummary(event.getSummary());
          event = latestEvent;
        } else {
          throw e;
        }
      }
      numAttempts++;
    } while (!isUpdated && numAttempts <= MAX_UPDATE_ATTEMPTS);

    if (isUpdated) {
      System.out.println("Event updated.");
    } else {
      System.out.println(String.format("Failed to update event after %d attempts.", numAttempts));
    }
  }

Koşullu alma

Bir kaynağı yalnızca son yaptığınız zamandan bu yana değişmişse almak istiyorsanız bir If-None-Match üst bilgisi belirtebilirsiniz (örneğin, önceki almadaki etag'in değeri. Giriş (ve dolayısıyla etag'i) yeni sürüm olan kaynağın yeni sürümü (ör. yeni etag döndürülür. Aksi takdirde bir 304 (Değiştirilmedi) alırsınız. yanıt kodu.

Aşağıdaki örnek kod snippet'i, koşullu yeni bir Java istemci kitaplığı.

  private static void run() throws IOException {
    // Create a test event.
    Event event = Utils.createTestEvent(client, "Test Event");
    System.out.println(String.format("Event created: %s", event.getHtmlLink()));

    // Pause while the user modifies the event in the Calendar UI.
    System.out.println("Modify the event's description and hit enter to continue.");
    System.in.read();

    // Fetch the event again if it's been modified.
    Calendar.Events.Get getRequest = client.events().get("primary", event.getId());
    getRequest.setRequestHeaders(new HttpHeaders().setIfNoneMatch(event.getEtag()));
    try {
      event = getRequest.execute();
      System.out.println("The event was modified, retrieved latest version.");
    } catch (GoogleJsonResponseException e) {
      if (e.getStatusCode() == 304) {
        // A 304 status code, "Not modified", indicates that the etags match, and the event has
        // not been modified since we last retrieved it.
        System.out.println("The event was not modified, using local version.");
      } else {
        throw e;
      }
    }
  }