Mỗi tài nguyên đều có một trường phiên bản thay đổi mỗi khi tài nguyên thay đổi – trường etag. ETag là một phần tiêu chuẩn của HTTP và được hỗ trợ trong API Lịch Google cho 2 trường hợp:
- Khi sửa đổi tài nguyên, để đảm bảo không có hoạt động ghi nào khác vào tài nguyên này xảy ra trong thời gian chờ (sửa đổi có điều kiện).
- Khi truy xuất tài nguyên, chỉ truy xuất dữ liệu nếu tài nguyên đã thay đổi (truy xuất có điều kiện).
Sửa đổi có điều kiện
Nếu chỉ muốn cập nhật hoặc xoá một tài nguyên nếu tài nguyên đó chưa thay đổi kể từ lần gần nhất bạn truy xuất, bạn có thể chỉ định một tiêu đề If-Match chứa giá trị của ETag từ lần truy xuất trước. Điều này giúp ngăn chặn các nội dung sửa đổi bị mất trên tài nguyên. Các ứng dụng có thể truy xuất lại tài nguyên và áp dụng lại các thay đổi.
Nếu mục nhập (và ETag của mục nhập đó) không thay đổi kể từ lần truy xuất gần đây nhất, thì quá trình sửa đổi sẽ thành công và máy chủ sẽ trả về phiên bản mới của tài nguyên cùng với ETag mới. Nếu không, máy chủ sẽ trả về mã phản hồi 412 Precondition Failed.
Mẫu sau đây minh hoạ cách thực hiện các hoạt động sửa đổi có điều kiện bằng thư viện ứng dụng 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)); } }
Truy xuất có điều kiện
Nếu chỉ muốn truy xuất một tài nguyên nếu tài nguyên đó đã thay đổi kể từ lần truy xuất gần đây nhất, bạn có thể chỉ định một tiêu đề If-None-Match chứa giá trị ETag từ lần truy xuất trước đó. Nếu mục nhập (và do đó là ETag của mục nhập) đã thay đổi kể từ lần truy xuất gần đây nhất, thì máy chủ sẽ trả về phiên bản mới của tài nguyên cùng với ETag mới. Nếu không, máy chủ sẽ trả về mã phản hồi 304 Not Modified.
Mẫu sau đây minh hoạ cách thực hiện truy xuất có điều kiện bằng thư viện ứng dụng 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; } } }