1 # Copyright 2019 AT&T Intellectual Property. All rights reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from RequestsLibrary import RequestsLibrary
15 from robot.api import logger
16 from robot.api.deco import keyword
17 from robot.libraries.BuiltIn import BuiltIn
21 from ONAPLibrary.Base64Keywords import Base64Keywords
22 from ONAPLibrary.RequestsHelper import RequestsHelper
25 class BaseSDCKeywords(object):
26 """The main interface for interacting with SDC. It handles low level stuff like managing the http request library
27 and required fields. """
30 super(BaseSDCKeywords, self).__init__()
31 self.reqs = RequestsHelper()
32 self.builtin = BuiltIn()
35 def run_get_request(self, endpoint, data_path, user, accept="application/json", auth=None):
36 """Runs an SDC get request"""
37 resp = self.get_request(endpoint, data_path, user, accept, auth)
38 self.builtin.should_be_equal_as_strings(resp.status_code, "200")
42 def run_post_request(self, endpoint, data_path, data, user, accept="application/json", auth=None):
43 """Runs an SDC post request"""
44 return self.post_request(endpoint, data_path, data, user, files=None, accept=accept, auth=auth)
47 def run_post_files_request(self, endpoint, data_path, files, user, accept="application/json", auth=None):
48 """Runs an SDC post files request"""
49 return self.post_request(endpoint, data_path, files, user, files=None, accept=accept,
50 content_type="multipart/form-data", auth=auth)
53 def run_put_request(self, endpoint, data_path, data, user, accept="application/json", auth=None):
54 """Runs an SDC post request"""
55 return self.put_request(endpoint, data_path, data, user, accept, auth)
57 def get_request(self, endpoint, data_path, user, accept="application/json", auth=None):
58 """Runs an SDC get request"""
59 logger.info("Creating session" + endpoint)
60 RequestsLibrary().create_session("sdc", endpoint, auth=auth)
61 headers = self.reqs.create_headers(sdc_user_id=user, accept=accept)
62 resp = RequestsLibrary().get_request("sdc", data_path, headers=headers)
63 logger.info("Received response from sdc " + resp.text)
66 def post_request(self, endpoint, data_path, data, user, files=None, accept="application/json",
67 content_type="application/json", auth=None):
68 """Runs an SDC post request"""
69 logger.info("Creating session" + endpoint)
72 md5checksum = Base64Keywords().base64_encode(md5.hexdigest())
73 RequestsLibrary().create_session("sdc", endpoint, auth=auth)
74 headers = self.reqs.create_headers(user, accept=accept, content_type=content_type, md5=md5checksum)
75 resp = RequestsLibrary().post_request("sdc", data_path, files=files, data=data, headers=headers)
77 logger.info("Received response from sdc " + resp.text)
80 def put_request(self, endpoint, data_path, data, user, accept="application/json", auth=None):
81 """Runs an SDC post request"""
82 logger.info("Creating session" + endpoint)
83 RequestsLibrary().create_session("sdc", endpoint, auth=auth)
84 headers = self.reqs.create_headers(sdc_user_id=user, accept=accept)
85 resp = RequestsLibrary().put_request("sdc", data_path, data=data, headers=headers)
86 logger.info("Received response from sdc " + resp.text)
89 def delete_request(self, endpoint, data_path, data, user, accept="application/json", auth=None):
90 """Runs an SDC post request"""
91 logger.info("Creating session" + endpoint)
92 RequestsLibrary().create_session("sdc", endpoint, auth=auth)
93 headers = self.reqs.create_headers(sdc_user_id=user, accept=accept)
94 resp = RequestsLibrary().delete_request("sdc", data_path, data=data, headers=headers)
95 logger.info("Received response from sdc " + resp.text)