b67b594e8531f6118be14b27a71ffb087ed57214
[osm/osmclient.git] / osmclient / common / http.py
1 # Copyright 2017 Sandvine
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
21
22 class Http(object):
23
24 def __init__(self, url, user='admin', password='admin'):
25 self._url = url
26 self._user = user
27 self._password = password
28 self._http_header = None
29
30 def set_http_header(self, header):
31 self._http_header = header
32
33 def _get_curl_cmd(self, endpoint):
34 curl_cmd = pycurl.Curl()
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)
38 curl_cmd.setopt(
39 pycurl.USERPWD,
40 '{}:{}'.format(
41 self._user,
42 self._password))
43 if self._http_header:
44 curl_cmd.setopt(pycurl.HTTPHEADER, self._http_header)
45 return curl_cmd
46
47 def get_cmd(self, endpoint):
48
49 data = BytesIO()
50 curl_cmd = self._get_curl_cmd(endpoint)
51 curl_cmd.setopt(pycurl.HTTPGET, 1)
52 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
53 self._logger.info("Request METHOD: {} URL: {}".format("GET",self._url + endpoint))
54 curl_cmd.perform()
55 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
56 self._logger.info("Response HTTPCODE: {}".format(http_code))
57 curl_cmd.close()
58 if data.getvalue():
59 self._logger.debug("Response DATA: {}".format(json.loads(data.getvalue().decode())))
60 return json.loads(data.getvalue().decode())
61 return None
62
63 def delete_cmd(self, endpoint):
64 data = BytesIO()
65 curl_cmd = self._get_curl_cmd(endpoint)
66 curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE")
67 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
68 self._logger.info("Request METHOD: {} URL: {}".format("DELETE",self._url + endpoint))
69 curl_cmd.perform()
70 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
71 self._logger.info("Response HTTPCODE: {}".format(http_code))
72 curl_cmd.close()
73 if data.getvalue():
74 self._logger.debug("Response DATA: {}".format(json.loads(data.getvalue().decode())))
75 return json.loads(data.getvalue().decode())
76 return None
77
78 def post_cmd(self, endpoint='', postfields_dict=None, formfile=None, ):
79 data = BytesIO()
80 curl_cmd = self._get_curl_cmd(endpoint)
81 curl_cmd.setopt(pycurl.POST, 1)
82 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
83
84 if postfields_dict is not None:
85 jsondata = json.dumps(postfields_dict)
86 curl_cmd.setopt(pycurl.POSTFIELDS, jsondata)
87
88 if formfile is not None:
89 curl_cmd.setopt(
90 pycurl.HTTPPOST,
91 [((formfile[0],
92 (pycurl.FORM_FILE,
93 formfile[1])))])
94
95 self._logger.info("Request METHOD: {} URL: {}".format("POST",self._url + endpoint))
96 curl_cmd.perform()
97 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
98 self._logger.info("Response HTTPCODE: {}".format(http_code))
99 curl_cmd.close()
100 if data.getvalue():
101 self._logger.debug("Response DATA: {}".format(json.loads(data.getvalue().decode())))
102 return json.loads(data.getvalue().decode())
103 return None