def add_query_parameter(self, name, value): """ Add a query parameter to the HttpRequest. Args: name (string): The name of the query parameter. value (string): The value of the query parameter. """ self.query_url = APIHelper.append_url_with_query_parameters(self.query_url, {name:value}) self.query_url = APIHelper.clean_url(self.query_url)
def update(self, entry_id, body): """Does a PUT request to /v1.1/whitelists/geolocations/{entry_id}/update. Update a Geo Location in the Whitelist Args: entry_id (string): a unique identifier for the Geo Location; opaque but likely a GUID body (GeoLocation): TODO: type description here. Example: Returns: UpdateResponse1: Response from the API. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ # Prepare query URL _url_path = '/v1.1/whitelists/geolocations/{entry_id}/update' _url_path = APIHelper.append_url_with_template_parameters( _url_path, {'entry_id': entry_id}) _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = { 'accept': 'application/json', 'content-type': 'application/json; charset=utf-8' } # Prepare and execute request _request = self.http_client.put( _query_url, headers=_headers, parameters=APIHelper.json_serialize(body)) CustomAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 400: raise ReturnException( 'Unexpected error in API call. See HTTP response body for details.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize(_context.response.raw_body, UpdateResponse1.from_dictionary)
def register_new_user(self, body): """Does a POST request to /v1.1/register. ONLY AVAILABLE when Bouncer is started with the `--testing` parameter. Register a new user with this instance of Bouncer. Args: body (RegisterNewUserRequest): TODO: type description here. Example: Returns: RegisterNewUserResponse: Response from the API. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ # Prepare query URL _url_path = '/v1.1/register' _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = { 'accept': 'application/json', 'content-type': 'application/json; charset=utf-8' } # Prepare and execute request _request = self.http_client.post( _query_url, headers=_headers, parameters=APIHelper.json_serialize(body)) CustomAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 400: raise ReturnException( 'Unexpected error in API call. See HTTP response body for details.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize( _context.response.raw_body, RegisterNewUserResponse.from_dictionary)
def create(self, body): """Does a POST request to /v1.1/whitelists/geolocations/create. Create a Geo Location in the Whitelist. When POSTed-to this endpoint, Bouncer scans `geolist.txt` for any IPs matching the Country Code (CC) in the POSTed object and, for each: Bouncer will create a new ipaddress in this list (black- or white-list). Args: body (GeoLocation): TODO: type description here. Example: Returns: CreateResponse1: Response from the API. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ # Prepare query URL _url_path = '/v1.1/whitelists/geolocations/create' _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = { 'accept': 'application/json', 'content-type': 'application/json; charset=utf-8' } # Prepare and execute request _request = self.http_client.post( _query_url, headers=_headers, parameters=APIHelper.json_serialize(body)) CustomAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 400: raise ReturnException( 'Unexpected error in API call. See HTTP response body for details.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize(_context.response.raw_body, CreateResponse1.from_dictionary)
def search(self, search_filter): """Does a GET request to /v1.1/blacklists/ipaddresses/filter/{search_filter}. Search for IP Addresses in the Blacklist Args: search_filter (string): an comma-separated lsit of IP addresses in CIDR format (`192.168.100.14/24`) except with `/` replaced by `+` Returns: SearchResponse: Response from the API. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ # Prepare query URL _url_path = '/v1.1/blacklists/ipaddresses/filter/{search_filter}' _url_path = APIHelper.append_url_with_template_parameters(_url_path, { 'search_filter': search_filter }) _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = { 'accept': 'application/json' } # Prepare and execute request _request = self.http_client.get(_query_url, headers=_headers) CustomAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 400: raise ReturnException('Unexpected error in API call. See HTTP response body for details.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize(_context.response.raw_body, SearchResponse.from_dictionary)
def execute_request(self, request, binary=False): """Executes an HttpRequest. Args: request (HttpRequest): The HttpRequest to execute. binary (bool): A flag which should be set to True if a binary response is expected. Returns: HttpContext: The HttpContext of the request. It contains, both, the request itself and the HttpResponse object. """ # Invoke the on before request HttpCallBack if specified if self.http_call_back != None: self.http_call_back.on_before_request(request) # Add global headers to request request.headers = APIHelper.merge_dicts(self.global_headers, request.headers) # Invoke the API call to fetch the response. func = self.http_client.execute_as_binary if binary else self.http_client.execute_as_string response = func(request) context = HttpContext(request, response) # Invoke the on after response HttpCallBack if specified if self.http_call_back != None: self.http_call_back.on_after_response(context) return context
def get_details(self, entry_id): """Does a GET request to /v1.1/blacklists/ipaddresses/{entry_id}. Get Details of an IP Address Entry in the Blacklist Args: entry_id (string): a unique identifier for the IP Address; opaque but likely a GUID Returns: GetDetailsResponse: Response from the API. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ # Prepare query URL _url_path = '/v1.1/blacklists/ipaddresses/{entry_id}' _url_path = APIHelper.append_url_with_template_parameters(_url_path, { 'entry_id': entry_id }) _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = { 'accept': 'application/json' } # Prepare and execute request _request = self.http_client.get(_query_url, headers=_headers) CustomAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 400: raise ReturnException('Unexpected error in API call. See HTTP response body for details.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize(_context.response.raw_body, GetDetailsResponse.from_dictionary)
def test_for_list_membership(self, ip_address): """Does a GET request to /v1.1/check/{ip_address}. Check if an IP Address is Already White- or Black-Listed Args: ip_address (string): the IP address to check Returns: TestForListMembershipResponse: Response from the API. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ # Prepare query URL _url_path = '/v1.1/check/{ip_address}' _url_path = APIHelper.append_url_with_template_parameters( _url_path, {'ip_address': ip_address}) _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = {'accept': 'application/json'} # Prepare and execute request _request = self.http_client.get(_query_url, headers=_headers) CustomAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 400: raise ReturnException( 'Unexpected error in API call. See HTTP response body for details.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize( _context.response.raw_body, TestForListMembershipResponse.from_dictionary)
def all_contents(self): """Does a GET request to /v1.1/whitelists. This will list the entire contents of the Whitelist including both IP Addresses and Geo Locations. Returns: AllContentsResponse: Response from the API. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ # Prepare query URL _url_path = '/v1.1/whitelists' _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers _headers = {'accept': 'application/json'} # Prepare and execute request _request = self.http_client.get(_query_url, headers=_headers) CustomAuth.apply(_request) _context = self.execute_request(_request) # Endpoint and global error handling using HTTP status codes. if _context.response.status_code == 400: raise ReturnException( 'Unexpected error in API call. See HTTP response body for details.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize(_context.response.raw_body, AllContentsResponse.from_dictionary)
def __init__(self, reason, context): """Constructor for the ReturnException class Args: reason (string): The reason (or error message) for the Exception to be raised. context (HttpContext): The HttpContext of the API call. """ super(ReturnException, self).__init__(reason, context) dictionary = APIHelper.json_deserialize(self.context.response.raw_body) if isinstance(dictionary, dict): self.unbox(dictionary)
def get_base_uri(cls, server=Server.DEFAULT): """Generates the appropriate base URI for the environment and the server. Args: server (Configuration.Server): The server enum for which the base URI is required. Returns: String: The base URI. """ parameters = { "defaultHost": cls.default_host, } return APIHelper.append_url_with_template_parameters( cls.environments[cls.environment][server], parameters, False)