aca8a4b20b72d564b08ae54bcf8c9012bfd8beb4
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
25 class Http(http
.Http
):
27 def __init__(self
, url
, user
='admin', password
='admin'):
30 self
._password
= password
31 self
._http
_header
= None
32 self
._logger
= logging
.getLogger('osmclient')
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)
43 curl_cmd
.setopt(pycurl
.HTTPHEADER
, self
._http
_header
)
46 def delete_cmd(self
, endpoint
):
47 self
._logger
.debug("")
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
))
54 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
55 self
._logger
.info("Response HTTPCODE: {}".format(http_code
))
57 # TODO 202 accepted should be returned somehow
59 self
._logger
.verbose("Response DATA: {}".format(json
.loads(data
.getvalue().decode())))
60 return http_code
, data
.getvalue().decode()
62 return http_code
, None
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("")
69 curl_cmd
= self
._get
_curl
_cmd
(endpoint
)
71 curl_cmd
.setopt(pycurl
.CUSTOMREQUEST
, "PUT")
73 curl_cmd
.setopt(pycurl
.CUSTOMREQUEST
, "PATCH")
74 curl_cmd
.setopt(pycurl
.POST
, 1)
75 curl_cmd
.setopt(pycurl
.WRITEFUNCTION
, data
.write
)
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
)
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:
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
)
100 self
._logger
.info("Request METHOD: {} URL: {}".format("PUT",self
._url
+ endpoint
))
102 self
._logger
.info("Request METHOD: {} URL: {}".format("PATCH",self
._url
+ endpoint
))
104 self
._logger
.info("Request METHOD: {} URL: {}".format("POST",self
._url
+ endpoint
))
106 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
107 self
._logger
.info("Response HTTPCODE: {}".format(http_code
))
110 self
._logger
.verbose("Response DATA: {}".format(json
.loads(data
.getvalue().decode())))
111 return http_code
, data
.getvalue().decode()
113 return http_code
, None
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
,
122 put_method
=False, patch_method
=False)
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
,
131 put_method
=True, patch_method
=False)
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
,
140 put_method
=False, patch_method
=True)
142 def get2_cmd(self
, endpoint
):
143 self
._logger
.debug("")
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
))
150 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
151 self
._logger
.info("Response HTTPCODE: {}".format(http_code
))
154 self
._logger
.debug("Response DATA: {}".format(json
.loads(data
.getvalue().decode())))
155 return http_code
, data
.getvalue().decode()
156 return http_code
, None