77from bs4 import BeautifulSoup
88
99from itk_dev_shared_components .graph .authentication import GraphAccess
10+ from itk_dev_shared_components .graph .common import get_request
1011
1112
1213@dataclass
@@ -67,7 +68,7 @@ def get_emails_from_folder(user: str, folder_path: str, graph_access: GraphAcces
6768
6869 endpoint = f"https://graph.microsoft.com/v1.0/users/{ user } /mailFolders/{ folder_id } /messages?$top=1000"
6970
70- response = _get_request (endpoint , graph_access )
71+ response = get_request (endpoint , graph_access )
7172 emails_raw = response .json ()['value' ]
7273
7374 return _unpack_email_response (user , emails_raw )
@@ -84,7 +85,7 @@ def get_email_as_mime(email: Email, graph_access: GraphAccess) -> io.BytesIO:
8485 io.BytesIO: A file-like object of the MIME file.
8586 """
8687 endpoint = f"https://graph.microsoft.com/v1.0/users/{ email .user } /messages/{ email .id } /$value"
87- response = _get_request (endpoint , graph_access )
88+ response = get_request (endpoint , graph_access )
8889 data = response .content
8990 return io .BytesIO (data )
9091
@@ -113,15 +114,15 @@ def get_folder_id_from_path(user: str, folder_path: str, graph_access: GraphAcce
113114
114115 # Get main folder
115116 endpoint = f"https://graph.microsoft.com/v1.0/users/{ user } /mailFolders"
116- response = _get_request (endpoint , graph_access ).json ()
117+ response = get_request (endpoint , graph_access ).json ()
117118 folder_id = _find_folder (response , main_folder )
118119 if folder_id is None :
119120 raise ValueError (f"Top level folder '{ main_folder } ' was not found for user '{ user } '." )
120121
121122 # Get child folders
122123 for child_folder in child_folders :
123124 endpoint = f"https://graph.microsoft.com/v1.0/users/{ user } /mailFolders/{ folder_id } /childFolders"
124- response = _get_request (endpoint , graph_access ).json ()
125+ response = get_request (endpoint , graph_access ).json ()
125126 folder_id = _find_folder (response , child_folder )
126127 if folder_id is None :
127128 raise ValueError (f"Child folder '{ child_folder } ' not found under '{ main_folder } ' for user '{ user } '." )
@@ -141,7 +142,7 @@ def list_email_attachments(email: Email, graph_access: GraphAccess) -> tuple[Att
141142 tuple[Attachment]: A tuple of Attachment objects describing the attachments.
142143 """
143144 endpoint = f"https://graph.microsoft.com/v1.0/users/{ email .user } /messages/{ email .id } /attachments?$select=name,size,id"
144- response = _get_request (endpoint , graph_access ).json ()
145+ response = get_request (endpoint , graph_access ).json ()
145146
146147 attachments = []
147148 for att in response ['value' ]:
@@ -162,7 +163,7 @@ def get_attachment_data(attachment: Attachment, graph_access: GraphAccess) -> io
162163 """
163164 email = attachment .email
164165 endpoint = f"https://graph.microsoft.com/v1.0/users/{ email .user } /messages/{ email .id } /attachments/{ attachment .id } /$value"
165- response = _get_request (endpoint , graph_access )
166+ response = get_request (endpoint , graph_access )
166167 data_bytes = response .content
167168 return io .BytesIO (data_bytes )
168169
@@ -287,31 +288,3 @@ def _unpack_email_response(user: str, emails_raw: list[dict[str, str]]) -> tuple
287288 )
288289
289290 return tuple (emails )
290-
291-
292- def _get_request (endpoint : str , graph_access : GraphAccess ) -> requests .models .Response :
293- """Sends a get request to the given Graph endpoint using the GraphAccess
294- and returns the json object of the response.
295-
296- Args:
297- endpoint: The URL of the Graph endpoint.
298- graph_access: The GraphAccess object used to authenticate.
299- timeout: Timeout in seconds of the HTTP request. Defaults to 10.
300-
301- Returns:
302- Response: The response object of the GET request.
303-
304- Raises:
305- HTTPError: Any errors raised while performing GET request.
306- """
307- token = graph_access .get_access_token ()
308- headers = {'Authorization' : f"Bearer { token } " }
309-
310- response = requests .get (
311- endpoint ,
312- headers = headers ,
313- timeout = 30
314- )
315- response .raise_for_status ()
316-
317- return response
0 commit comments