取得特定版本的資源

每個資源都有版本欄位,資源每次變更時,該欄位都會隨之變更,也就是 etag 欄位。ETag 是 HTTP 的標準部分,Google 日曆 API 支援以下兩種情況:

  • 在資源修改方面,確保這段期間沒有其他寫入這項資源的作業 (條件式修改)。
  • 在擷取資源時,僅在資源變更時擷取資料 (條件式擷取)。

條件式修改

如果您只想在資源自上次擷取後未變更時更新或刪除資源,可以指定包含先前擷取作業 ETag 值的 If-Match 標頭。這有助於防止資源遺失修改內容。用戶端可以重新擷取資源,並重新套用變更。

如果項目 (和 ETag) 自上次擷取後未變更,修改作業就會成功,且伺服器會傳回新版資源和新的 ETag。否則,伺服器會傳回 412 Precondition Failed 回應代碼。

以下範例說明如何使用 Java 用戶端程式庫執行條件式修改:

  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));
    }
  }

條件式擷取

如果您只想在資源自上次擷取後有所變更時擷取資源,可以指定包含先前擷取作業 ETag 值的 If-None-Match 標頭。如果項目 (以及 ETag) 自上次擷取後已變更,伺服器會傳回新版資源和新的 ETag。否則,伺服器會傳回 304 Not Modified 回應代碼。

以下範例示範如何使用 Java 用戶端程式庫執行條件式擷取:

  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;
      }
    }
  }