def __init__(self, **kwargs): self.s3_client = boto3.client( "s3", aws_access_key_id=get_setting("aws_access_key_id"), aws_secret_access_key=get_setting("aws_secret_access_key"), ) self.aws_bucket_name = get_setting("aws_bucket_name") super().__init__(**kwargs)
def get_files_in_single_folder(request_user, folder_id: int): """ Gets the files contained within a single folder from the VDR API Makes an http call to the External Service to get all the files from inside a single folder, sends the json response to the data parsing function. :param request_user: the user authenticated during the request :param folder_id: int :return: a data class for an array of files """ VDR_BASEURL = get_setting("remote_system_base_url") access_token = SocialToken.objects.get(account__user=request_user) url = f"{VDR_BASEURL}/files-in-folder/{folder_id}" headers = { "Authorization": f"Bearer {access_token}", "Accept": "application/json", } response = requests.get(url, headers=headers) if response.status_code != 200: result = VDRServiceError( message=response.text, status_code=response.status_code, endpoint=url, timestamp=datetime.datetime.now(), ) else: result = parse_get_files_in_single_folder(response.json()) return result
def get_single_site(request_user, id: int): """ Gets the detailed single site call from the VDR API Makes an http call to the External Collaborate Service to get a single site, sends the json response to the data parsing function. :param request_user, the remote_vdr site id :return: a data class for a single remote VDR site's details. """ VDR_BASEURL = get_setting("remote_system_base_url") site_id = id access_token = SocialToken.objects.get(account__user=request_user) url = f"{VDR_BASEURL}/sites/{site_id}" headers = { "Authorization": f"Bearer {access_token}", "Accept": "application/json", } response = requests.get(url, headers=headers) if response.status_code != 200: result = VDRServiceError( message=response.text, status_code=response.status_code, endpoint=url, timestamp=datetime.datetime.now(), ) else: result = parse_get_single_site(response.json()) return result
def permanently_delete_single_folder(request_user, folder_id: int): """ Deletes a folder out of it's site's deleted items folder and removes from the system entirely. Irreversible. Makes an http call to the External Service to permanently delete a single folder, :param request_user: the user authenticated during the request :param folder_id: int :return: the response object from the call to the http service """ VDR_BASEURL = get_setting("remote_system_base_url") access_token = SocialToken.objects.get(account__user=request_user) url = f"{VDR_BASEURL}/hard-delete-folder/{folder_id}" headers = { "Authorization": f"Bearer {access_token}", "Accept": "application/json", } response = requests.delete(url, headers=headers) if response.status_code != 200: result = VDRServiceError( message=response.text, status_code=response.status_code, endpoint=url, timestamp=datetime.datetime.now(), ) return result else: result = response return result
def shallow_delete_single_file(request_user, file_id: int): """ Moves a file to it's sites deleted items folder Makes an http call to the External Service to shallow delete a single file, :param request_user: the user authenticated during the request :param file_id: int :return: the response object from the call to the http service """ VDR_BASEURL = get_setting("remote_system_base_url") access_token = SocialToken.objects.get(account__user=request_user) url = f"{VDR_BASEURL}/soft-delete-file/{file_id}" headers = { "Authorization": f"Bearer {access_token}", "Accept": "application/json", } response = requests.delete(url, headers=headers) if response.status_code != 200: result = VDRServiceError( message=response.text, status_code=response.status_code, endpoint=url, timestamp=datetime.datetime.now(), ) return result else: result = response return result
def download_single_file(request_user, file_id: int): """ Downloads a file from the VDR API Makes an http call to the External Service to download a single file, :param request_user: the user authenticated during the request :param file_id: int :return: the response object from the call to the http service """ VDR_BASEURL = get_setting("remote_system_base_url") access_token = SocialToken.objects.get(account__user=request_user) url = f"{VDR_BASEURL}/download/{file_id}" headers = { "Authorization": f"Bearer {access_token}", "Accept": "application/json", } response = requests.get(url, headers=headers, stream=True) if response.status_code != 200: result = VDRServiceError( message=response.text, status_code=response.status_code, endpoint=url, timestamp=datetime.datetime.now(), ) else: result = response return result
class CustomAdapter(OAuth2Adapter): VDR_SERVER_OAUTH_BASE_URL = get_setting("remote_system_base_url") provider_id = CustomProvider.id access_token_url = f"{VDR_SERVER_OAUTH_BASE_URL}/token" profile_url = f"{VDR_SERVER_OAUTH_BASE_URL}/users/" # TODO: Look into the issues caused by the host. authorize_url = f"http://127.0.0.1:9000/auth" def complete_login(self, request, app, token, **kwargs): headers = { "Authorization": f"Bearer {token.token}", "Accept": "application/json", } useremail = kwargs["response"]["useremail"] resp = requests.get(self.profile_url + f"{useremail}", headers=headers) extra_data = resp.json() return self.get_provider().sociallogin_from_response( request, extra_data)
def get_all_sites(request_user, request_query_params=None): """ Gets all sites from the VDR API Makes an http call to the External Service to get all the site for the user, sends the json response to the data parsing function. :param request_user, any query params(optional) :return: a data class for a list of the remote VDR sites. """ VDR_BASEURL = get_setting("remote_system_base_url") if request_query_params: params = request_query_params.urlencode() else: params = "" access_token = SocialToken.objects.get(account__user=request_user) url = f"{VDR_BASEURL}/sites?{params}&limit=10" headers = { "Authorization": f"Bearer {access_token}", "Accept": "application/json", } response = requests.get(url, headers=headers) if response.status_code != 200: result = VDRServiceError( message=response.text, status_code=response.status_code, endpoint=url, timestamp=datetime.datetime.now(), ) else: result = parse_get_all_sites(response.json()) return result
def test_get_wrong_setting(remote_system_settings): with pytest.raises(FieldDoesNotExist): silly_setting = get_setting("foo_bar")
def test_get_settings(remote_system_settings): base_url = get_setting("remote_system_base_url") assert base_url == "http://system.com/system"