获取特定版本的资源

每个资源都有一个版本字段,该字段每次资源都会更改 etag 字段。Etag 是 HTTP 的标准组成部分, 日历 API 中支持的两种情况:

  • 针对资源修改执行,以确保在此期间没有对此资源执行任何其他写入操作(有条件地修改)
  • 检索资源,以便仅在资源发生更改时检索资源数据(条件检索)

有条件的修改

如果您只想更新或删除未更改过的资源,请执行以下操作: 您可以指定一个 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;
      }
    }
  }