-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to save API request to database (#125)
* Add function to save API request to database * edited logging * changed directories of database * added tests * added tests * solved linting issues
- Loading branch information
1 parent
11cbc1e
commit 7dd989f
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
Functions to read from the database and format. | ||
""" | ||
import logging | ||
|
||
from pvsite_datamodel.read.user import get_user_by_email as get_user_by_db | ||
from pvsite_datamodel.sqlmodels import APIRequestSQL | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def save_api_call_to_db(url, session, user=None): | ||
""" | ||
Save api call to database. | ||
""" | ||
url = str(url) | ||
if user is None: | ||
email = "unknown" | ||
else: | ||
email = user.email | ||
|
||
# get user from db | ||
user = get_user_by_db(session=session, email=email) | ||
# make api call | ||
logger.info(f"Saving api call ({url=}) to database for user {email}") | ||
api_request = APIRequestSQL(url=url, user=user) | ||
|
||
# commit to database | ||
session.add(api_request) | ||
session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,9 @@ | |
from sqlalchemy.exc import SQLAlchemyError | ||
from sqlalchemy.orm import Session | ||
|
||
from pvsite_datamodel.sqlmodels import ForecastSQL, ForecastValueSQL, GenerationSQL | ||
from pvsite_datamodel.read.user import get_user_by_email | ||
from pvsite_datamodel.sqlmodels import APIRequestSQL, ForecastSQL, ForecastValueSQL, GenerationSQL | ||
from pvsite_datamodel.write.database import save_api_call_to_db | ||
from pvsite_datamodel.write.forecast import insert_forecast_values | ||
from pvsite_datamodel.write.generation import insert_generation_values | ||
from pvsite_datamodel.write.user_and_site import ( | ||
|
@@ -244,3 +246,10 @@ def test_change_user_site_group(db_session): | |
|
||
assert user_site_group == site_group2.site_group_name | ||
assert user == "[email protected]" | ||
|
||
|
||
def test_save_api_call_to_db(db_session): | ||
user = get_user_by_email(session=db_session, email="[email protected]") | ||
url = "test" | ||
save_api_call_to_db(url=url, session=db_session, user=user) | ||
assert len(db_session.query(APIRequestSQL).all()) == 1 |