5
5
from datetime import datetime
6
6
from datetime import timedelta
7
7
from functools import total_ordering
8
+ from urllib .parse import urlparse
8
9
import os
9
10
import platform
10
11
import pyrfc3339
@@ -423,6 +424,8 @@ def __init__(self, hostname, username="admin", password="admin", verify=True):
423
424
self .acquisition = TimeSeriesSession (hostname , "/AQUARIUS/Acquisition/v2" , verify = verify )
424
425
self .provisioning = TimeSeriesSession (hostname , "/AQUARIUS/Provisioning/v1" , verify = verify )
425
426
427
+ self ._configure_reauthentication (username , password )
428
+
426
429
# Authenticate once
427
430
self .connect (username , password )
428
431
@@ -447,10 +450,47 @@ def connect(self, username, password):
447
450
self .acquisition .set_session_token (token )
448
451
self .provisioning .set_session_token (token )
449
452
453
+ return token
454
+
450
455
def disconnect (self ):
451
456
"""Destroys the authenticated session"""
452
457
self .publish .delete ('/session' )
453
458
459
+ def _configure_reauthentication (self , username , password ):
460
+ self ._username = username
461
+ self ._password = password
462
+ self ._reauthenticating = False
463
+ self ._reauthenticate_session = requests .Session ()
464
+
465
+ self .publish .hooks ['response' ].append (self ._reauthenticate )
466
+ self .acquisition .hooks ['response' ].append (self ._reauthenticate )
467
+ self .provisioning .hooks ['response' ].append (self ._reauthenticate )
468
+
469
+ def _reauthenticate (self , response , * args , ** kwargs ):
470
+ if response .status_code == 401 and not self ._reauthenticating :
471
+ self ._reauthenticating = True
472
+ token = self .connect (self ._username , self ._password )
473
+
474
+ response .request .headers .update ({"X-Authentication-Token" : token })
475
+
476
+ new_response = self ._reauthenticate_session .send (response .request , ** kwargs )
477
+ self ._reauthenticating = False
478
+
479
+ return new_response
480
+
481
+ def _create_authenticated_endpoint (self , root_path : str , verify = True ) -> TimeSeriesSession :
482
+ """
483
+ Creates an authenticated endpoint to something other than the public API surface.
484
+
485
+ :param root_path: The path to the endpoint on the AQTS app server
486
+ :param verify: The standard requests library certificate check
487
+ :return: An authenticated session for making requests to the end point
488
+ """
489
+ url = urlparse (self .publish .base_url )
490
+ session = TimeSeriesSession (f'{ url .scheme } ://{ url .hostname } ' , root_path , verify = verify )
491
+ session .set_session_token (self .publish .headers ['X-Authentication-Token' ])
492
+ return session
493
+
454
494
def isVersionLessThan (self , source_version , target_version = None ):
455
495
"""
456
496
Is the source version strictly less than the target version.
@@ -517,12 +557,11 @@ def getTimeSeriesUniqueId(self, timeSeriesIdentifier):
517
557
:param timeSeriesIdentifier: The identifier to lookup
518
558
:return: The unique ID of the series
519
559
"""
520
- parts = timeSeriesIdentifier .split ('@' )
521
-
522
- if len (parts ) < 2 :
560
+ match = re .search ('[^\\ \\ ]@(?P<location>.+)$' , timeSeriesIdentifier )
561
+ if not match :
523
562
return timeSeriesIdentifier
524
563
525
- location = parts [ 1 ]
564
+ location = match . group ( 'location' )
526
565
527
566
# Get the descriptions from the location
528
567
try :
@@ -541,12 +580,11 @@ def getTimeSeriesUniqueId(self, timeSeriesIdentifier):
541
580
542
581
def getLocationIdentifier (self , timeSeriesOrRatingModelIdentifier ):
543
582
"""Extracts the location identifier from a 'Parameter.Label@Location' time-series or rating model identifier"""
544
- parts = timeSeriesOrRatingModelIdentifier .split ('@' )
545
-
546
- if len (parts ) < 2 :
583
+ match = re .search ('[^\\ \\ ]@(?P<location>.+)$' , timeSeriesOrRatingModelIdentifier )
584
+ if not match :
547
585
raise LocationNotFoundException (timeSeriesOrRatingModelIdentifier )
548
586
549
- return parts [ 1 ]
587
+ return match . group ( 'location' )
550
588
551
589
def getLocationData (self , identifier ):
552
590
"""Gets the location data"""
0 commit comments