[CalendarServer-changes] [11210] CalDAVClientLibrary/trunk/caldavclientlibrary

source_changes at macosforge.org source_changes at macosforge.org
Fri May 17 07:39:12 PDT 2013


Revision: 11210
          http://trac.calendarserver.org//changeset/11210
Author:   cdaboo at apple.com
Date:     2013-05-17 07:39:12 -0700 (Fri, 17 May 2013)
Log Message:
-----------
Add CardDAV multiget and query support, as per tickets #806 and #810.

Modified Paths:
--------------
    CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py

Added Paths:
-----------
    CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/multiget.py
    CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/query.py
    CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/
    CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/__init__.py
    CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/test_makeaddressbook.py
    CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/test_multiget.py

Modified: CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py	2013-05-17 14:12:27 UTC (rev 11209)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/client/clientsession.py	2013-05-17 14:39:12 UTC (rev 11210)
@@ -17,11 +17,12 @@
 from caldavclientlibrary.client.httpshandler import SmartHTTPConnection
 from caldavclientlibrary.protocol.caldav.definitions import headers
 from caldavclientlibrary.protocol.caldav.makecalendar import MakeCalendar
+from caldavclientlibrary.protocol.caldav.multiget import Multiget as CalMultiget
+from caldavclientlibrary.protocol.caldav.query import QueryVEVENTTimeRange
 from caldavclientlibrary.protocol.carddav.makeaddressbook import MakeAddressBook
+from caldavclientlibrary.protocol.carddav.multiget import Multiget as AdbkMultiget
 from caldavclientlibrary.protocol.http.authentication.basic import Basic
 from caldavclientlibrary.protocol.http.authentication.digest import Digest
-from caldavclientlibrary.protocol.webdav.synccollection import SyncCollection
-from caldavclientlibrary.protocol.caldav.query import QueryVEVENTTimeRange
 try:
     from caldavclientlibrary.protocol.http.authentication.gssapi import Kerberos
 except ImportError:
@@ -44,6 +45,7 @@
 from caldavclientlibrary.protocol.webdav.proppatch import PropPatch
 from caldavclientlibrary.protocol.webdav.put import Put
 from caldavclientlibrary.protocol.webdav.session import Session
+from caldavclientlibrary.protocol.webdav.synccollection import SyncCollection
 from xml.etree.ElementTree import Element, tostring
 import types
 
@@ -513,6 +515,64 @@
             self.handleHTTPError(request)
 
 
+    def calendarMultiGet(self, rurl, hrefs, props):
+        """
+        Fetches the specified props for the specified hrefs using a single
+        multiget call. The return value is a dictionary where the keys are the
+        hrefs and the values are PropFindResult objects containing results for
+        the requested props.
+        """
+
+        assert(isinstance(rurl, URL))
+
+        request = CalMultiget(self, rurl.relativeURL(), hrefs=hrefs, props=props)
+        result = ResponseDataString()
+        request.setOutput(result)
+
+        # Process it
+        self.runSession(request)
+
+        # If it's a 207 we want to parse the XML
+        if request.getStatusCode() == statuscodes.MultiStatus:
+
+            parser = PropFindParser()
+            parser.parseData(result.getData())
+            return parser.getResults()
+
+        else:
+            self.handleHTTPError(request)
+            return None
+
+
+    def addressbookMultiGet(self, rurl, hrefs, props):
+        """
+        Fetches the specified props for the specified hrefs using a single
+        multiget call. The return value is a dictionary where the keys are the
+        hrefs and the values are PropFindResult objects containing results for
+        the requested props.
+        """
+
+        assert(isinstance(rurl, URL))
+
+        request = AdbkMultiget(self, rurl.relativeURL(), hrefs=hrefs, props=props)
+        result = ResponseDataString()
+        request.setOutput(result)
+
+        # Process it
+        self.runSession(request)
+
+        # If it's a 207 we want to parse the XML
+        if request.getStatusCode() == statuscodes.MultiStatus:
+
+            parser = PropFindParser()
+            parser.parseData(result.getData())
+            return parser.getResults()
+
+        else:
+            self.handleHTTPError(request)
+            return None
+
+
     def syncCollection(self, rurl, synctoken, props=(), infinite=False):
 
         assert(isinstance(rurl, URL))

Added: CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/multiget.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/multiget.py	                        (rev 0)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/multiget.py	2013-05-17 14:39:12 UTC (rev 11210)
@@ -0,0 +1,73 @@
+##
+# Copyright (c) 2007-2013 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from StringIO import StringIO
+from caldavclientlibrary.protocol.carddav.definitions import carddavxml
+from caldavclientlibrary.protocol.http.data.string import RequestDataString
+from caldavclientlibrary.protocol.utils.xmlhelpers import BetterElementTree
+from caldavclientlibrary.protocol.webdav.definitions import davxml
+from caldavclientlibrary.protocol.webdav.report import Report
+from xml.etree.ElementTree import Element
+from xml.etree.ElementTree import SubElement
+
+class Multiget(Report):
+
+    def __init__(self, session, url, hrefs, props=()):
+        super(Multiget, self).__init__(session, url)
+        self.props = props
+        self.hrefs = hrefs
+
+        self.initRequestData()
+
+
+    def initRequestData(self):
+        # Write XML info to a string
+        os = StringIO()
+        self.generateXML(os)
+        self.request_data = RequestDataString(os.getvalue(), "text/xml charset=utf-8")
+
+
+    def generateXML(self, os):
+        # Structure of document is:
+        #
+        # <CardDAV:addressbook-multiget>
+        #   <DAV:prop>
+        #     <<names of each property as elements>>
+        #   </DAV:prop>
+        #   <DAV:href>...</DAV:href>
+        #   ...
+        # </CardDAV:addressbook-multiget>
+
+        # <CalDAV:calendar-multiget> element
+        multiget = Element(carddavxml.addressbook_multiget)
+
+        if self.props:
+            # <DAV:prop> element
+            prop = SubElement(multiget, davxml.prop)
+
+            # Now add each property
+            for propname in self.props:
+                # Add property element taking namespace into account
+                SubElement(prop, propname)
+
+        # Now add each href
+        for href in self.hrefs:
+            # Add href elements
+            SubElement(multiget, davxml.href).text = href
+
+        # Now we have the complete document, so write it out (no indentation)
+        xmldoc = BetterElementTree(multiget)
+        xmldoc.writeUTF8(os)

Added: CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/query.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/query.py	                        (rev 0)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/query.py	2013-05-17 14:39:12 UTC (rev 11210)
@@ -0,0 +1,85 @@
+##
+# Copyright (c) 2012-2013 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from StringIO import StringIO
+from caldavclientlibrary.protocol.carddav.definitions import carddavxml
+from caldavclientlibrary.protocol.http.data.string import RequestDataString
+from caldavclientlibrary.protocol.utils.xmlhelpers import BetterElementTree
+from caldavclientlibrary.protocol.webdav.definitions import davxml
+from caldavclientlibrary.protocol.webdav.report import Report
+from xml.etree.ElementTree import Element
+from xml.etree.ElementTree import SubElement
+
+class Query(Report):
+
+    def __init__(self, session, url, props=()):
+        super(Query, self).__init__(session, url)
+        self.props = props
+
+        self.initRequestData()
+
+
+    def initRequestData(self):
+        # Write XML info to a string
+        os = StringIO()
+        self.generateXML(os)
+        self.request_data = RequestDataString(os.getvalue(), "text/xml charset=utf-8")
+
+
+    def generateXML(self, os):
+        # Structure of document is:
+        #
+        # <CardDAV:addressbook-query>
+        #   <DAV:prop>
+        #     <<names of each property as elements>>
+        #   </DAV:prop>
+        #   <CardDAV:filter>...</CardDAV:filter>
+        # </CardDAV:addressbook-query>
+
+        # <CardDAV:addressbook-query> element
+        query = Element(carddavxml.addressbook_query)
+
+        self.addProps(query)
+
+        # Now add each href
+        self.addFilterElement(query)
+
+        # Now we have the complete document, so write it out (no indentation)
+        xmldoc = BetterElementTree(query)
+        xmldoc.writeUTF8(os)
+
+
+    def addProps(self, query):
+        """
+        Add properties to the query XML.
+        """
+
+        if self.props:
+            # <DAV:prop> element
+            prop = SubElement(query, davxml.prop)
+
+            # Now add each property
+            for propname in self.props:
+                # Add property element taking namespace into account
+                SubElement(prop, propname)
+
+
+    def addFilterElement(self, query):
+        """
+        Add a CardDAV:filter element to the specified CardDAV:addressbook-query element.
+        Sub-classes must override to add specific types of query
+        """
+        raise NotImplementedError

Added: CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/__init__.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/__init__.py	                        (rev 0)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/__init__.py	2013-05-17 14:39:12 UTC (rev 11210)
@@ -0,0 +1,15 @@
+##
+# Copyright (c) 2013 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##

Added: CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/test_makeaddressbook.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/test_makeaddressbook.py	                        (rev 0)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/test_makeaddressbook.py	2013-05-17 14:39:12 UTC (rev 11210)
@@ -0,0 +1,97 @@
+##
+# Copyright (c) 2006-2013 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from caldavclientlibrary.protocol.webdav.session import Session
+from caldavclientlibrary.protocol.carddav.makeaddressbook import MakeAddressBook
+from StringIO import StringIO
+import unittest
+
+class TestRequest(unittest.TestCase):
+
+
+    def test_Method(self):
+
+        server = Session("www.example.com")
+        request = MakeAddressBook(server, "/")
+        self.assertEqual(request.getMethod(), "MKCOL")
+
+
+
+class TestRequestHeaders(unittest.TestCase):
+    pass
+
+
+
+class TestRequestBody(unittest.TestCase):
+
+    def test_GenerateXMLDisplayname(self):
+
+        server = Session("www.example.com")
+        request = MakeAddressBook(server, "/", "home")
+        os = StringIO()
+        request.generateXML(os)
+        self.assertEqual(os.getvalue(), """<?xml version='1.0' encoding='utf-8'?>
+<ns0:mkcol xmlns:ns0="DAV:">
+  <ns0:set>
+    <ns0:prop>
+      <ns0:resourcetype>
+        <ns0:collection />
+        <ns1:addressbook xmlns:ns1="urn:ietf:params:xml:ns:carddav" />
+      </ns0:resourcetype>
+      <ns0:displayname>home</ns0:displayname>
+    </ns0:prop>
+  </ns0:set>
+</ns0:mkcol>
+""".replace("\n", "\r\n")
+)
+
+
+    def test_GenerateXMLMultipleProperties(self):
+
+        server = Session("www.example.com")
+        request = MakeAddressBook(server, "/", "home", "my personal address book")
+        os = StringIO()
+        request.generateXML(os)
+        self.assertEqual(os.getvalue(), """<?xml version='1.0' encoding='utf-8'?>
+<ns0:mkcol xmlns:ns0="DAV:">
+  <ns0:set>
+    <ns0:prop>
+      <ns0:resourcetype>
+        <ns0:collection />
+        <ns1:addressbook xmlns:ns1="urn:ietf:params:xml:ns:carddav" />
+      </ns0:resourcetype>
+      <ns0:displayname>home</ns0:displayname>
+      <ns1:addressbook-description xmlns:ns1="urn:ietf:params:xml:ns:carddav">my personal address book</ns1:addressbook-description>
+    </ns0:prop>
+  </ns0:set>
+</ns0:mkcol>
+""".replace("\n", "\r\n")
+)
+
+
+
+class TestResponse(unittest.TestCase):
+    pass
+
+
+
+class TestResponseHeaders(unittest.TestCase):
+    pass
+
+
+
+class TestResponseBody(unittest.TestCase):
+    pass

Added: CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/test_multiget.py
===================================================================
--- CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/test_multiget.py	                        (rev 0)
+++ CalDAVClientLibrary/trunk/caldavclientlibrary/protocol/carddav/tests/test_multiget.py	2013-05-17 14:39:12 UTC (rev 11210)
@@ -0,0 +1,119 @@
+##
+# Copyright (c) 2006-2013 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from StringIO import StringIO
+from caldavclientlibrary.protocol.carddav.multiget import Multiget
+from caldavclientlibrary.protocol.webdav.definitions import davxml
+from caldavclientlibrary.protocol.webdav.session import Session
+import unittest
+
+class TestRequest(unittest.TestCase):
+
+
+    def test_Method(self):
+
+        server = Session("www.example.com")
+        request = Multiget(server, "/", ())
+        self.assertEqual(request.getMethod(), "REPORT")
+
+
+
+class TestRequestHeaders(unittest.TestCase):
+    pass
+
+
+
+class TestRequestBody(unittest.TestCase):
+
+    def test_GenerateXMLOneHrefOnly(self):
+
+        server = Session("www.example.com")
+        request = Multiget(server, "/", ("/a",))
+        os = StringIO()
+        request.generateXML(os)
+        self.assertEqual(os.getvalue(), """<?xml version='1.0' encoding='utf-8'?>
+<ns0:addressbook-multiget xmlns:ns0="urn:ietf:params:xml:ns:carddav">
+  <ns1:href xmlns:ns1="DAV:">/a</ns1:href>
+</ns0:addressbook-multiget>
+""".replace("\n", "\r\n")
+)
+
+
+    def test_GenerateXMLMultipleHrefsOnly(self):
+
+        server = Session("www.example.com")
+        request = Multiget(server, "/", ("/a", "/b",))
+        os = StringIO()
+        request.generateXML(os)
+        self.assertEqual(os.getvalue(), """<?xml version='1.0' encoding='utf-8'?>
+<ns0:addressbook-multiget xmlns:ns0="urn:ietf:params:xml:ns:carddav">
+  <ns1:href xmlns:ns1="DAV:">/a</ns1:href>
+  <ns1:href xmlns:ns1="DAV:">/b</ns1:href>
+</ns0:addressbook-multiget>
+""".replace("\n", "\r\n")
+)
+
+
+    def test_GenerateXMLMultipleHrefsOneProperty(self):
+
+        server = Session("www.example.com")
+        request = Multiget(server, "/", ("/a", "/b",), (davxml.getetag,))
+        os = StringIO()
+        request.generateXML(os)
+        self.assertEqual(os.getvalue(), """<?xml version='1.0' encoding='utf-8'?>
+<ns0:addressbook-multiget xmlns:ns0="urn:ietf:params:xml:ns:carddav">
+  <ns1:prop xmlns:ns1="DAV:">
+    <ns1:getetag />
+  </ns1:prop>
+  <ns1:href xmlns:ns1="DAV:">/a</ns1:href>
+  <ns1:href xmlns:ns1="DAV:">/b</ns1:href>
+</ns0:addressbook-multiget>
+""".replace("\n", "\r\n")
+)
+
+
+    def test_GenerateXMLMultipleHrefsMultipleProperties(self):
+
+        server = Session("www.example.com")
+        request = Multiget(server, "/", ("/a", "/b",), (davxml.getetag, davxml.displayname,))
+        os = StringIO()
+        request.generateXML(os)
+        self.assertEqual(os.getvalue(), """<?xml version='1.0' encoding='utf-8'?>
+<ns0:addressbook-multiget xmlns:ns0="urn:ietf:params:xml:ns:carddav">
+  <ns1:prop xmlns:ns1="DAV:">
+    <ns1:getetag />
+    <ns1:displayname />
+  </ns1:prop>
+  <ns1:href xmlns:ns1="DAV:">/a</ns1:href>
+  <ns1:href xmlns:ns1="DAV:">/b</ns1:href>
+</ns0:addressbook-multiget>
+""".replace("\n", "\r\n")
+)
+
+
+
+class TestResponse(unittest.TestCase):
+    pass
+
+
+
+class TestResponseHeaders(unittest.TestCase):
+    pass
+
+
+
+class TestResponseBody(unittest.TestCase):
+    pass
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130517/0ea62e70/attachment-0001.html>


More information about the calendarserver-changes mailing list