Skip to content

Commit

Permalink
Feat/refactor incident helper with integrations (#616)
Browse files Browse the repository at this point in the history
* feat: support broader query pattern

* fix: handle fields param

* fix: return response and update docs

* fix: handle delegated user and 'fields' param

* feat: add Google Sheets module

* chore: lint

* fix: refactor using integrations instead of google_drive custom module
  • Loading branch information
gcharest authored Aug 7, 2024
1 parent 9135b21 commit d11a28f
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 104 deletions.
6 changes: 3 additions & 3 deletions app/integrations/google_workspace/google_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ def create(title: str) -> str:


@handle_google_api_errors
def batch_update(document_id: str, requests: list) -> None:
def batch_update(document_id: str, requests: list) -> dict:
"""Applies a list of updates to a document in Google Docs.
Args:
document_id (str): The id of the document to update.
requests (list): A list of update requests.
Returns:
None
dict: The response from the Google Docs API.
"""
execute_google_api_call(
return execute_google_api_call(
"docs",
"v1",
"documents",
Expand Down
39 changes: 23 additions & 16 deletions app/integrations/google_workspace/google_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@


@handle_google_api_errors
def add_metadata(file_id, key, value):
def add_metadata(file_id, key, value, delegated_user_email=None):
"""Add metadata to a file in Google Drive.
Args:
file_id (str): The file id of the file to add metadata to.
key (str): The key of the metadata to add.
value (str): The value of the metadata to add.
delegated_user_email (str, optional): The email address of the user to impersonate.
Returns:
dict: The updated file metadata.
Expand All @@ -30,6 +31,7 @@ def add_metadata(file_id, key, value):
"v3",
"files",
"update",
delegated_user_email=delegated_user_email,
fileId=file_id,
body={"appProperties": {key: value}},
fields="name, appProperties",
Expand All @@ -38,12 +40,13 @@ def add_metadata(file_id, key, value):


@handle_google_api_errors
def delete_metadata(file_id, key):
def delete_metadata(file_id, key, delegated_user_email=None):
"""Delete metadata from a file in Google Drive.
Args:
file_id (str): The file id of the file to delete metadata from.
key (str): The key of the metadata to delete.
delegated_user_email (str, optional): The email address of the user to impersonate.
Returns:
dict: The updated file metadata.
Expand All @@ -53,6 +56,7 @@ def delete_metadata(file_id, key):
"v3",
"files",
"update",
delegated_user_email=delegated_user_email,
fileId=file_id,
body={"appProperties": {key: None}},
fields="name, appProperties",
Expand Down Expand Up @@ -82,17 +86,19 @@ def list_metadata(file_id):


@handle_google_api_errors
def create_folder(name, parent_folder):
def create_folder(name, parent_folder, fields=None):
"""Create a new folder in Google Drive.
Args:
name (str): The name of the new folder.
parent_folder (str): The id of the parent folder.
fields (str, optional): The fields to include in the response.
Returns:
str: The id of the new folder.
dict: A File resource representing the new folder.
(https://developers.google.com/drive/api/reference/rest/v3/files#File)
"""
result = execute_google_api_call(
return execute_google_api_call(
"drive",
"v3",
"files",
Expand All @@ -103,14 +109,12 @@ def create_folder(name, parent_folder):
"parents": [parent_folder],
},
supportsAllDrives=True,
fields="id",
fields=fields,
)

return result["id"]


@handle_google_api_errors
def create_file_from_template(name, folder, template):
def create_file_from_template(name, folder, template, fields=None):
"""Create a new file in Google Drive from a template
(Docs, Sheets, Slides, Forms, or Sites.)
Expand All @@ -120,21 +124,19 @@ def create_file_from_template(name, folder, template):
template (str): The id of the template to use.
Returns:
str: The id of the new file.
dict: A File resource representing the new file with a mask of 'id'.
"""
result = execute_google_api_call(
return execute_google_api_call(
"drive",
"v3",
"files",
"copy",
fileId=template,
body={"name": name, "parents": [folder]},
supportsAllDrives=True,
fields="id",
fields=fields,
)

return result["id"]


@handle_google_api_errors
def create_file(name, folder, file_type):
Expand Down Expand Up @@ -215,15 +217,20 @@ def get_file_by_name(name, folder_id=None):


@handle_google_api_errors
def list_folders_in_folder(folder):
def list_folders_in_folder(folder, query=None):
"""List all folders in a folder in Google Drive.
Args:
folder (str): The id of the folder to list.
query (str, optional): A query to filter the folders.
Returns:
list: A list of folders in the folder.
"""
base_query = f"parents in '{folder}' and mimeType = 'application/vnd.google-apps.folder' and trashed=false"
if query:
base_query += f" and {query}"

return execute_google_api_call(
"drive",
"v3",
Expand All @@ -234,7 +241,7 @@ def list_folders_in_folder(folder):
supportsAllDrives=True,
includeItemsFromAllDrives=True,
corpora="user",
q=f"parents in '{folder}' and mimeType = 'application/vnd.google-apps.folder' and trashed=false",
q=base_query,
fields="files(id, name)",
)

Expand Down
7 changes: 4 additions & 3 deletions app/integrations/google_workspace/google_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def wrapper(*args, **kwargs):
result, unsupported_params = result
if unsupported_params:
logging.warning(
f"Unsupported parameters in '{func.__name__}' were filtered out: {', '.join(unsupported_params)}"
f"Unknown parameters in '{func.__name__}' were detected: {', '.join(unsupported_params)}"
)
return result
except HttpError as e:
Expand Down Expand Up @@ -152,7 +152,7 @@ def execute_google_api_call(
k: v for k, v in formatted_kwargs.items() if k in supported_params
}
unsupported_params = set(formatted_kwargs.keys()) - set(filtered_params.keys())

# filtered_params = kwargs
if paginate:
all_results = []
request = api_method(**filtered_params)
Expand All @@ -178,8 +178,9 @@ def get_google_api_command_parameters(resource_obj, method):
list: The names of the parameters for the API method, excluding non-parameter documentation.
"""
api_method = getattr(resource_obj, method)
# Add known parameters not documented in the docstring
parameter_names = ["fields"]

parameter_names = []
if hasattr(api_method, "__doc__") and api_method.__doc__:
parsing_parameters = False
doc_lines = api_method.__doc__.splitlines()
Expand Down
63 changes: 63 additions & 0 deletions app/integrations/google_workspace/sheets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Google Sheets API calls."""

from integrations.google_workspace.google_service import (
handle_google_api_errors,
execute_google_api_call,
)


@handle_google_api_errors
def get_values(
spreadsheetId: str, range: str | None = None, includeGridData=None, fields=None
):
"""Gets the values from a Google Sheet.
Args:
spreadsheetId (str): The id of the Google Sheet.
range (str, optional): The range of the values to retrieve.
includeGridData (bool, optional): Whether to include grid data.
fields (str, optional): The fields to include in the response.
Returns:
dict: The response from the Google Sheets API.
"""
return execute_google_api_call(
"sheets",
"v4",
"spreadsheets.values",
"get",
spreadsheetId=spreadsheetId,
range=range,
includeGridData=includeGridData,
fields=fields,
)


def batch_update_values(
spreadsheetId: str,
range: str,
values: list,
valueInputOption: str = "USER_ENTERED",
) -> dict:
"""Updates values in a Google Sheet.
Args:
spreadsheetId (str): The id of the Google Sheet.
range (str): The range to update.
values (list): The values to update.
valueInputOption (str, optional): The value input option.
Returns:
dict: The response from the Google Sheets API.
"""
return execute_google_api_call(
"sheets",
"v4",
"spreadsheets.values",
"batchUpdate",
spreadsheetId=spreadsheetId,
body={
"valueInputOption": valueInputOption,
"data": [{"range": range, "values": values}],
},
)
Loading

0 comments on commit d11a28f

Please sign in to comment.