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

constellations: start defaulting to the production service! #28

Merged
merged 1 commit into from
Oct 22, 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
2 changes: 2 additions & 0 deletions docs/api/wwt_api_client.constellations.ClientConfig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ ClientConfig

~ClientConfig.new_default
~ClientConfig.new_dev
~ClientConfig.new_prod

.. rubric:: Methods Documentation

.. automethod:: new_default
.. automethod:: new_dev
.. automethod:: new_prod
62 changes: 36 additions & 26 deletions wwt_api_client/constellations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
.. _OpenID Connect: https://openid.net/connect/

The client can connect to different instances of the backend API and
authentication service: the production environment (**which doesn't exist
yet**), the development environment, or a local testing instance. To make it so
that your code can choose which version to use on-the-fly, use the default
constructors and set the environment variable ``NUXT_PUBLIC_API_URL``. You'll
probably wish to use one of the following values:

- ``http://localhost:7000`` for a standard local testing environment, or
- ``https://api.wwtelescope.dev/`` for the development environment
authentication service. By default, it connects to the production environment.
To make it so that your code can choose which version to use on-the-fly, use the
default constructors and set the environment variable ``NUXT_PUBLIC_API_URL``.
You'll probably wish to use one of the following values:

- ``https://api.worldwidetelescope.org`` for the production API
- ``http://localhost:7000`` for a standard local testing environment
"""

from dataclasses import dataclass
Expand Down Expand Up @@ -56,28 +54,28 @@ def new_default(cls) -> "ClientConfig":
"""
Create a new client configuration with sensible default settings.

**Note!** Eventually this method will default to using the public,
production WWT Constellations service. But since that doesn't exist, you
currently must set *at least* the environment variable
``NUXT_PUBLIC_API_URL`` to indicate which service to use. The short
advice for now is that you should almost definitely set
``NUXT_PUBLIC_API_URL`` to either ``http://localhost:7000`` or to
``https://api.wwtelescope.dev/``.
This method defaults to using the public, production WWT Constellations
service. To override the backend, set ``NUXT_PUBLIC_API_URL`` to
something else, such as ``http://localhost:7000`` for the default local
testing configuration.

The long version is that the "sensible default" settings are determined
in the following way:
The "sensible default" settings are determined in the following way:

- If the environment variable ``NUXT_PUBLIC_API_URL`` is set, its value
used as the base URL for all API calls. (The name of this variable
aligns with the one used by the Constellations frontend server.)
- **Otherwise, an error is raised as mentioned above.**
- Otherwise, ``https://api.worldwidetelescope.org`` is used.
- If the environment variable ``NUXT_PUBLIC_KEYCLOAK_URL`` is set, its
value used as the base URL for the authentication service.
- Otherwise, if the environment variable ``KEYCLOAK_URL`` is set, its
value is used.
- Otherwise, if the base API URL contains the string ``localhost``, the
value ``http://localhost:8080`` is used. This is the default used by
the standard Keycloak Docker image.
- Otherwise, if the base API URL contains the string
``worldwidetelescope.org``, the value
``https://worldwidetelescope.org/auth/`` is used. This is the setting
for the WWT Constellations production environment.
- Otherwise, if the base API URL contains the string
``wwtelescope.dev``, the value ``https://wwtelescope.dev/auth/`` is
used. This is the setting for the WWT Constellations development
Expand All @@ -88,12 +86,12 @@ def new_default(cls) -> "ClientConfig":
the text ``realms/constellations`` is appended.
- Finally, if the environment variable ``WWT_API_CLIENT_ID`` is set, its
value is used to set the client ID.
- Otherwise it defaults to ``cli-tool``.
- Otherwise it defaults to ``automation``.
"""

api_url = os.environ.get("NUXT_PUBLIC_API_URL")
client_id = os.environ.get("WWT_API_CLIENT_ID", "cli-tool")
default_id_base = None
client_id = os.environ.get("WWT_API_CLIENT_ID", "automation")
default_id_base = "https://worldwidetelescope.org/auth/"

if api_url is not None:
if "localhost" in api_url:
Expand All @@ -103,10 +101,7 @@ def new_default(cls) -> "ClientConfig":
# dev mode?
default_id_base = "https://wwtelescope.dev/auth/"
else:
# TODO: default to using the production API, once it exists!
raise Exception(
"until WWT Constellations is released, you must set the environment variable NUXT_PUBLIC_API_URL"
)
api_url = "https://api.worldwidetelescope.org"

if api_url.endswith("/"):
api_url = api_url[:-1]
Expand All @@ -128,6 +123,21 @@ def new_default(cls) -> "ClientConfig":
api_url=api_url,
)

@classmethod
def new_prod(cls) -> "ClientConfig":
"""
Create a new client configuration explicitly set up for the WWT
Constellations production environment.

You should probably use :meth:`new_default` unless you explicitly want
your code to *always* refer to the production environment.
"""
return cls(
id_provider_url="https://worldwidetelescope.org/auth/realms/constellations",
client_id="automation",
api_url="https://api.worldwidetelescope.org",
)

@classmethod
def new_dev(cls) -> "ClientConfig":
"""
Expand All @@ -140,7 +150,7 @@ def new_dev(cls) -> "ClientConfig":
return cls(
id_provider_url="https://wwtelescope.dev/auth/realms/constellations",
client_id="cli-tool",
api_url="https://api.wwtelescope.dev/",
api_url="https://api.wwtelescope.dev",
)


Expand Down