Skip to content

Commit 427dd64

Browse files
committed
Try to reauthenticate on a http status code 403
If we get a http status code 403, we should try to reauthenticate in case the session timed out. If we then still get the error, our user might miss the permissions for the requested action. Using a decorator to achieve this.
1 parent 28d7e72 commit 427dd64

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

dspace_rest_client/client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ def do_paginate(url, params):
158158
return decorated
159159
return decorator
160160

161+
@staticmethod
162+
def reauthenticate(func):
163+
@functools.wraps(func)
164+
def decorated(self, *args, reauthenticate=True, **kwargs):
165+
r = func(self, *args, **kwargs)
166+
if r is None:
167+
return r
168+
if r.status_code == 401 and reauthenticate:
169+
if self.authenticate():
170+
r = func(self, *args, **kwargs)
171+
return r
172+
return decorated
173+
161174
@staticmethod
162175
def refresh_csrf(func):
163176
@functools.wraps(func)
@@ -314,6 +327,7 @@ def api_get(self, url, params=None, data=None, headers=None):
314327
self.update_token(r)
315328
return r
316329

330+
@reauthenticate
317331
@refresh_csrf
318332
def api_post(self, url, params, json):
319333
"""
@@ -331,6 +345,7 @@ def api_post(self, url, params, json):
331345
self.update_token(r)
332346
return r
333347

348+
@reauthenticate
334349
@refresh_csrf
335350
def api_post_uri(self, url, params, uri_list):
336351
"""
@@ -348,6 +363,7 @@ def api_post_uri(self, url, params, uri_list):
348363
self.update_token(r)
349364
return r
350365

366+
@reauthenticate
351367
@refresh_csrf
352368
def api_put(self, url, params, json):
353369
"""
@@ -365,6 +381,7 @@ def api_put(self, url, params, json):
365381
self.update_token(r)
366382
return r
367383

384+
@reauthenticate
368385
@refresh_csrf
369386
def api_delete(self, url, params):
370387
"""
@@ -378,6 +395,7 @@ def api_delete(self, url, params):
378395
self.update_token(r)
379396
return r
380397

398+
@reauthenticate
381399
@refresh_csrf
382400
def api_patch(self, url, operation, path, value, params=None):
383401
"""

0 commit comments

Comments
 (0)