4debf0d97cd804157d14a845361135accc994adf
[osm/osmclient.git] / osmclient / sol005 / http.py
1 # Copyright 2018 Telefonica
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 yaml
21 from osmclient.common import http
22 from osmclient.common.exceptions import ClientException
23
24 class Http(http.Http):
25
26 def __init__(self, url, user='admin', password='admin'):
27 self._url = url
28 self._user = user
29 self._password = password
30 self._http_header = None
31
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)
38 if self._http_header:
39 curl_cmd.setopt(pycurl.HTTPHEADER, self._http_header)
40 return curl_cmd
41
42 def delete_cmd(self, endpoint):
43 data = BytesIO()
44 curl_cmd = self._get_curl_cmd(endpoint)
45 curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE")
46 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
47 curl_cmd.perform()
48 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
49 #print 'HTTP_CODE: {}'.format(http_code)
50 curl_cmd.close()
51 # TODO 202 accepted should be returned somehow
52 if data.getvalue():
53 return http_code, data.getvalue().decode()
54 else:
55 return http_code, None
56
57 def send_cmd(self, endpoint='', postfields_dict=None,
58 formfile=None, filename=None,
59 put_method=False, patch_method=False):
60 data = BytesIO()
61 curl_cmd = self._get_curl_cmd(endpoint)
62 if put_method:
63 curl_cmd.setopt(pycurl.CUSTOMREQUEST, "PUT")
64 elif patch_method:
65 curl_cmd.setopt(pycurl.CUSTOMREQUEST, "PATCH")
66 curl_cmd.setopt(pycurl.POST, 1)
67 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
68
69 if postfields_dict is not None:
70 jsondata = json.dumps(postfields_dict)
71 curl_cmd.setopt(pycurl.POSTFIELDS, jsondata)
72 elif formfile is not None:
73 curl_cmd.setopt(
74 pycurl.HTTPPOST,
75 [((formfile[0],
76 (pycurl.FORM_FILE,
77 formfile[1])))])
78 elif filename is not None:
79 with open(filename, 'r') as stream:
80 postdata=stream.read()
81 curl_cmd.setopt(pycurl.POSTFIELDS, postdata)
82
83 curl_cmd.perform()
84 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
85 curl_cmd.close()
86 if data.getvalue():
87 return http_code, data.getvalue().decode()
88 else:
89 return http_code, None
90
91 def post_cmd(self, endpoint='', postfields_dict=None,
92 formfile=None, filename=None):
93 return self.send_cmd(endpoint=endpoint,
94 postfields_dict=postfields_dict,
95 formfile=formfile,
96 filename=filename,
97 put_method=False, patch_method=False)
98
99 def put_cmd(self, endpoint='', postfields_dict=None,
100 formfile=None, filename=None):
101 return self.send_cmd(endpoint=endpoint,
102 postfields_dict=postfields_dict,
103 formfile=formfile,
104 filename=filename,
105 put_method=True, patch_method=False)
106
107 def patch_cmd(self, endpoint='', postfields_dict=None,
108 formfile=None, filename=None):
109 return self.send_cmd(endpoint=endpoint,
110 postfields_dict=postfields_dict,
111 formfile=formfile,
112 filename=filename,
113 put_method=False, patch_method=True)
114
115 def get2_cmd(self, endpoint):
116 data = BytesIO()
117 curl_cmd = self._get_curl_cmd(endpoint)
118 curl_cmd.setopt(pycurl.HTTPGET, 1)
119 curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
120 curl_cmd.perform()
121 http_code = curl_cmd.getinfo(pycurl.HTTP_CODE)
122 curl_cmd.close()
123 if data.getvalue():
124 return http_code, data.getvalue().decode()
125 return http_code, None
126