APIRequest

class wwt_api_client.APIRequest(client)[source]

Bases: object

A base class represent various WWT API requests.

This class provides a generic representation of WWT API requests. For instance, every API request instance provides a make_request() method that gets the underlying HTTP request as a requests.Request class.

You don’t generally need to instantiate requests yourself. Instead, use the methods on Client to create requests.

Parameters:
clientClient

The client with which this request is associated.

Methods Summary

invalidity_reason()

Check whether the parameters of this request are valid.

make_request()

Generate a requests.Request from the current parameters.

send([raw_response])

Issue the request and return its result.

to_text()

Issue the request and return its results as text.

to_xml()

Issue the request and return its results as parsed XML.

Methods Documentation

invalidity_reason()[source]

Check whether the parameters of this request are valid.

Returns:
reasonstring or None

If None, indicates that this request is valid. Otherwise, the returned string explains what about the request’ parameters is invalid.

Examples

You can manually check if a request is correctly set up:

>>> from wwt_api_client import Client
>>> req = Client().show_image('http://example.com/space.jpg', 'My Image')
>>> assert req.invalidity_reason() is None
make_request()[source]

Generate a requests.Request from the current parameters.

This method returns a requests.Request object ready for sending to the API server.

Returns:
requestrequests.Request object

The HTTP request.

Examples

Get the URL that will be accessed for a request:

>>> from urllib.parse import urlparse
>>> from wwt_api_client import Client
>>> req = Client().show_image('http://example.com/space.jpg', 'My Image')
>>> parsed_url = urlparse(req.make_request().prepare().url)
>>> print(parsed_url.path)
/WWTWeb/ShowImage.aspx
send(raw_response=False)[source]

Issue the request and return its result.

The request’s validity will be checked before sending.

Parameters:
raw_responsebool, optional, default False

If True, the raw requests response will be returned rather than the processed version.

Returns:
responsevaries

The server reponse, postprocesed into whatever form makes the most sense for this API. The default is text.

Raises:
InvalidRequestError

Raised if the request parameters are invalid.

APIResponseError

Raised if the API call results in an HTTP error code.

Examples

Send a ShowImage request:

>>> from wwt_api_client import Client
>>> req = Client().show_image('http://example.com/space.jpg', 'My Image')
>>> print(req.send()[:10])  # prints start of a WTML XML document
<?xml vers
to_text()[source]

Issue the request and return its results as text.

to_xml()[source]

Issue the request and return its results as parsed XML.