1 # -------------------------------------------------------------------------
2 # Copyright (c) 2015-2017 AT&T Intellectual Property
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # -------------------------------------------------------------------------
23 from osdf.config.base import osdf_config, creds_prefixes
24 from osdf.logging.osdf_logging import MH, debug_log
27 def get_rest_client(request_json, service):
28 """Get a RestClient based on request_json's callback URL and osdf_config's credentials based on service name
30 :param service: so or cm
31 :return: rc -- RestClient
33 callback_url = request_json["requestInfo"]["callbackUrl"]
34 prefix = creds_prefixes[service]
35 config = osdf_config.deployment
36 c_userid, c_passwd = config[prefix + "Username"], config[prefix + "Password"]
37 return RestClient(url=callback_url, userid=c_userid, passwd=c_passwd)
40 def json_from_file(file_name):
41 """Read a policy from a file"""
42 with open(file_name) as fid:
46 def yaml_from_file(file_name):
47 """Read a policy from a file"""
48 with open(file_name) as fid:
52 class RestClient(object):
53 """Simple REST Client that supports get/post and basic auth"""
55 def __init__(self, userid=None, passwd=None, log_func=None, url=None, timeout=None, headers=None,
56 method="POST", req_id=None):
57 self.auth = (userid, passwd) if userid and passwd else None
58 self.headers = headers if headers else {}
61 self.log_func = log_func
62 self.timeout = (30, 90) if timeout is None else timeout
65 def add_headers(self, headers):
66 self.headers.update(headers)
68 def request(self, url=None, method=None, asjson=True, ok_codes=(2, ),
69 raw_response=False, noresponse=False, timeout=None, **kwargs):
71 :param url: REST end point to query
72 :param method: GET or POST (default is None => self.method)
73 :param asjson: whether the expected response is in json format
74 :param ok_codes: expected codes (prefix matching -- e.g. can be (20, 21, 32) or (2, 3))
75 :param noresponse: If no response is expected (as long as response codes are OK)
76 :param raw_response: If we need just the raw response (e.g. conductor sends transaction IDs in headers)
77 :param timeout: Connection and read timeouts
78 :param kwargs: Other parameters
82 debug_log.debug("Requesting URL: {}".format(url or self.url))
84 debug_log.debug("Requesting URL: {} for request ID: {}".format(url or self.url, self.req_id))
86 res = requests.request(url=url or self.url, method=method or self.method,
87 auth=self.auth, headers=self.headers,
88 timeout=timeout or self.timeout, **kwargs)
91 self.log_func(MH.received_http_response(res))
93 res_code = str(res.status_code)
94 if not any(res_code.startswith(x) for x in map(str, ok_codes)):
95 raise res.raise_for_status()