Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update API status checking method #247

Merged
merged 9 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions argopy/data_fetchers/argovis_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
dataset_ids = ['phy'] # First is default
# api_server = 'https://argovis.colorado.edu' # API root url
api_server = "https://argovisbeta02.colorado.edu" # v1 API, expires on March 31, 2023
api_server_check = (
api_server + "/selection/overview"
) # URL to check if the API is alive
api_server_check = {'url': api_server + "/selection/overview", 'keyword': 'numberOfProfiles'} # URL to check if the API is alive

log = logging.getLogger("argopy.argovis.data")

Expand Down
11 changes: 11 additions & 0 deletions argopy/tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
list_multiprofile_file_variables,
check_localftp,
isconnected,
urlhaskeyword,
isalive,
isAPIconnected,
erddap_ds_exists,
linear_interpolation_remap,
Expand Down Expand Up @@ -72,6 +74,15 @@ def test_isconnected():
assert isconnected(host="http://dummyhost") is False


def test_urlhaskeyword():
assert isinstance(urlhaskeyword('http://api.ifremer.fr/argopy/data/ARGO-FULL.json', 'label'), bool)


def test_isalive():
assert isinstance(isalive('https://github.com/euroargodev'), bool)
assert isinstance(isalive({'url': 'http://api.ifremer.fr/argopy/data/ARGO-FULL.json', 'keyword': 'label'}), bool)


def test_isAPIconnected():
assert isinstance(isAPIconnected(src="erddap", data=True), bool)
assert isinstance(isAPIconnected(src="erddap", data=False), bool)
Expand Down
41 changes: 40 additions & 1 deletion argopy/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,45 @@ def isconnected(host="https://www.ifremer.fr", maxtry=10):
return os.path.exists(host)


def urlhaskeyword(url: str = "", keyword: str = '', maxtry: int = 10):
""" Check if a keyword is in the content of a URL

Parameters
----------
url: str
keyword: str
maxtry: int, default: 10
Maximum number of host connections to try before returning False

Returns
-------
bool
"""
it = 0
while it < maxtry:
try:
with fsspec.open(url) as f:
data = f.read()
result = keyword in str(data)
it = maxtry
except Exception:
result, it = False, it + 1
return result


def isalive(api_server_check):
"""Check if a source API is alive or not

2 methods are available:
- Ping a URL
- Check for a keyword in a URL content
"""
if isinstance(api_server_check, dict):
return urlhaskeyword(url=api_server_check['url'], keyword=api_server_check['keyword'])
else:
return isconnected(api_server_check)


def isAPIconnected(src="erddap", data=True):
""" Check if a source API is alive or not

Expand All @@ -727,7 +766,7 @@ def isAPIconnected(src="erddap", data=True):
# This is a special case because the source here is a local folder
result = check_localftp(OPTIONS["local_ftp"])
else:
result = isconnected(list_src[src].api_server_check)
result = isalive(list_src[src].api_server_check)
return result
else:
raise InvalidFetcher
Expand Down
5 changes: 5 additions & 0 deletions docs/api-hidden.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
argopy.utilities.list_available_index_src
argopy.utilities.Chunker

argopy.utilities.isconnected
argopy.utilities.urlhaskeyword
argopy.utilities.isalive
argopy.utilities.isAPIconnected

argopy.utilities.ArgoNVSReferenceTables
argopy.utilities.ArgoNVSReferenceTables.valid_ref
argopy.utilities.ArgoNVSReferenceTables.all_tbl
Expand Down
29 changes: 29 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,32 @@ Argovis
argopy.data_fetchers.argovis_data.Fetch_wmo
argopy.data_fetchers.argovis_data.Fetch_box

Plotters
--------

.. autosummary::
:toctree: generated/

argopy.plot.dashboard
argopy.plot.plot_trajectory
argopy.plot.bar_plot
argopy.plot.open_sat_altim_report

Utilities
---------

.. autosummary::
:toctree: generated/

argopy.utilities.check_wmo
argopy.utilities.check_cyc
argopy.utilities.float_wmo
argopy.utilities.Registry
argopy.utilities.list_standard_variables
argopy.utilities.list_multiprofile_file_variables
argopy.utilities.Chunker

argopy.utilities.isconnected
argopy.utilities.urlhaskeyword
argopy.utilities.isalive
argopy.utilities.isAPIconnected
5 changes: 5 additions & 0 deletions docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ What's New

|pypi dwn| |conda dwn|


v0.1.13 (xx Mar. 2023)
----------------------

Expand All @@ -25,6 +26,10 @@ v0.1.13 (xx Mar. 2023)
df = deployment.to_dataframe()
deployment.status_code

**Internals**

- New method to check status of web API: now allows for a keyword check rather than a simple url ping. This comes with 2 new utilities functions :meth:`utilities.urlhaskeyword` and :meth:`utilities.isalive`. (:pr:`247`) by `G. Maze <http://www.github.com/gmaze>`_.


v0.1.12 (16 May 2022)
----------------------
Expand Down