Skip to content

Commit

Permalink
Better default handling + auto lookup auth format
Browse files Browse the repository at this point in the history
  • Loading branch information
ceesem committed Nov 20, 2023
1 parent 4e20767 commit 415786c
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions caveclient/jsonservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,33 @@ def ngl_url(self):
def ngl_url(self, new_ngl_url):
self._ngl_url = new_ngl_url

def get_neuroglancer_info(self, ngl_url):
def get_neuroglancer_info(self, ngl_url=None):
"""Get the info field from a Neuroglancer deployment
Parameters
----------
ngl_url : str
URL to a Neuroglancer deployment
ngl_url : str (optional)
URL to a Neuroglancer deployment.
If None, defaults to the value for the datastack or the client.
Returns
-------
dict
JSON-formatted info field from the Neuroglancer deployment
"""
url_mapping"ngl_url"] = ngl_url
if ngl_url is None:
ngl_url = self.ngl_url

url_mapping = self.default_url_mapping
url_mapping["ngl_url"] = ngl_url
url = ngl_endpoints_common.get('get_info').format_map(url_mapping)
response = self.session.get(url)
if response.status_code == 404:
return {}
handle_response(response, as_json=False)
return json.loads(response.content)



def get_state_json(self, state_id):
"""Download a Neuroglancer JSON state
Expand Down Expand Up @@ -215,6 +221,7 @@ def upload_state_json(self, json_state, state_id=None, timestamp=None):
response_re = re.search(".*\/(\d+)", str(response.content))
return int(response_re.groups()[0])


def build_neuroglancer_url(self, state_id, ngl_url=None, target_site=None):
"""Build a URL for a Neuroglancer deployment that will automatically retrieve specified state.
If the datastack is specified, this is prepopulated from the info file field "viewer_site".
Expand All @@ -227,10 +234,9 @@ def build_neuroglancer_url(self, state_id, ngl_url=None, target_site=None):
ngl_url : str
Base url of a neuroglancer deployment. If None, defaults to the value for the datastack or the client.
If no value is found, only the URL to the JSON state is returned.
target_site : str (optional)
target_site : 'seunglab' or 'cave-explorer' or 'mainline' or None
Set this to 'seunglab' for a seunglab deployment, or 'cave-explorer'/'mainline' for a google main branch deployment.
If none, defaults to seunglab.
If None, checks the info field of the neuroglancer endpoint to determine which to use.
Returns
-------
Expand All @@ -239,8 +245,12 @@ def build_neuroglancer_url(self, state_id, ngl_url=None, target_site=None):
"""
if ngl_url is None:
ngl_url = self.ngl_url
if target_site is None:
target_site = "seunglab"
if target_site is None and ngl_url is not None:
ngl_info = self.get_neuroglancer_info(ngl_url)
if len(ngl_info) > 0:
target_site = 'cave-explorer'
else:
target_site = "seunglab"

if ngl_url is None:
ngl_url = ""
Expand Down

0 comments on commit 415786c

Please sign in to comment.