CasJobsUrllib3Client#

class astropathdb.db_io.dbconnect.CasJobsUrllib3Client#

Bases: CasJobsTask

Execute queries against CasJobs RESTAPI using urllib3.

We optimize request to allow for use with urllib3 and allow submission of a list of queries to be passed in parallel. This class will keep the connection pool open so that requests do not need a new connection.

Danger

As noted throughout this module, this was not designed as a user facing module or as a replacement for the SciScript-Python.

CasJobsUrllib3Client Attributes#

http

Shared connection pool so requests reuse open connections.

CasJobsUrllib3Client Methods#

execute_query_single(sql[, context, _format, _futures])

Execute one synchronous SQL query in a CasJobs database context.

execute_query_futures(sql[, context, _format, nthreads])

Execute SQL queries in parallel in a CasJobs database context.

execute_query(sql, *args, **kwargs)

Execute one SQL query, or a sequence of queries in parallel.

get_taskname(task_id)

Get the CasJobs task name for a local or SciServer compute client.

set_headers(accept_format)

Set the token and 'Accept' in _headers for next requests.

format_data(sql, task_id)

Format sql and task_id as a JSON request body.

CasJobsUrllib3Client References#

Attribute Reference#

attribute http#

Shared connection pool so requests reuse open connections.

Holds up to 30 connections and blocks when all are in use. ‘Service Unavailable’ (503) responses to POST requests are retried up to 15 times with jittered backoff. Verifies TLS against the operating system’s trust store and the certifi bundle.

Method Reference#

method execute_query_single(sql, context=CASJOBSDCONST.DEFAULT_DATABASE, _format=CASJOBSDCONST.DEFAULT_RETURNTYPE, *, _futures=False, **_)#

Execute one synchronous SQL query in a CasJobs database context.

Extra keyword arguments, such as nthreads forwarded by execute_query(), are ignored.

Parameters:
  • sql (str) – The SQL query.

  • context (str) – The database context.

  • _format (str) – The output format name. See query() for the output formats.

  • _futures (bool) – Whether the request runs inside execute_query_futures(). If True, reuses the headers already set and returns the first raw result set dictionary unformatted.

Returns:

The result sets in the _format output format.

Raises:
Return type:

dict | list[dict] | tuple[list, list] | DataFrame | list[DataFrame]

See also

SciServer.CasJobs.executeQuery for the SciScript-Python equivalent.

method execute_query_futures(sql, context=CASJOBSDCONST.DEFAULT_DATABASE, _format=CASJOBSDCONST.DEFAULT_RETURNTYPE, nthreads=CASJOBSDCONST.DEFAULT_THREADS)#

Execute SQL queries in parallel in a CasJobs database context.

Only the first result set of each query is kept. Results are gathered in completion order, not input order.

Parameters:
  • sql (Sequence[str]) – The SQL queries.

  • context (str) – The database context.

  • _format (str) – The output format name. See query() for the output formats.

  • nthreads (int) – Number of threads used to run the queries.

Returns:

The result sets in the _format output format.

Raises:

CasJobsFuturesConnectionError – One or more queries failed; holds each query’s error.

Return type:

list[dict] | tuple[list, list] | DataFrame | list[DataFrame]

method execute_query(sql, *args, **kwargs)#

Execute one SQL query, or a sequence of queries in parallel.

A string runs through execute_query_single(); any other iterable runs through execute_query_futures().

Parameters:
  • sql (str | Sequence[str]) – The SQL query or queries.

  • args – Positional arguments for the selected method.

  • kwargs – Keyword arguments for the selected method.

Returns:

The result sets in the requested output format.

Raises:

InvalidSQLFormatError – sql is neither a string nor an iterable.

Return type:

dict | list[dict] | tuple[list, list] | DataFrame | list[DataFrame]

static get_taskname(task_id)#

Get the CasJobs task name for a local or SciServer compute client.

Parameters:

task_id (str) – The task id (ex: ‘executeQuery’).

Returns:

'Compute.SciScript-Python.CasJobs.<task_id>' in a SciServer compute environment, otherwise 'SciScript-Python.CasJobs.<task_id>'.

Return type:

str

method set_headers(accept_format)#

Set the token and ‘Accept’ in _headers for next requests.

Parameters:

accept_format (str) – The output format name. See query() for the output formats.

Return type:

None

static format_data(sql, task_id)#

Format sql and task_id as a JSON request body.

Parameters:
  • sql (str) – The SQL query to pass.

  • task_id (str) – The task name for the query.

Returns:

The JSON-encoded request body.

Return type:

bytes

Private Method Reference#

method _validate_response(response)#

Raise if the response status is not ‘OK’ (200).

Raises:
Parameters:

response (urllib3.BaseHTTPResponse)

Return type:

None