รับเวอร์ชันที่เฉพาะเจาะจงของทรัพยากร

ทรัพยากรทุกรายการจะมีช่องเวอร์ชันที่มีการเปลี่ยนแปลงทุกครั้งที่ทรัพยากร การเปลี่ยนแปลง — ช่อง etag Etag คือส่วนมาตรฐานของ HTTP และ API ปฏิทินรองรับ 2 กรณีดังนี้

  • เกี่ยวกับการแก้ไขทรัพยากรเพื่อให้แน่ใจว่าไม่มีการเขียนอื่นๆ ลงในทรัพยากรนี้ในระหว่างนี้ (การแก้ไขแบบมีเงื่อนไข)
  • ในการดึงข้อมูลทรัพยากรเพื่อดึงข้อมูลทรัพยากรเฉพาะเมื่อทรัพยากรมีการเปลี่ยนแปลงเท่านั้น (การดึงข้อมูลแบบมีเงื่อนไข)

การแก้ไขแบบมีเงื่อนไข

หากต้องการอัปเดตหรือลบทรัพยากรเฉพาะในกรณีที่ไม่มีการเปลี่ยนแปลงนับจากนั้น ครั้งล่าสุดที่คุณดึงข้อมูลมา คุณสามารถระบุส่วนหัว If-Match ที่มี ของ 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;
      }
    }
  }