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
20 from osmclient
.common
import http
22 class Http(http
.Http
):
24 def __init__(self
, url
, user
='admin', password
='admin'):
27 self
._password
= password
28 self
._http
_header
= None
30 def _get_curl_cmd(self
, endpoint
):
31 curl_cmd
= pycurl
.Curl()
32 #print self._url + endpoint
33 curl_cmd
.setopt(pycurl
.URL
, self
._url
+ endpoint
)
34 curl_cmd
.setopt(pycurl
.SSL_VERIFYPEER
, 0)
35 curl_cmd
.setopt(pycurl
.SSL_VERIFYHOST
, 0)
37 curl_cmd
.setopt(pycurl
.HTTPHEADER
, self
._http
_header
)
40 def delete_cmd(self
, endpoint
):
42 curl_cmd
= self
._get
_curl
_cmd
(endpoint
)
43 curl_cmd
.setopt(pycurl
.CUSTOMREQUEST
, "DELETE")
44 curl_cmd
.setopt(pycurl
.WRITEFUNCTION
, data
.write
)
46 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
47 #print 'HTTP_CODE: {}'.format(http_code)
49 # TODO 202 accepted should be returned somehow
51 return http_code
, data
.getvalue().decode()
53 return http_code
, None
55 def send_cmd(self
, endpoint
='', postfields_dict
=None,
56 formfile
=None, filename
=None,
57 put_method
=False, patch_method
=False):
59 curl_cmd
= self
._get
_curl
_cmd
(endpoint
)
61 curl_cmd
.setopt(pycurl
.CUSTOMREQUEST
, "PUT")
63 curl_cmd
.setopt(pycurl
.CUSTOMREQUEST
, "PATCH")
64 curl_cmd
.setopt(pycurl
.POST
, 1)
65 curl_cmd
.setopt(pycurl
.WRITEFUNCTION
, data
.write
)
67 if postfields_dict
is not None:
68 jsondata
= json
.dumps(postfields_dict
)
69 curl_cmd
.setopt(pycurl
.POSTFIELDS
, jsondata
)
70 elif formfile
is not None:
76 elif filename
is not None:
77 with
open(filename
, 'rb') as stream
:
78 postdata
=stream
.read()
79 curl_cmd
.setopt(pycurl
.POSTFIELDS
, postdata
)
82 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
85 return http_code
, data
.getvalue().decode()
87 return http_code
, None
89 def post_cmd(self
, endpoint
='', postfields_dict
=None,
90 formfile
=None, filename
=None):
91 return self
.send_cmd(endpoint
=endpoint
,
92 postfields_dict
=postfields_dict
,
95 put_method
=False, patch_method
=False)
97 def put_cmd(self
, endpoint
='', postfields_dict
=None,
98 formfile
=None, filename
=None):
99 return self
.send_cmd(endpoint
=endpoint
,
100 postfields_dict
=postfields_dict
,
103 put_method
=True, patch_method
=False)
105 def patch_cmd(self
, endpoint
='', postfields_dict
=None,
106 formfile
=None, filename
=None):
107 return self
.send_cmd(endpoint
=endpoint
,
108 postfields_dict
=postfields_dict
,
111 put_method
=False, patch_method
=True)
113 def get2_cmd(self
, endpoint
):
115 curl_cmd
= self
._get
_curl
_cmd
(endpoint
)
116 curl_cmd
.setopt(pycurl
.HTTPGET
, 1)
117 curl_cmd
.setopt(pycurl
.WRITEFUNCTION
, data
.write
)
119 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
122 return http_code
, data
.getvalue().decode()
123 return http_code
, None