ทรัพยากรทุกรายการจะมีช่องเวอร์ชันที่เปลี่ยนทุกครั้งที่ทรัพยากรมีการเปลี่ยนแปลง ซึ่งก็คือช่อง etag Etags เป็นส่วนมาตรฐานของ HTTP และ API ปฏิทินรองรับใน 2 กรณีต่อไปนี้
- เมื่อมีการแก้ไขทรัพยากรเพื่อให้แน่ใจว่าไม่มีการเขียนทรัพยากรนี้ในระหว่างนั้น (การแก้ไขแบบมีเงื่อนไข)
- เมื่อมีการดึงข้อมูลทรัพยากรเพื่อดึงข้อมูลทรัพยากรก็ต่อเมื่อทรัพยากรมีการเปลี่ยนแปลง (การดึงข้อมูลแบบมีเงื่อนไข)
การแก้ไขแบบมีเงื่อนไข
หากต้องการอัปเดตหรือลบทรัพยากรก็ต่อเมื่อทรัพยากรไม่มีการเปลี่ยนแปลงนับตั้งแต่ที่คุณดึงข้อมูลครั้งล่าสุด คุณสามารถระบุส่วนหัว If-Match ที่มีค่า etag จากการดึงข้อมูลครั้งก่อน ซึ่งมีประโยชน์มากในการป้องกันการแก้ไขทรัพยากรที่สูญหาย ไคลเอ็นต์มีตัวเลือกในการดึงข้อมูลทรัพยากรอีกครั้งและใช้การเปลี่ยนแปลงอีกครั้ง
หากรายการ (และ etag ของรายการ) ไม่มีการเปลี่ยนแปลงนับตั้งแต่การดึงข้อมูลครั้งล่าสุด การแก้ไขจะสำเร็จและระบบจะแสดงผลทรัพยากรเวอร์ชันใหม่พร้อม etag ใหม่ มิเช่นนั้นคุณจะได้รับโค้ดตอบกลับ 412 (Precondition failed)
ข้อมูลโค้ดตัวอย่างด้านล่างแสดงวิธีทำการแก้ไขแบบมีเงื่อนไข ด้วย ไลบรารีของไคลเอ็นต์ 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 (Not Modified)
ข้อมูลโค้ดตัวอย่างด้านล่างแสดงวิธีทำการดึงข้อมูลแบบมีเงื่อนไข ด้วย ไลบรารีของไคลเอ็นต์ 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; } } }