1 # Copyright 2017 Sandvine
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
19 from requests
.auth
import HTTPBasicAuth
23 urllib3
.disable_warnings()
27 def __init__(self
, url
, user
="admin", password
="admin"):
30 self
._password
= password
31 self
._http
_header
= None
32 self
._logger
= logging
.getLogger("osmclient")
34 def set_http_header(self
, header
):
35 self
._http
_header
= header
37 def _get_requests_cmd(self
, endpoint
):
38 requests_cmd
= requests
.Request()
39 requests_cmd
.url
= self
._url
+ endpoint
40 requests_cmd
.auth
= HTTPBasicAuth(self
._user
, self
._password
)
42 requests_cmd
.headers
= self
._http
_header
45 def get_cmd(self
, endpoint
):
46 session_cmd
= requests
.Session()
47 requests_cmd
= self
._get
_requests
_cmd
(endpoint
)
48 requests_cmd
.method
= "GET"
50 "Request METHOD: {} URL: {}".format("GET", self
._url
+ endpoint
)
52 requests_cmd
= requests_cmd
.prepare()
53 resp
= session_cmd
.send(requests_cmd
, verify
=False)
54 http_code
= resp
.status_code
55 self
._logger
.info("Response HTTPCODE: {}".format(http_code
))
59 self
._logger
.debug("Response DATA: {}".format(json
.loads(data
.decode())))
60 return json
.loads(data
.decode())
63 def delete_cmd(self
, endpoint
):
64 session_cmd
= requests
.Session()
65 requests_cmd
= self
._get
_requests
_cmd
(endpoint
)
66 requests_cmd
.method
= "DELETE"
68 "Request METHOD: {} URL: {}".format("DELETE", self
._url
+ endpoint
)
70 requests_cmd
= requests_cmd
.prepare()
71 resp
= session_cmd
.send(requests_cmd
, verify
=False)
72 http_code
= resp
.status_code
73 self
._logger
.info("Response HTTPCODE: {}".format(http_code
))
77 self
._logger
.debug("Response DATA: {}".format(json
.loads(data
.decode())))
78 return json
.loads(data
.decode())
87 session_cmd
= requests
.Session()
88 requests_cmd
= self
._get
_requests
_cmd
(endpoint
)
89 requests_cmd
.method
= "POST"
91 if postfields_dict
is not None:
92 requests_cmd
.json
= json
.dumps(postfields_dict
)
94 if formfile
is not None:
95 requests_cmd
.files
= {formfile
[0]: formfile
[1]}
98 "Request METHOD: {} URL: {}".format("POST", self
._url
+ endpoint
)
100 requests_cmd
= requests_cmd
.prepare()
101 resp
= session_cmd
.send(requests_cmd
, verify
=False)
102 http_code
= resp
.status_code
103 self
._logger
.info("Response HTTPCODE: {}".format(http_code
))
107 self
._logger
.debug("Response DATA: {}".format(json
.loads(data
.decode())))
108 return json
.loads(data
.decode())