Enable black and pylint in tox, and update code accordingly
[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 import logging
21
22
23 class Http(object):
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 self._logger = logging.getLogger("osmclient")
30
31 def set_http_header(self, header):
32 self._http_header = header
33
34 def _get_curl_cmd(self, endpoint):
35 curl_cmd = pycurl.Curl()
36 curl_cmd.setopt(pycurl.URL, self._url + endpoint)
37 curl_cmd.setopt(pycurl.SSL_VERIFYPEER, 0)
38 curl_cmd.setopt(pycurl.SSL_VERIFYHOST, 0)
39 curl_cmd.setopt(pycurl.USERPWD, "{}:{}".format(self._user, self._password))
40 if self._http_header:
41 curl_cmd.setopt(pycurl.HTTPHEADER, self._http_header)
42 return curl_cmd
43
44 def get_cmd(self, endpoint):
45
46 data = BytesIO()
47 curl_cmd = self._get_curl_cmd(endpoint)
48 curl_cmd.setopt(pycurl.HTTPGET, 1)
49 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
50 self._logger.info(
51 "Request METHOD: {} URL: {}".format("GET", self._url + endpoint)
52 )
53 curl_cmd.perform()
54 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
55 self._logger.info("Response HTTPCODE: {}".format(http_code))
56 curl_cmd.close()
57 if data.getvalue():
58 self._logger.debug(
59 "Response DATA: {}".format(json.loads(data.getvalue().decode()))
60 )
61 return json.loads(data.getvalue().decode())
62 return None
63
64 def delete_cmd(self, endpoint):
65 data = BytesIO()
66 curl_cmd = self._get_curl_cmd(endpoint)
67 curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE")
68 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
69 self._logger.info(
70 "Request METHOD: {} URL: {}".format("DELETE", self._url + endpoint)
71 )
72 curl_cmd.perform()
73 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
74 self._logger.info("Response HTTPCODE: {}".format(http_code))
75 curl_cmd.close()
76 if data.getvalue():
77 self._logger.debug(
78 "Response DATA: {}".format(json.loads(data.getvalue().decode()))
79 )
80 return json.loads(data.getvalue().decode())
81 return None
82
83 def post_cmd(
84 self,
85 endpoint="",
86 postfields_dict=None,
87 formfile=None,
88 ):
89 data = BytesIO()
90 curl_cmd = self._get_curl_cmd(endpoint)
91 curl_cmd.setopt(pycurl.POST, 1)
92 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
93
94 if postfields_dict is not None:
95 jsondata = json.dumps(postfields_dict)
96 curl_cmd.setopt(pycurl.POSTFIELDS, jsondata)
97
98 if formfile is not None:
99 curl_cmd.setopt(
100 pycurl.HTTPPOST, [((formfile[0], (pycurl.FORM_FILE, formfile[1])))]
101 )
102
103 self._logger.info(
104 "Request METHOD: {} URL: {}".format("POST", self._url + endpoint)
105 )
106 curl_cmd.perform()
107 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
108 self._logger.info("Response HTTPCODE: {}".format(http_code))
109 curl_cmd.close()
110 if data.getvalue():
111 self._logger.debug(
112 "Response DATA: {}".format(json.loads(data.getvalue().decode()))
113 )
114 return json.loads(data.getvalue().decode())
115 return None