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
21 from osmclient
.common
import http
22 from osmclient
.common
.exceptions
import ClientException
24 class Http(http
.Http
):
26 def __init__(self
, url
, user
='admin', password
='admin'):
29 self
._password
= password
30 self
._http
_header
= None
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)
39 curl_cmd
.setopt(pycurl
.HTTPHEADER
, self
._http
_header
)
42 def delete_cmd(self
, endpoint
):
44 curl_cmd
= self
._get
_curl
_cmd
(endpoint
)
45 curl_cmd
.setopt(pycurl
.CUSTOMREQUEST
, "DELETE")
46 curl_cmd
.setopt(pycurl
.WRITEFUNCTION
, data
.write
)
48 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
49 #print 'HTTP_CODE: {}'.format(http_code)
51 # TODO 202 accepted should be returned somehow
53 return http_code
, json
.loads(data
.getvalue().decode())
55 return http_code
, None
57 def send_cmd(self
, endpoint
='', postfields_dict
=None,
58 formfile
=None, filename
=None,
59 put_method
=False, patch_method
=False):
61 curl_cmd
= self
._get
_curl
_cmd
(endpoint
)
63 curl_cmd
.setopt(pycurl
.PUT
, 1)
65 curl_cmd
.setopt(pycurl
.CUSTOMREQUEST
, "PATCH")
67 curl_cmd
.setopt(pycurl
.POST
, 1)
68 curl_cmd
.setopt(pycurl
.WRITEFUNCTION
, data
.write
)
70 if postfields_dict
is not None:
71 jsondata
= json
.dumps(postfields_dict
)
72 curl_cmd
.setopt(pycurl
.POSTFIELDS
, jsondata
)
73 elif formfile
is not None:
79 elif filename
is not None:
80 with
open(filename
, 'r') as stream
:
81 postdata
=stream
.read()
82 curl_cmd
.setopt(pycurl
.POSTFIELDS
, postdata
)
85 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
87 if http_code
not in (200, 201, 202, 204):
88 raise ClientException(data
.getvalue().decode())
89 if postfields_dict
is not None:
91 return json
.loads(data
.getvalue().decode())
93 elif formfile
is not None:
95 return yaml
.safe_load(data
.getvalue().decode())
97 elif filename
is not None:
99 return yaml
.safe_load(data
.getvalue().decode())
103 def post_cmd(self
, endpoint
='', postfields_dict
=None,
104 formfile
=None, filename
=None):
105 return self
.send_cmd(endpoint
=endpoint
,
106 postfields_dict
=postfields_dict
,
109 put_method
=False, patch_method
=False)
111 def put_cmd(self
, endpoint
='', postfields_dict
=None,
112 formfile
=None, filename
=None):
113 return self
.send_cmd(endpoint
=endpoint
,
114 postfields_dict
=postfields_dict
,
117 put_method
=True, patch_method
=False)
119 def patch_cmd(self
, endpoint
='', postfields_dict
=None,
120 formfile
=None, filename
=None):
121 return self
.send_cmd(endpoint
=endpoint
,
122 postfields_dict
=postfields_dict
,
125 put_method
=False, patch_method
=True)
127 def get2_cmd(self
, endpoint
):
129 curl_cmd
= self
._get
_curl
_cmd
(endpoint
)
130 curl_cmd
.setopt(pycurl
.HTTPGET
, 1)
131 curl_cmd
.setopt(pycurl
.WRITEFUNCTION
, data
.write
)
133 http_code
= curl_cmd
.getinfo(pycurl
.HTTP_CODE
)
136 return http_code
, data
.getvalue()
137 return http_code
, None