संसाधनों के खास वर्शन पाना

हर संसाधन में एक वर्शन फ़ील्ड होता है. जब भी संसाधन में बदलाव होता है, तब यह फ़ील्ड बदल जाता है. इसे etag फ़ील्ड कहते हैं. Etags, एचटीटीपी का एक स्टैंडर्ड हिस्सा हैं. साथ ही, Calendar API में इनका इस्तेमाल दो मामलों में किया जा सकता है:

  • संसाधन में किए गए बदलावों पर, ताकि यह पक्का किया जा सके कि इस दौरान इस संसाधन में कोई और बदलाव नहीं किया गया है (शर्त के साथ बदलाव)
  • संसाधन में बदलाव होने पर ही संसाधन का डेटा वापस पाने के लिए, संसाधन वापस पाने की सुविधा (शर्त के साथ वापस पाना)

शर्त के हिसाब से बदलाव करना

अगर आपको किसी संसाधन को सिर्फ़ तब अपडेट या मिटाना है, जब उसे पिछली बार पाने के बाद से उसमें कोई बदलाव न हुआ हो, तो If-Match हेडर तय किया जा सकता है. इसमें पिछली बार संसाधन पाने के समय का ईटैग वैल्यू शामिल होती है. इससे संसाधनों में किए गए बदलावों को मिटने से रोकने में मदद मिलती है. क्लाइंट के पास संसाधन को फिर से पाने और बदलावों को फिर से लागू करने का विकल्प होता है.

अगर पिछली बार फ़ेच करने के बाद से एंट्री (और उसका ईटैग) नहीं बदला है, तो बदलाव हो जाएगा. साथ ही, संसाधन का नया वर्शन नए ईटैग के साथ दिखेगा. ऐसा न होने पर, आपको 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 हेडर तय किया जा सकता है. इसमें पिछली बार वापस पाए गए ईटैग की वैल्यू होती है. अगर पिछली बार फ़ेच करने के बाद से एंट्री (और इस तरह इसका ईटैग) में बदलाव हुआ है, तो संसाधन का नया वर्शन नए ईटैग के साथ दिखेगा. ऐसा न करने पर, आपको 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;
      }
    }
  }