获取特定版本的资源

每个资源都有一个版本字段,该字段会在资源每次发生更改时更改 - 即 etag 字段。ETag 是 HTTP 的标准部分,在以下两种情况下,日历 API 支持 ETag:

  • 对资源修改进行监控,以确保在此期间没有对此资源进行任何其他写入(有条件的修改)
  • 在资源检索时,仅在资源发生更改时检索资源数据(有条件检索)

条件修改

如果您希望仅在资源自上次检索以来没有更改时才更新或删除该资源,可以指定一个 If-Match 标头,其中包含上次检索时 ETag 的值。这对于防止资源的修改丢失非常有用。客户端可以选择重新检索资源并重新应用更改。

如果该条目(及其 ETag)自上次检索后未发生更改,则修改会成功,系统会返回具有新 ETag 的新版资源。否则,您会收到 412(前提条件失败)响应代码。

以下示例代码段演示了如何使用 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));
    }
  }

条件检索

如果您只想在资源自上次检索后发生更改时检索该资源,可以指定一个 If-None-Match 标头,其中包含上次检索时 ETag 的值。如果自上次检索以来条目(及其 ETag)已更改,则系统会返回具有新 ETag 的新版资源。否则,您会收到 304(未修改)响应代码。

以下示例代码段演示了如何使用 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;
      }
    }
  }