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

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

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

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

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

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