Skip to content

Commit

Permalink
Refactoring and tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
rjambrecic committed Jun 19, 2024
1 parent 9414b1e commit b14d692
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 19 deletions.
36 changes: 18 additions & 18 deletions google_sheets/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,6 @@ def _get_sheet(service: Any, spreadsheet_id: str, range: str) -> Any:
return values


@asyncify # type: ignore[misc]
def list_sheets(service: Any) -> List[Dict[str, str]]:
# Call the Drive v3 API
results = (
service.files()
.list(
q="mimeType='application/vnd.google-apps.spreadsheet'",
pageSize=10,
fields="nextPageToken, files(id, name)",
)
.execute()
)
items = results.get("files", [])
return items # type: ignore[no-any-return]


@app.get("/sheet", description="Get data from a Google Sheet")
async def get_sheet(
user_id: Annotated[
Expand All @@ -137,14 +121,30 @@ async def get_sheet(
return values # type: ignore[no-any-return]


@app.get("/all", description="Get all sheets associated with the user")
@asyncify # type: ignore[misc]
def _get_files(service: Any) -> List[Dict[str, str]]:
# Call the Drive v3 API
results = (
service.files()
.list(
q="mimeType='application/vnd.google-apps.spreadsheet'",
pageSize=100, # The default value is 100
fields="nextPageToken, files(id, name)",
)
.execute()
)
items = results.get("files", [])
return items # type: ignore[no-any-return]


@app.get("/get-all-file-names", description="Get all sheets associated with the user")
async def get_all_file_names(
user_id: Annotated[
int, Query(description="The user ID for which the data is requested")
],
) -> Dict[str, str]:
service = await _build_service(user_id=user_id, service_name="drive", version="v3")
files: List[Dict[str, str]] = await list_sheets(service=service)
files: List[Dict[str, str]] = await _get_files(service=service)
# create dict where key is id and value is name
files_dict = {file["id"]: file["name"] for file in files}
return files_dict
67 changes: 66 additions & 1 deletion tests/app/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ def test_get_sheet(self) -> None:
assert response.status_code == 200
assert response.json() == excepted

def test_get_all_file_names(self) -> None:
with (
patch(
"google_sheets.app.load_user_credentials",
return_value={"refresh_token": "abcdf"},
) as mock_load_user_credentials,
patch(
"google_sheets.app._get_files",
return_value=[
{"id": "abc", "name": "file1"},
{"id": "def", "name": "file2"},
],
) as mock_get_files,
):
expected = {"abc": "file1", "def": "file2"}
response = client.get("/get-all-file-names?user_id=123")
mock_load_user_credentials.assert_called_once()
mock_get_files.assert_called_once()
assert response.status_code == 200
assert response.json() == expected

def test_openapi(self) -> None:
expected = {
"openapi": "3.1.0",
Expand Down Expand Up @@ -115,7 +136,51 @@ def test_openapi(self) -> None:
},
},
}
}
},
"/get-all-file-names": {
"get": {
"summary": "Get All File Names",
"description": "Get all sheets associated with the user",
"operationId": "get_all_file_names_get_all_file_names_get",
"parameters": [
{
"name": "user_id",
"in": "query",
"required": True,
"schema": {
"type": "integer",
"description": "The user ID for which the data is requested",
"title": "User Id",
},
"description": "The user ID for which the data is requested",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"additionalProperties": {"type": "string"},
"title": "Response Get All File Names Get All File Names Get",
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
},
"components": {
"schemas": {
Expand Down

0 comments on commit b14d692

Please sign in to comment.