|
| 1 | +import json |
| 2 | +import requests |
| 3 | + |
| 4 | +from rich import print |
| 5 | + |
| 6 | +from contrast_api.models import * |
| 7 | +from contrast_api.request_handler import RequestHandler |
| 8 | + |
| 9 | +class BaseAPI: |
| 10 | + api_version = "api/ng" |
| 11 | + |
| 12 | + def __init__(self, session, api_config): |
| 13 | + self._session = session |
| 14 | + self._config = api_config |
| 15 | + |
| 16 | + self.hostname = self._config.teamserver_url |
| 17 | + self.requests = RequestHandler(session=self._session) |
| 18 | + |
| 19 | + @property |
| 20 | + def _base_url(self) -> str: |
| 21 | + base_url = self.hostname |
| 22 | + base_url += f"/{self.api_version}" if self.api_version else "" |
| 23 | + return base_url |
| 24 | + |
| 25 | + @property |
| 26 | + def current_org_uuid(self): |
| 27 | + if self._config.active_profile: |
| 28 | + return self._config.active_profile.org_uuid |
| 29 | + else: |
| 30 | + return self._config.default_profile.org_uuid |
| 31 | + |
| 32 | + def full_url(self, endpoint: str) -> str: |
| 33 | + return f"{self._base_url}/{endpoint}" |
| 34 | + |
| 35 | + def get(self, endpoint: str, skip_links: bool = False, expand: str = None, params: Dict = None) -> Response: |
| 36 | + if any([skip_links, expand, params]): |
| 37 | + params = params or {} |
| 38 | + expand = expand or "" |
| 39 | + if skip_links: |
| 40 | + expand = "skip_links," + expand |
| 41 | + if expand: |
| 42 | + params["expand"] = expand |
| 43 | + return self.requests.get(url=self.full_url(endpoint), params=params) |
| 44 | + |
| 45 | + def post(self, endpoint: str, skip_links: bool = False, expand: List = None, params: Dict = None, data: Dict = {}) -> Response: |
| 46 | + if any([skip_links, expand, params]): |
| 47 | + params = params or {} |
| 48 | + expand = expand or "" |
| 49 | + if skip_links: |
| 50 | + expand = "skip_links," + expand |
| 51 | + if expand: |
| 52 | + params["expand"] = expand |
| 53 | + return self.requests.post(url=self.full_url(endpoint), params=params, data=data) |
| 54 | + |
| 55 | + |
| 56 | +class APIHandler(BaseAPI): |
| 57 | + api_version = "" |
| 58 | + |
| 59 | + def get_current_user_profile(self): |
| 60 | + print("Getting current user profile") |
| 61 | + return self.get("profile/current-user") |
| 62 | + |
| 63 | + |
| 64 | +class PolicyHandler(BaseAPI): |
| 65 | + |
| 66 | + def get_org_policy(self) -> Policy: |
| 67 | + response = self.get(f"{self.current_org_uuid}/rules", skip_links=True) |
| 68 | + return Policy.from_response(response) |
| 69 | + |
| 70 | + def get_details_for_rule(self, rule_name: str) -> Rule: |
| 71 | + response = self.get(f"{self.current_org_uuid}/rules/{rule_name}") |
| 72 | + return Rule.from_response(response, _policy_handler=self) |
| 73 | + |
| 74 | + def reset_rule(self, rule_name: str) -> Response: |
| 75 | + response = self.post(f"{self.current_org_uuid}/rules/{rule_name}", data={"override": "false"}) |
| 76 | + return response |
| 77 | + |
| 78 | + def update_rule(self, rule_name: str, customizations: Dict) -> Response: |
| 79 | + response = self.post(f"{self.current_org_uuid}/rules/{rule_name}", data=customizations) |
| 80 | + return response |
| 81 | + |
| 82 | + |
| 83 | +class ProfileHandler(BaseAPI): |
| 84 | + |
| 85 | + def get_profile(self): |
| 86 | + response = self.get("profile", expand="email,preferences,login,signup,service_key,ip_address") |
| 87 | + return User.from_response(response) |
| 88 | + |
| 89 | + def get_current_user_profile(self): |
| 90 | + response = self.get("profile/current-user", expand="ip_address") |
| 91 | + return User.from_response(response) |
| 92 | + |
| 93 | + def get_organizations_in_profile(self): |
| 94 | + response = self.get("profile/organizations", expand="role") |
| 95 | + return Organization.from_response(response) |
| 96 | + |
| 97 | + def get_default_org_from_profile(self): |
| 98 | + response = self.get("profile/organizations/default") |
| 99 | + return Organization.from_response(response) |
| 100 | + |
| 101 | + def get_profile_user_roles_for_org(self): |
| 102 | + response = self.get(f"profile/organizations/{self.current_org_uuid}", expand="role") |
| 103 | + return Organization.from_response(response) |
0 commit comments