Skip to content

Commit 9864fcd

Browse files
committed
Add calls to make sure credentials are right shape, fail early on invalid connection, and add fallback for closing connection when function crashes.
1 parent 959562b commit 9864fcd

20 files changed

Lines changed: 189 additions & 11 deletions

R/get_acoustic_deployments.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ get_acoustic_deployments <- function(
5959
connection <-
6060
connect_to_etn(credentials$username, credentials$password)
6161

62+
# Ensure the connection is closed when the function exits, even when it fails.
63+
withr::defer(
64+
if (DBI::dbIsValid(connection)) {
65+
DBI::dbDisconnect(connection)
66+
}
67+
)
68+
6269
# Check if we can make a connection
6370
check_connection(connection)
6471

R/get_acoustic_detections.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ get_acoustic_detections <- function(credentials = list(
9696
# Create connection object
9797
connection <- connect_to_etn(credentials$username, credentials$password)
9898

99+
# Ensure the connection is closed when the function exits, even when it fails.
100+
withr::defer(
101+
if (DBI::dbIsValid(connection)) {
102+
DBI::dbDisconnect(connection)
103+
}
104+
)
105+
99106
# Check if we can make a connection
100107
check_connection(connection)
101108

R/get_acoustic_detections_page.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ get_acoustic_detections_page <- function(credentials = list(
6060
# Create connection object
6161
connection <- connect_to_etn(credentials$username, credentials$password)
6262

63+
# Ensure the connection is closed when the function exits, even when it fails.
64+
withr::defer(
65+
if (DBI::dbIsValid(connection)) {
66+
DBI::dbDisconnect(connection)
67+
}
68+
)
69+
6370
# Check if we can make a connection
6471
check_connection(connection)
6572

R/get_acoustic_projects.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ get_acoustic_projects <- function(credentials = list(
3737
connection <-
3838
connect_to_etn(credentials$username, credentials$password)
3939

40+
# Ensure the connection is closed when the function exits, even when it fails.
41+
withr::defer(
42+
if (DBI::dbIsValid(connection)) {
43+
DBI::dbDisconnect(connection)
44+
}
45+
)
46+
4047
# Check connection
4148
check_connection(connection)
4249

R/get_acoustic_receivers.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ get_acoustic_receivers <- function(credentials = list(
3737
status = NULL) {
3838
# Create connection object
3939
connection <- connect_to_etn(credentials$username, credentials$password)
40-
40+
41+
# Ensure the connection is closed when the function exits, even when it fails.
42+
withr::defer(
43+
if (DBI::dbIsValid(connection)) {
44+
DBI::dbDisconnect(connection)
45+
}
46+
)
4147
# Check connection
4248
check_connection(connection)
4349

R/get_animal_projects.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ get_animal_projects <- function(credentials = list(
3535

3636
# Create connection object
3737
connection <- connect_to_etn(credentials$username, credentials$password)
38-
38+
39+
# Ensure the connection is closed when the function exits, even when it fails.
40+
withr::defer(
41+
if (DBI::dbIsValid(connection)) {
42+
DBI::dbDisconnect(connection)
43+
}
44+
)
45+
3946
# Check connection
4047
check_connection(connection)
4148

R/get_animals.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ get_animals <- function(credentials = list(
5858

5959
# Create connection object
6060
connection <- connect_to_etn(credentials$username, credentials$password)
61-
61+
62+
# Ensure the connection is closed when the function exits, even when it fails.
63+
withr::defer(
64+
if (DBI::dbIsValid(connection)) {
65+
DBI::dbDisconnect(connection)
66+
}
67+
)
68+
6269
# Check connection
6370
check_connection(connection)
6471

R/get_archival_data_uuid.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ get_archival_data_uuid <- function(credentials = list(
2626
# Create connection object
2727
connection <- connect_to_etn(credentials$username, credentials$password)
2828

29+
# Ensure the connection is closed when the function exits, even when it fails.
30+
withr::defer(
31+
if (DBI::dbIsValid(connection)) {
32+
DBI::dbDisconnect(connection)
33+
}
34+
)
35+
2936
# Check connection
3037
check_connection(connection)
3138

R/get_cpod_projects.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ get_cpod_projects <- function(credentials = list(
3535

3636
# Create connection object
3737
connection <- connect_to_etn(credentials$username, credentials$password)
38-
38+
39+
# Ensure the connection is closed when the function exits, even when it fails.
40+
withr::defer(
41+
if (DBI::dbIsValid(connection)) {
42+
DBI::dbDisconnect(connection)
43+
}
44+
)
45+
3946
# Check connection
4047
check_connection(connection)
4148

R/get_tags.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ get_tags <- function(credentials = list(
5555
# Create connection object
5656
connection <- connect_to_etn(credentials$username, credentials$password)
5757

58+
# Ensure the connection is closed when the function exits, even when it fails.
59+
withr::defer(
60+
if (DBI::dbIsValid(connection)) {
61+
DBI::dbDisconnect(connection)
62+
}
63+
)
64+
5865
# Check connection
5966
check_connection(connection)
6067

0 commit comments

Comments
 (0)