Support of new OSM NB API following SOL005
[osm/osmclient.git] / osmclient / sol005 / http.py
1 # Copyright 2018 Telefonica
2 #
3 # All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16
17 from io import BytesIO
18 import pycurl
19 import json
20 import yaml
21 from osmclient.common import http
22 from osmclient.common.exceptions import ClientException
23
24 class Http(http.Http):
25
26 def __init__(self, url, user='admin', password='admin'):
27 self._url = url
28 self._user = user
29 self._password = password
30 self._http_header = None
31
32 def _get_curl_cmd(self, endpoint):
33 curl_cmd = pycurl.Curl()
34 #print self._url + endpoint
35 curl_cmd.setopt(pycurl.URL, self._url + endpoint)
36 curl_cmd.setopt(pycurl.SSL_VERIFYPEER, 0)
37 curl_cmd.setopt(pycurl.SSL_VERIFYHOST, 0)
38 if self._http_header:
39 curl_cmd.setopt(pycurl.HTTPHEADER, self._http_header)
40 return curl_cmd
41
42 def delete_cmd(self, endpoint):
43 data = BytesIO()
44 curl_cmd = self._get_curl_cmd(endpoint)
45 curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE")
46 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
47 curl_cmd.perform()
48 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
49 #print 'HTTP_CODE: {}'.format(http_code)
50 curl_cmd.close()
51 if http_code == 204:
52 return None
53 elif http_code == 404:
54 if data.getvalue():
55 return json.loads(data.getvalue().decode())
56 else:
57 return "NOT FOUND"
58 if data.getvalue():
59 return json.loads(data.getvalue().decode())
60 return "Failed"
61
62 def send_cmd(self, endpoint='', postfields_dict=None,
63 formfile=None, filename=None,
64 put_method=False):
65 data = BytesIO()
66 curl_cmd = self._get_curl_cmd(endpoint)
67 if put_method:
68 curl_cmd.setopt(pycurl.PUT, 1)
69 else:
70 curl_cmd.setopt(pycurl.POST, 1)
71 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
72
73 if postfields_dict is not None:
74 jsondata = json.dumps(postfields_dict)
75 curl_cmd.setopt(pycurl.POSTFIELDS, jsondata)
76 elif formfile is not None:
77 curl_cmd.setopt(
78 pycurl.HTTPPOST,
79 [((formfile[0],
80 (pycurl.FORM_FILE,
81 formfile[1])))])
82 elif filename is not None:
83 with open(filename, 'r') as stream:
84 postdata=stream.read()
85 curl_cmd.setopt(pycurl.POSTFIELDS, postdata)
86
87 curl_cmd.perform()
88 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
89 curl_cmd.close()
90 if http_code not in (200, 201, 202, 204):
91 raise ClientException(data.getvalue().decode())
92 if postfields_dict is not None:
93 if data.getvalue():
94 return json.loads(data.getvalue().decode())
95 return None
96 elif formfile is not None:
97 if data.getvalue():
98 return yaml.safe_load(data.getvalue().decode())
99 return None
100 elif filename is not None:
101 if data.getvalue():
102 return yaml.safe_load(data.getvalue().decode())
103 return None
104 return None
105
106 def post_cmd(self, endpoint='', postfields_dict=None,
107 formfile=None, filename=None):
108 return self.send_cmd(endpoint=endpoint,
109 postfields_dict=postfields_dict,
110 formfile=formfile,
111 filename=filename,
112 put_method=False)
113
114 def put_cmd(self, endpoint='', postfields_dict=None,
115 formfile=None, filename=None):
116 return self.send_cmd(endpoint=endpoint,
117 postfields_dict=postfields_dict,
118 formfile=formfile,
119 filename=filename,
120 put_method=True)
121
122 def get2_cmd(self, endpoint):
123 data = BytesIO()
124 curl_cmd = self._get_curl_cmd(endpoint)
125 curl_cmd.setopt(pycurl.HTTPGET, 1)
126 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
127 curl_cmd.perform()
128 curl_cmd.close()
129 return data.getvalue()
130