Adding logging capabilities to osmclient
[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 logging
21 import copy
22 from osmclient.common import http
23
24
25 class Http(http.Http):
26
27 def __init__(self, url, user='admin', password='admin'):
28 self._url = url
29 self._user = user
30 self._password = password
31 self._http_header = None
32 self._logger = logging.getLogger('osmclient')
33
34 def _get_curl_cmd(self, endpoint):
35 self._logger.debug("")
36 curl_cmd = pycurl.Curl()
37 if self._logger.getEffectiveLevel() == logging.DEBUG:
38 curl_cmd.setopt(pycurl.VERBOSE, True)
39 curl_cmd.setopt(pycurl.URL, self._url + endpoint)
40 curl_cmd.setopt(pycurl.SSL_VERIFYPEER, 0)
41 curl_cmd.setopt(pycurl.SSL_VERIFYHOST, 0)
42 if self._http_header:
43 curl_cmd.setopt(pycurl.HTTPHEADER, self._http_header)
44 return curl_cmd
45
46 def delete_cmd(self, endpoint):
47 self._logger.debug("")
48 data = BytesIO()
49 curl_cmd = self._get_curl_cmd(endpoint)
50 curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE")
51 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
52 self._logger.info("Request METHOD: {} URL: {}".format("DELETE",self._url + endpoint))
53 curl_cmd.perform()
54 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
55 self._logger.info("Response HTTPCODE: {}".format(http_code))
56 curl_cmd.close()
57 # TODO 202 accepted should be returned somehow
58 if data.getvalue():
59 self._logger.verbose("Response DATA: {}".format(json.loads(data.getvalue().decode())))
60 return http_code, data.getvalue().decode()
61 else:
62 return http_code, None
63
64 def send_cmd(self, endpoint='', postfields_dict=None,
65 formfile=None, filename=None,
66 put_method=False, patch_method=False):
67 self._logger.debug("")
68 data = BytesIO()
69 curl_cmd = self._get_curl_cmd(endpoint)
70 if put_method:
71 curl_cmd.setopt(pycurl.CUSTOMREQUEST, "PUT")
72 elif patch_method:
73 curl_cmd.setopt(pycurl.CUSTOMREQUEST, "PATCH")
74 curl_cmd.setopt(pycurl.POST, 1)
75 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
76
77 if postfields_dict is not None:
78 jsondata = json.dumps(postfields_dict)
79 if 'password' in postfields_dict:
80 postfields_dict_copy = copy.deepcopy(postfields_dict)
81 postfields_dict_copy['password']='******'
82 jsondata_log = json.dumps(postfields_dict_copy)
83 else:
84 jsondata_log = jsondata
85 self._logger.verbose("Request POSTFIELDS: {}".format(jsondata_log))
86 curl_cmd.setopt(pycurl.POSTFIELDS, jsondata)
87 elif formfile is not None:
88 curl_cmd.setopt(
89 pycurl.HTTPPOST,
90 [((formfile[0],
91 (pycurl.FORM_FILE,
92 formfile[1])))])
93 elif filename is not None:
94 with open(filename, 'rb') as stream:
95 postdata=stream.read()
96 self._logger.verbose("Request POSTFIELDS: Binary content")
97 curl_cmd.setopt(pycurl.POSTFIELDS, postdata)
98
99 if put_method:
100 self._logger.info("Request METHOD: {} URL: {}".format("PUT",self._url + endpoint))
101 elif patch_method:
102 self._logger.info("Request METHOD: {} URL: {}".format("PATCH",self._url + endpoint))
103 else:
104 self._logger.info("Request METHOD: {} URL: {}".format("POST",self._url + endpoint))
105 curl_cmd.perform()
106 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
107 self._logger.info("Response HTTPCODE: {}".format(http_code))
108 curl_cmd.close()
109 if data.getvalue():
110 self._logger.verbose("Response DATA: {}".format(json.loads(data.getvalue().decode())))
111 return http_code, data.getvalue().decode()
112 else:
113 return http_code, None
114
115 def post_cmd(self, endpoint='', postfields_dict=None,
116 formfile=None, filename=None):
117 self._logger.debug("")
118 return self.send_cmd(endpoint=endpoint,
119 postfields_dict=postfields_dict,
120 formfile=formfile,
121 filename=filename,
122 put_method=False, patch_method=False)
123
124 def put_cmd(self, endpoint='', postfields_dict=None,
125 formfile=None, filename=None):
126 self._logger.debug("")
127 return self.send_cmd(endpoint=endpoint,
128 postfields_dict=postfields_dict,
129 formfile=formfile,
130 filename=filename,
131 put_method=True, patch_method=False)
132
133 def patch_cmd(self, endpoint='', postfields_dict=None,
134 formfile=None, filename=None):
135 self._logger.debug("")
136 return self.send_cmd(endpoint=endpoint,
137 postfields_dict=postfields_dict,
138 formfile=formfile,
139 filename=filename,
140 put_method=False, patch_method=True)
141
142 def get2_cmd(self, endpoint):
143 self._logger.debug("")
144 data = BytesIO()
145 curl_cmd = self._get_curl_cmd(endpoint)
146 curl_cmd.setopt(pycurl.HTTPGET, 1)
147 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
148 self._logger.info("Request METHOD: {} URL: {}".format("GET",self._url + endpoint))
149 curl_cmd.perform()
150 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
151 self._logger.info("Response HTTPCODE: {}".format(http_code))
152 curl_cmd.close()
153 if data.getvalue():
154 self._logger.debug("Response DATA: {}".format(json.loads(data.getvalue().decode())))
155 return http_code, data.getvalue().decode()
156 return http_code, None
157