1 # Copyright 2018 Telefonica
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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
17 from io
import BytesIO
22 from osmclient
.common
import http
23 from osmclient
.common
.exceptions
import OsmHttpException
26 class Http(http
.Http
):
28 def __init__(self
, url
, user
='admin', password
='admin'):
31 self
._password
= password
32 self
._http
_header
= None
33 self
._logger
= logging
.getLogger('osmclient')
35 def _get_curl_cmd(self
, endpoint
):
36 self
._logger
.debug("")
37 curl_cmd
= pycurl
.Curl()
38 if self
._logger
.getEffectiveLevel() == logging
.DEBUG
:
39 curl_cmd
.setopt(pycurl
.VERBOSE
, True)
40 curl_cmd
.setopt(pycurl
.URL
, self
._url
+ endpoint
)
41 curl_cmd
.setopt(pycurl
.SSL_VERIFYPEER
, 0)
42 curl_cmd
.setopt(pycurl
.SSL_VERIFYHOST
, 0)
44 curl_cmd
.setopt(pycurl
.HTTPHEADER
, self
._http
_header
)
47 def delete_cmd(self
, endpoint
):
48 self
._logger
.debug("")
50 curl_cmd
= self
._get
_curl
_cmd
(endpoint
)
51 curl_cmd
.setopt(pycurl
.CUSTOMREQUEST
, "DELETE")
52 curl_cmd
.setopt(pycurl
.WRITEFUNCTION
, data
.write
)
53 self
._logger
.info("Request METHOD: {} URL: {}".format("DELETE",self
._url
+ endpoint
))
55 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
56 self
._logger
.info("Response HTTPCODE: {}".format(http_code
))
58 self
.check_http_response(http_code
, data
)
59 # TODO 202 accepted should be returned somehow
61 self
._logger
.verbose("Response DATA: {}".format(json
.loads(data
.getvalue().decode())))
62 return http_code
, data
.getvalue().decode()
64 return http_code
, None
66 def send_cmd(self
, endpoint
='', postfields_dict
=None,
67 formfile
=None, filename
=None,
68 put_method
=False, patch_method
=False):
69 self
._logger
.debug("")
71 curl_cmd
= self
._get
_curl
_cmd
(endpoint
)
73 curl_cmd
.setopt(pycurl
.CUSTOMREQUEST
, "PUT")
75 curl_cmd
.setopt(pycurl
.CUSTOMREQUEST
, "PATCH")
76 curl_cmd
.setopt(pycurl
.POST
, 1)
77 curl_cmd
.setopt(pycurl
.WRITEFUNCTION
, data
.write
)
79 if postfields_dict
is not None:
80 jsondata
= json
.dumps(postfields_dict
)
81 if 'password' in postfields_dict
:
82 postfields_dict_copy
= copy
.deepcopy(postfields_dict
)
83 postfields_dict_copy
['password']='******'
84 jsondata_log
= json
.dumps(postfields_dict_copy
)
86 jsondata_log
= jsondata
87 self
._logger
.verbose("Request POSTFIELDS: {}".format(jsondata_log
))
88 curl_cmd
.setopt(pycurl
.POSTFIELDS
, jsondata
)
89 elif formfile
is not None:
95 elif filename
is not None:
96 with
open(filename
, 'rb') as stream
:
97 postdata
=stream
.read()
98 self
._logger
.verbose("Request POSTFIELDS: Binary content")
99 curl_cmd
.setopt(pycurl
.POSTFIELDS
, postdata
)
102 self
._logger
.info("Request METHOD: {} URL: {}".format("PUT",self
._url
+ endpoint
))
104 self
._logger
.info("Request METHOD: {} URL: {}".format("PATCH",self
._url
+ endpoint
))
106 self
._logger
.info("Request METHOD: {} URL: {}".format("POST",self
._url
+ endpoint
))
108 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
109 self
._logger
.info("Response HTTPCODE: {}".format(http_code
))
111 self
.check_http_response(http_code
, data
)
113 self
._logger
.verbose("Response DATA: {}".format(json
.loads(data
.getvalue().decode())))
114 return http_code
, data
.getvalue().decode()
116 return http_code
, None
118 def post_cmd(self
, endpoint
='', postfields_dict
=None,
119 formfile
=None, filename
=None):
120 self
._logger
.debug("")
121 return self
.send_cmd(endpoint
=endpoint
,
122 postfields_dict
=postfields_dict
,
125 put_method
=False, patch_method
=False)
127 def put_cmd(self
, endpoint
='', postfields_dict
=None,
128 formfile
=None, filename
=None):
129 self
._logger
.debug("")
130 return self
.send_cmd(endpoint
=endpoint
,
131 postfields_dict
=postfields_dict
,
134 put_method
=True, patch_method
=False)
136 def patch_cmd(self
, endpoint
='', postfields_dict
=None,
137 formfile
=None, filename
=None):
138 self
._logger
.debug("")
139 return self
.send_cmd(endpoint
=endpoint
,
140 postfields_dict
=postfields_dict
,
143 put_method
=False, patch_method
=True)
145 def get2_cmd(self
, endpoint
):
146 self
._logger
.debug("")
148 curl_cmd
= self
._get
_curl
_cmd
(endpoint
)
149 curl_cmd
.setopt(pycurl
.HTTPGET
, 1)
150 curl_cmd
.setopt(pycurl
.WRITEFUNCTION
, data
.write
)
151 self
._logger
.info("Request METHOD: {} URL: {}".format("GET",self
._url
+ endpoint
))
153 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
154 self
._logger
.info("Response HTTPCODE: {}".format(http_code
))
156 self
.check_http_response(http_code
, data
)
158 self
._logger
.debug("Response DATA: {}".format(json
.loads(data
.getvalue().decode())))
159 return http_code
, data
.getvalue().decode()
160 return http_code
, None
162 def check_http_response(self
, http_code
, data
):
166 resp
=": " + data
.getvalue().decode()
167 raise OsmHttpException("Error {}{}".format(http_code
, resp
))