Skip to content

Commit

Permalink
Move reader_client into ReaderQueryHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinphippsstfc committed Sep 25, 2024
1 parent ade1049 commit b3807ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
26 changes: 12 additions & 14 deletions datagateway_api/src/datagateway_api/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,21 +340,19 @@ def get_data_with_filters(
if reader_query.is_query_eligible_for_reader_performance():
log.info("Query is eligible to be passed as reader acount")

Check warning on line 341 in datagateway_api/src/datagateway_api/icat/helpers.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/helpers.py#L341

Added line #L341 was not covered by tests
if reader_query.is_user_authorised_to_see_entity_id(client):
# TODO - make reader client reuseable
if client_pool:
reader_client = get_cached_client(None, client_pool)
else:
reader_client = ICATClient("datagateway_api")
reader_config = Config.config.datagateway_api.use_reader_for_performance
login_credentals = {
"username": reader_config.reader_username,
"password": reader_config.reader_password,
}
reader_client.login(reader_config.reader_mechanism, login_credentals)
reader_client = ReaderQueryHandler.reader_client
log.info("Query to be executed as reader account")
return execute_entity_query(
reader_client, entity_type, filters, aggregate=aggregate,
)
try:
results = execute_entity_query(

Check warning on line 346 in datagateway_api/src/datagateway_api/icat/helpers.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/helpers.py#L343-L346

Added lines #L343 - L346 were not covered by tests
reader_client, entity_type, filters, aggregate=aggregate,
)
except ICATSessionError:

Check warning on line 349 in datagateway_api/src/datagateway_api/icat/helpers.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/helpers.py#L349

Added line #L349 was not covered by tests
# re-login as reader and try the query again
reader_client = reader_query.create_reader_client()
results = execute_entity_query(

Check warning on line 352 in datagateway_api/src/datagateway_api/icat/helpers.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/helpers.py#L351-L352

Added lines #L351 - L352 were not covered by tests
reader_client, entity_type, filters, aggregate=aggregate,
)
return results

Check warning on line 355 in datagateway_api/src/datagateway_api/icat/helpers.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/helpers.py#L355

Added line #L355 was not covered by tests
else:
raise AuthenticationError(

Check warning on line 357 in datagateway_api/src/datagateway_api/icat/helpers.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/helpers.py#L357

Added line #L357 was not covered by tests
"Not authorised to access the"
Expand Down
18 changes: 18 additions & 0 deletions datagateway_api/src/datagateway_api/icat/reader_query_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datagateway_api.src.common.config import Config
from datagateway_api.src.common.filter_order_handler import FilterOrderHandler
from datagateway_api.src.datagateway_api.icat.filters import PythonICATWhereFilter
from datagateway_api.src.datagateway_api.icat.icat_client_pool import ICATClient
from datagateway_api.src.datagateway_api.icat.query import ICATQuery

Check warning on line 7 in datagateway_api/src/datagateway_api/icat/reader_query_handler.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/reader_query_handler.py#L3-L7

Added lines #L3 - L7 were not covered by tests

log = logging.getLogger()

Check warning on line 9 in datagateway_api/src/datagateway_api/icat/reader_query_handler.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/reader_query_handler.py#L9

Added line #L9 was not covered by tests
Expand All @@ -13,6 +14,8 @@ class ReaderQueryHandler:
# TODO - add docstrings
entity_filter_check = {"Datafile": "dataset.id", "Dataset": "investigation.id"}
entity_type_check = {"Datafile": "Dataset", "Dataset": "Investigation"}

Check warning on line 16 in datagateway_api/src/datagateway_api/icat/reader_query_handler.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/reader_query_handler.py#L15-L16

Added lines #L15 - L16 were not covered by tests
# keep a cached reader_client for faster queries
reader_client = None

Check warning on line 18 in datagateway_api/src/datagateway_api/icat/reader_query_handler.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/reader_query_handler.py#L18

Added line #L18 was not covered by tests

def __init__(self, entity_type, filters):
self.entity_type = entity_type
Expand All @@ -22,6 +25,21 @@ def __init__(self, entity_type, filters):
self.entity_type,
)
self.reader_query_eligible = self.check_eligibility()
self.create_reader_client()

Check warning on line 28 in datagateway_api/src/datagateway_api/icat/reader_query_handler.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/reader_query_handler.py#L27-L28

Added lines #L27 - L28 were not covered by tests

def create_reader_client(self):
log.info("Creating reader_client")
ReaderQueryHandler.reader_client = ICATClient("datagateway_api")
reader_config = Config.config.datagateway_api.use_reader_for_performance
login_credentals = {

Check warning on line 34 in datagateway_api/src/datagateway_api/icat/reader_query_handler.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/reader_query_handler.py#L30-L34

Added lines #L30 - L34 were not covered by tests
"username": reader_config.reader_username,
"password": reader_config.reader_password,
}
ReaderQueryHandler.reader_client.login(

Check warning on line 38 in datagateway_api/src/datagateway_api/icat/reader_query_handler.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/reader_query_handler.py#L38

Added line #L38 was not covered by tests
reader_config.reader_mechanism,
login_credentals,
)
return ReaderQueryHandler.reader_client

Check warning on line 42 in datagateway_api/src/datagateway_api/icat/reader_query_handler.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/reader_query_handler.py#L42

Added line #L42 was not covered by tests

def check_eligibility(self):
reader_config = Config.config.datagateway_api.use_reader_for_performance

Check warning on line 45 in datagateway_api/src/datagateway_api/icat/reader_query_handler.py

View check run for this annotation

Codecov / codecov/patch

datagateway_api/src/datagateway_api/icat/reader_query_handler.py#L44-L45

Added lines #L44 - L45 were not covered by tests
Expand Down

0 comments on commit b3807ae

Please sign in to comment.