Skip to content

Commit

Permalink
Modify how HCA cache is cleared (#431)
Browse files Browse the repository at this point in the history
* Modify how HCA cache is cleared

- Changed refresh_swagger to clear_cache
- Fixed typo in clear_cache documentation
- Adding clear-hca-cache to CLI to clear the cache of all SwaggerClients subclasses.
- Updated tests and tutorials.
  • Loading branch information
Bento007 committed Aug 26, 2019
1 parent 8af1a66 commit 7a6f774
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
7 changes: 7 additions & 0 deletions hca/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@

from .config import HCAConfig, get_config, logger
from . import dss, upload, query


def clear_hca_cache(args):
"""Clear the cached HCA API definitions. This can help resolve errors communicating with the API."""
from hca.util import SwaggerClient
for swagger_client in SwaggerClient.__subclasses__():
swagger_client().clear_cache()
2 changes: 2 additions & 0 deletions hca/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .upload import cli as upload_cli
from .query import cli as query_cli
from .util.compat import USING_PYTHON2
from . import clear_hca_cache
from . import logger, get_config


Expand Down Expand Up @@ -94,6 +95,7 @@ def get_parser(help_menu=False):
parser.add_argument("--log-level", default=get_config().get("log_level"),
help=str([logging.getLevelName(i) for i in range(10, 60, 10)]),
choices={logging.getLevelName(i) for i in range(10, 60, 10)})
parser.add_parser_func(clear_hca_cache, help=clear_hca_cache.__doc__)

def help(args):
parser.print_help()
Expand Down
6 changes: 3 additions & 3 deletions hca/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def __init__(self, config=None, swagger_url=None, **session_kwargs):
else:
self.__class__.__doc__ = _md2rst(self.swagger_spec["info"]["description"])
self.methods = {}
self.commands = [self.login, self.logout, self.refresh_swagger]
self.commands = [self.login, self.logout]
self.http_paths = collections.defaultdict(dict)
if "openapi" in self.swagger_spec:
server = self.swagger_spec["servers"][0]
Expand Down Expand Up @@ -314,9 +314,9 @@ def _get_swagger_filename(self, swagger_url):
swagger_filename = os.path.join(self.config.user_config_dir, swagger_filename)
return swagger_filename

def refresh_swagger(self):
def clear_cache(self):
"""
Manually refresh the swagger document. This can help resolve errors communicate with the API.
Clear the cached API definitions for a component. This can help resolve errors communicating with the API.
"""
try:
os.remove(self._get_swagger_filename(self.swagger_url))
Expand Down
6 changes: 3 additions & 3 deletions test/integration/dss/test_dss_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,14 @@ def test_get_bundle_iteration_pagination(self, limit=128):
if ix > limit:
break

def test_refresh_swagger(self):
"""Testing refresh_swagger by comparing the creation date of the old swagger with the refreshed swagger that
def test_clear_cache(self):
"""Testing clear_cache by comparing the creation date of the old swagger with the refreshed swagger that
replaces it"""
client = hca.dss.DSSClient()
swagger_filename = client._get_swagger_filename(client.swagger_url)
self.assertTrue(os.path.isfile(swagger_filename), "Pass if file exists initially")
old_swagger = datetime.datetime.fromtimestamp(os.path.getmtime(swagger_filename))
client.refresh_swagger()
client.clear_cache()
new_swagger = datetime.datetime.fromtimestamp(os.path.getmtime(swagger_filename))
self.assertGreater(new_swagger, old_swagger)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

dss = DSSClient()

dss.refresh_swagger()
dss.clear_cache()
2 changes: 1 addition & 1 deletion test/tutorial/scripts/tutorial/get_file_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
dss.login(access_token=access_token)

# Refreshes Swagger document. Can help resolve errors communicate with the API.
dss.refresh_swagger()
dss.clear_cache()

# JSON query and returns matching bundle identifiers.
for results in dss.post_search.iterate(replica="aws", es_query={}):
Expand Down

0 comments on commit 7a6f774

Please sign in to comment.