每个资源都有一个版本字段,即 etag
字段,每当资源发生更改时,版本字段都会更改。Etag 是 HTTP 的标准部分,在两种情况下在 calendar 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; } } }