chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lmig...@apache.org
Subject svn commit: r1836995 - in /chemistry/cmislib/branches/py3_compat/src/cmislib: atompub/binding.py browser/binding.py cmis_services.py
Date Sun, 29 Jul 2018 16:56:51 GMT
Author: lmignon
Date: Sun Jul 29 16:56:51 2018
New Revision: 1836995

URL: http://svn.apache.org/viewvc?rev=1836995&view=rev
Log:
py3compat: share more code between the AtomPub and Browser bindings

Modified:
    chemistry/cmislib/branches/py3_compat/src/cmislib/atompub/binding.py
    chemistry/cmislib/branches/py3_compat/src/cmislib/browser/binding.py
    chemistry/cmislib/branches/py3_compat/src/cmislib/cmis_services.py

Modified: chemistry/cmislib/branches/py3_compat/src/cmislib/atompub/binding.py
URL: http://svn.apache.org/viewvc/chemistry/cmislib/branches/py3_compat/src/cmislib/atompub/binding.py?rev=1836995&r1=1836994&r2=1836995&view=diff
==============================================================================
--- chemistry/cmislib/branches/py3_compat/src/cmislib/atompub/binding.py (original)
+++ chemistry/cmislib/branches/py3_compat/src/cmislib/atompub/binding.py Sun Jul 29 16:56:51
2018
@@ -108,21 +108,12 @@ class AtomPubBinding(Binding):
     """
 
     def __init__(self, **kwargs):
-        self.extArgs = kwargs
+        super(AtomPubBinding, self).__init__(**kwargs)
         self.user_agent = 'cmislib/atompub +http://chemistry.apache.org/'
 
     def getRepositoryService(self):
         return RepositoryService()
 
-    def _get_http_headers(self, **kwargs):
-        headers = {}
-        if kwargs:
-            if 'headers' in kwargs:
-                headers = kwargs['headers']
-                del kwargs['headers']
-        headers['User-Agent'] = self.user_agent
-        return headers
-
     def get(self, url, session, **kwargs):
 
         """
@@ -135,13 +126,7 @@ class AtomPubBinding(Binding):
         the root folder (:class:`Repository.getRootFolder`) and drill down from
         there.
         """
-
-        # merge the cmis client extended args with the ones that got passed in
-        if len(self.extArgs) > 0:
-            kwargs.update(self.extArgs)
-
-        headers = self._get_http_headers(**kwargs)
-        response = session.get(url, params=kwargs, headers=headers)
+        response = super(AtomPubBinding, self).get(url, session, **kwargs)
         if '+xml' in response.headers.get('content-type'):
             try:
                 return minidom.parse(BytesIO(response.content))
@@ -149,24 +134,6 @@ class AtomPubBinding(Binding):
                 raise CmisException('Could not parse server response', url)
         return response
 
-    def delete(self, url, session, **kwargs):
-
-        """
-        Does a delete against the CMIS service. More than likely, you will not
-        need to call this method. Instead, let the other objects do it for you.
-
-        For example, to delete a folder you'd call :class:`Folder.delete` and
-        to delete a document you'd call :class:`Document.delete`.
-        """
-
-        # merge the cmis client extended args with the ones that got passed in
-        if len(self.extArgs) > 0:
-            kwargs.update(self.extArgs)
-
-        headers = self._get_http_headers(**kwargs)
-        response = session.delete(url, params=kwargs, headers=headers)
-        return response
-
     def post(self, url, session, payload, contentType, **kwargs):
 
         """
@@ -179,15 +146,11 @@ class AtomPubBinding(Binding):
         """
 
         # merge the cmis client extended args with the ones that got passed in
-        if len(self.extArgs) > 0:
-            kwargs.update(self.extArgs)
-        headers = self._get_http_headers(**kwargs)
-        headers['Content-Type'] = contentType
-        result = session.post(
-            url, params=kwargs, data=payload, headers=headers)
-        if result.text:
+        response = super(AtomPubBinding, self).post(
+            url, session, payload, contentType, **kwargs)
+        if response.content and '+xml' in response.headers.get('content-type'):
             try:
-                return minidom.parse(BytesIO(result.content))
+                return minidom.parse(BytesIO(response.content))
             except ExpatError:
                 raise CmisException('Could not parse server response', url)
         return None
@@ -202,20 +165,12 @@ class AtomPubBinding(Binding):
         :class:`CmisObject.updateProperties`. Or, to check in a document that's
         been checked out, you'd call :class:`Document.checkin` on the PWC.
         """
-
-        # merge the cmis client extended args with the ones that got passed in
-        if len(self.extArgs) > 0:
-            kwargs.update(self.extArgs)
-
-        headers = self._get_http_headers(**kwargs)
-        headers['Content-Type'] = contentType
-        response = session.put(url, data=payload, params=kwargs, headers=headers)
-        if response.text:
+        response = super(AtomPubBinding, self).put(url, session, payload, contentType, **kwargs)
+        if response.content and '+xml' in response.headers.get('content-type'):
             try:
-                return minidom.parseString(response.text)
+                return minidom.parse(BytesIO(response.content))
             except ExpatError:
-                # This may happen and is normal
-                return None
+                raise CmisException('Could not parse server response', url)
         return None
 
 

Modified: chemistry/cmislib/branches/py3_compat/src/cmislib/browser/binding.py
URL: http://svn.apache.org/viewvc/chemistry/cmislib/branches/py3_compat/src/cmislib/browser/binding.py?rev=1836995&r1=1836994&r2=1836995&view=diff
==============================================================================
--- chemistry/cmislib/branches/py3_compat/src/cmislib/browser/binding.py (original)
+++ chemistry/cmislib/branches/py3_compat/src/cmislib/browser/binding.py Sun Jul 29 16:56:51
2018
@@ -49,21 +49,12 @@ class BrowserBinding(Binding):
     """
 
     def __init__(self, **kwargs):
-        self.extArgs = kwargs
+        super(BrowserBinding, self).__init__(**kwargs)
         self.user_agent = 'cmislib/browser +http://chemistry.apache.org/'
 
     def getRepositoryService(self):
         return RepositoryService()
 
-    def _get_http_headers(self, **kwargs):
-        headers = {}
-        if kwargs:
-            if 'headers' in kwargs:
-                headers = kwargs['headers']
-                del kwargs['headers']
-        headers['User-Agent'] = self.user_agent
-        return headers
-
     def get(self, url, session, **kwargs):
 
         """
@@ -76,13 +67,7 @@ class BrowserBinding(Binding):
         the root folder (:class:`Repository.getRootFolder`) and drill down from
         there.
         """
-
-        # merge the cmis client extended args with the ones that got passed in
-        if len(self.extArgs) > 0:
-            kwargs.update(self.extArgs)
-
-        headers = self._get_http_headers(**kwargs)
-        response = session.get(url, params=kwargs, headers=headers)
+        response = super(BrowserBinding, self).get(url, session, **kwargs)
         if 'application/json' in response.headers.get('content-type'):
             return response.json()
         return response
@@ -97,16 +82,10 @@ class BrowserBinding(Binding):
         :class:`CmisObject.updateProperties`. Or, to check in a document that's
         been checked out, you'd call :class:`Document.checkin` on the PWC.
         """
-
-        # merge the cmis client extended args with the ones that got passed in
-        if len(self.extArgs) > 0:
-            kwargs.update(self.extArgs)
-        headers = self._get_http_headers(**kwargs)
-        headers['Content-Type'] = contentType
-        result = session.post(
-            url, params=kwargs, data=payload, headers=headers)
-        if result.text:
-            return result.json()
+        response = super(BrowserBinding, self).post(
+            url, session, payload, contentType, **kwargs)
+        if response.content and 'application/json' in response.headers.get('content-type'):
+            return response.json()
         return None
 
 

Modified: chemistry/cmislib/branches/py3_compat/src/cmislib/cmis_services.py
URL: http://svn.apache.org/viewvc/chemistry/cmislib/branches/py3_compat/src/cmislib/cmis_services.py?rev=1836995&r1=1836994&r2=1836995&view=diff
==============================================================================
--- chemistry/cmislib/branches/py3_compat/src/cmislib/cmis_services.py (original)
+++ chemistry/cmislib/branches/py3_compat/src/cmislib/cmis_services.py Sun Jul 29 16:56:51
2018
@@ -33,6 +33,10 @@ class Binding(object):
     Represents the binding used to communicate with the CMIS server.
     """
 
+    def __init__(self, **kwargs):
+        self.extArgs = kwargs
+        self.user_agent = 'cmislib +http://chemistry.apache.org/'
+
     def getRepositoryService(self):
 
         """
@@ -41,6 +45,92 @@ class Binding(object):
 
         pass
 
+    def _get_http_headers(self, **kwargs):
+        headers = {}
+        if kwargs:
+            if 'headers' in kwargs:
+                headers = kwargs['headers']
+                del kwargs['headers']
+        headers['User-Agent'] = self.user_agent
+        return headers
+
+    def get(self, url, session, **kwargs):
+
+        """
+        Does a get against the CMIS service. More than likely, you will not
+        need to call this method. Instead, let the other objects do it for you.
+
+        For example, if you need to get a specific object by object id, try
+        :class:`Repository.getObject`. If you have a path instead of an object
+        id, use :class:`Repository.getObjectByPath`. Or, you could start with
+        the root folder (:class:`Repository.getRootFolder`) and drill down from
+        there.
+        """
+
+        # merge the cmis client extended args with the ones that got passed in
+        if len(self.extArgs) > 0:
+            kwargs.update(self.extArgs)
+
+        headers = self._get_http_headers(**kwargs)
+        return session.get(url, params=kwargs, headers=headers)
+
+    def post(self, url, session, payload, contentType, **kwargs):
+
+        """
+        Does a post against the CMIS service. More than likely, you will not
+        need to call this method. Instead, let the other objects do it for you.
+
+        For example, to update the properties on an object, you'd call
+        :class:`CmisObject.updateProperties`. Or, to check in a document that's
+        been checked out, you'd call :class:`Document.checkin` on the PWC.
+        """
+
+        # merge the cmis client extended args with the ones that got passed in
+        if len(self.extArgs) > 0:
+            kwargs.update(self.extArgs)
+        headers = self._get_http_headers(**kwargs)
+        headers['Content-Type'] = contentType
+        return session.post(
+            url, params=kwargs, data=payload, headers=headers)
+
+    def delete(self, url, session, **kwargs):
+
+        """
+        Does a delete against the CMIS service. More than likely, you will not
+        need to call this method. Instead, let the other objects do it for you.
+
+        For example, to delete a folder you'd call :class:`Folder.delete` and
+        to delete a document you'd call :class:`Document.delete`.
+        """
+
+        # merge the cmis client extended args with the ones that got passed in
+        if len(self.extArgs) > 0:
+            kwargs.update(self.extArgs)
+
+        headers = self._get_http_headers(**kwargs)
+        response = session.delete(url, params=kwargs, headers=headers)
+        return response
+
+    def put(self, url, session, payload, contentType, **kwargs):
+
+        """
+        Does a put against the CMIS service. More than likely, you will not
+        need to call this method. Instead, let the other objects do it for you.
+
+        For example, to update the properties on an object, you'd call
+        :class:`CmisObject.updateProperties`. Or, to check in a document that's
+        been checked out, you'd call :class:`Document.checkin` on the PWC.
+        """
+
+        # merge the cmis client extended args with the ones that got passed in
+        if len(self.extArgs) > 0:
+            kwargs.update(self.extArgs)
+
+        headers = self._get_http_headers(**kwargs)
+        headers['Content-Type'] = contentType
+        return session.put(url, data=payload, params=kwargs, headers=headers)
+
+
     def _processCommonErrors(self, response):
 
         """



Mime
View raw message