Support of new OSM NB API following SOL005
[osm/osmclient.git] / osmclient / sol005 / http.py
diff --git a/osmclient/sol005/http.py b/osmclient/sol005/http.py
new file mode 100644 (file)
index 0000000..11ddca6
--- /dev/null
@@ -0,0 +1,130 @@
+# Copyright 2018 Telefonica
+#
+# 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 io import BytesIO
+import pycurl
+import json
+import yaml
+from osmclient.common import http
+from osmclient.common.exceptions import ClientException
+
+class Http(http.Http):
+
+    def __init__(self, url, user='admin', password='admin'):
+        self._url = url
+        self._user = user
+        self._password = password
+        self._http_header = None
+
+    def _get_curl_cmd(self, endpoint):
+        curl_cmd = pycurl.Curl()
+        #print self._url + endpoint
+        curl_cmd.setopt(pycurl.URL, self._url + endpoint)
+        curl_cmd.setopt(pycurl.SSL_VERIFYPEER, 0)
+        curl_cmd.setopt(pycurl.SSL_VERIFYHOST, 0)
+        if self._http_header:
+            curl_cmd.setopt(pycurl.HTTPHEADER, self._http_header)
+        return curl_cmd
+
+    def delete_cmd(self, endpoint):
+        data = BytesIO()
+        curl_cmd = self._get_curl_cmd(endpoint)
+        curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE")
+        curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
+        curl_cmd.perform()
+        http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
+        #print 'HTTP_CODE: {}'.format(http_code)
+        curl_cmd.close()
+        if http_code == 204:
+            return None
+        elif http_code == 404:
+            if data.getvalue():
+                return json.loads(data.getvalue().decode())
+            else:
+                return "NOT FOUND"
+        if data.getvalue():
+            return json.loads(data.getvalue().decode())
+        return "Failed"
+
+    def send_cmd(self, endpoint='', postfields_dict=None,
+                 formfile=None, filename=None,
+                 put_method=False):
+        data = BytesIO()
+        curl_cmd = self._get_curl_cmd(endpoint)
+        if put_method:
+            curl_cmd.setopt(pycurl.PUT, 1)
+        else:
+            curl_cmd.setopt(pycurl.POST, 1)
+        curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
+
+        if postfields_dict is not None:
+            jsondata = json.dumps(postfields_dict)
+            curl_cmd.setopt(pycurl.POSTFIELDS, jsondata)
+        elif formfile is not None:
+            curl_cmd.setopt(
+                pycurl.HTTPPOST,
+                [((formfile[0],
+                           (pycurl.FORM_FILE,
+                            formfile[1])))])
+        elif filename is not None:
+            with open(filename, 'r') as stream:
+                postdata=stream.read()
+            curl_cmd.setopt(pycurl.POSTFIELDS, postdata)
+
+        curl_cmd.perform()
+        http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
+        curl_cmd.close()
+        if http_code not in (200, 201, 202, 204):
+            raise ClientException(data.getvalue().decode())
+        if postfields_dict is not None:
+            if data.getvalue():
+                return json.loads(data.getvalue().decode())
+            return None
+        elif formfile is not None:
+            if data.getvalue():
+                return yaml.safe_load(data.getvalue().decode())
+            return None
+        elif filename is not None:
+            if data.getvalue():
+                return yaml.safe_load(data.getvalue().decode())
+            return None
+        return None
+
+    def post_cmd(self, endpoint='', postfields_dict=None,
+                 formfile=None, filename=None):
+        return self.send_cmd(endpoint=endpoint,
+                             postfields_dict=postfields_dict,
+                             formfile=formfile,
+                             filename=filename,
+                             put_method=False)
+
+    def put_cmd(self, endpoint='', postfields_dict=None,
+                formfile=None, filename=None):
+        return self.send_cmd(endpoint=endpoint,
+                             postfields_dict=postfields_dict,
+                             formfile=formfile,
+                             filename=filename,
+                             put_method=True)
+
+    def get2_cmd(self, endpoint):
+        data = BytesIO()
+        curl_cmd = self._get_curl_cmd(endpoint)
+        curl_cmd.setopt(pycurl.HTTPGET, 1)
+        curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
+        curl_cmd.perform()
+        curl_cmd.close()
+        return data.getvalue()
+