Skip to content

Commit 7b111c5

Browse files
karandeep-joharKarandeep Johar
andauthored
Add Close() method to _DropboxTransport to cleanup any network resources (#207)
* Add Close() method to _DropboxTransport to cleanup any network resources * Added contextManager * lint fixes Co-authored-by: Karandeep Johar <kjohar@dropbox.com>
1 parent 51b8d3a commit 7b111c5

File tree

6 files changed

+43
-28
lines changed

6 files changed

+43
-28
lines changed

dropbox/dropbox.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,19 @@ def with_path_root(self, path_root):
663663
headers=new_headers
664664
)
665665

666+
def close(self):
667+
"""
668+
Cleans up all resources like the request session/network connection.
669+
"""
670+
self._session.close()
671+
672+
def __enter__(self):
673+
return self
674+
675+
def __exit__(self, *args):
676+
self.close()
677+
678+
666679
class Dropbox(_DropboxTransport, DropboxBase):
667680
"""
668681
Use this class to make requests to the Dropbox API using a user's access

example/back-up-and-restore/backup-and-restore-example.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,24 @@ def select_revision():
7676

7777
# Create an instance of a Dropbox class, which can make requests to the API.
7878
print("Creating a Dropbox object...")
79-
dbx = dropbox.Dropbox(TOKEN)
79+
with dropbox.Dropbox(TOKEN) as dbx:
8080

81-
# Check that the access token is valid
82-
try:
83-
dbx.users_get_current_account()
84-
except AuthError:
85-
sys.exit("ERROR: Invalid access token; try re-generating an "
86-
"access token from the app console on the web.")
81+
# Check that the access token is valid
82+
try:
83+
dbx.users_get_current_account()
84+
except AuthError:
85+
sys.exit("ERROR: Invalid access token; try re-generating an "
86+
"access token from the app console on the web.")
8787

88-
# Create a backup of the current settings file
89-
backup()
88+
# Create a backup of the current settings file
89+
backup()
9090

91-
# Change the user's file, create another backup
92-
change_local_file("updated")
93-
backup()
91+
# Change the user's file, create another backup
92+
change_local_file(b"updated")
93+
backup()
9494

95-
# Restore the local and Dropbox files to a certain revision
96-
to_rev = select_revision()
97-
restore(to_rev)
95+
# Restore the local and Dropbox files to a certain revision
96+
to_rev = select_revision()
97+
restore(to_rev)
9898

99-
print("Done!")
99+
print("Done!")

example/oauth/commandline-oauth-pkce.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
print('Error: %s' % (e,))
2727
exit(1)
2828

29-
dbx = dropbox.Dropbox(oauth2_refresh_token=oauth_result.refresh_token, app_key=APP_KEY)
30-
dbx.users_get_current_account()
31-
print("Successfully set up client!")
29+
with dropbox.Dropbox(oauth2_refresh_token=oauth_result.refresh_token, app_key=APP_KEY) as dbx:
30+
dbx.users_get_current_account()
31+
print("Successfully set up client!")

example/oauth/commandline-oauth-scopes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@
8080
print('Error: %s' % (e,))
8181
exit(1)
8282

83-
dbx = dropbox.Dropbox(oauth2_access_token=oauth_result.access_token,
84-
oauth2_access_token_expiration=oauth_result.expires_at,
85-
oauth2_refresh_token=oauth_result.refresh_token,
86-
app_key=APP_KEY,
87-
app_secret=APP_SECRET)
88-
print("Successfully set up client!")
83+
with dropbox.Dropbox(oauth2_access_token=oauth_result.access_token,
84+
oauth2_access_token_expiration=oauth_result.expires_at,
85+
oauth2_refresh_token=oauth_result.refresh_token,
86+
app_key=APP_KEY,
87+
app_secret=APP_SECRET):
88+
print("Successfully set up client!")

example/oauth/commandline-oauth.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
print('Error: %s' % (e,))
2525
exit(1)
2626

27-
dbx = dropbox.Dropbox(oauth2_access_token=oauth_result.access_token)
28-
dbx.users_get_current_account()
29-
print("Successfully set up client!")
27+
with dropbox.Dropbox(oauth2_access_token=oauth_result.access_token) as dbx:
28+
dbx.users_get_current_account()
29+
print("Successfully set up client!")

example/updown.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def main():
122122
print('OK, skipping directory:', name)
123123
dirs[:] = keep
124124

125+
dbx.close()
126+
125127
def list_folder(dbx, folder, subfolder):
126128
"""List a folder.
127129

0 commit comments

Comments
 (0)