refacotr out headers so they are common across ecomp
[testsuite/python-testing-utils.git] / robotframework-onap / ONAPLibrary / BaseSDCKeywords.py
1 # Copyright 2019 AT&T Intellectual Property. All rights reserved.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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
18
19 import hashlib
20
21 from ONAPLibrary.Base64Keywords import Base64Keywords
22 from ONAPLibrary.RequestsHelper import RequestsHelper
23
24
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. """
28
29     def __init__(self):
30         super(BaseSDCKeywords, self).__init__()
31         self.reqs = RequestsHelper()
32         self.builtin = BuiltIn()
33
34     @keyword
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")
39         return resp
40
41     @keyword
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)
45
46     @keyword
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)
51
52     @keyword
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)
56
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)
64         return resp
65
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)
70         md5 = hashlib.md5()
71         md5.update(data)
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)
76
77         logger.info("Received response from sdc " + resp.text)
78         return resp
79
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)
87         return resp
88
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)
96         return resp