每項資源都有版本欄位,每次資源都會變更
變更 — 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 (未修改)
回應代碼。
下列程式碼片段示範如何執行條件式 擷取 YAML 檔案 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; } } }