"""The endpoints for email_change objects.
SPDX-License-Identifier: AGPL-3.0-only OR BSD-3-Clause-Clear
"""
from __future__ import annotations
import os
import typing as t
import cg_request_args as rqa
from cg_maybe import Maybe, Nothing
from .. import parsers, utils
if t.TYPE_CHECKING or os.getenv("CG_EAGERIMPORT", False):
from .. import client
from ..models.confirm_new_email_change_data import (
ConfirmNewEmailChangeData,
)
from ..models.confirm_old_email_change_data import (
ConfirmOldEmailChangeData,
)
from ..models.create_email_change_data import CreateEmailChangeData
from ..models.email_change_confirm_old_response import (
EmailChangeConfirmOldResponse,
)
from ..models.email_change_done_response import EmailChangeDoneResponse
from ..models.email_change_response import EmailChangeResponse
_ClientT = t.TypeVar("_ClientT", bound="client._BaseClient")
[docs]
class EmailChangeService(t.Generic[_ClientT]):
__slots__ = ("__client",)
def __init__(self, client: _ClientT) -> None:
self.__client = client
[docs]
def confirm_new(
self,
json_body: ConfirmNewEmailChangeData,
*,
token: str,
) -> EmailChangeDoneResponse:
"""Verify the new-email code and complete the email change.
Only valid for sessions in `pending_new_confirm` status. On success,
the user's email is updated and marked as verified.
Does not require authentication — the token in the URL is the
credential. IP-bound to the initiating client.
:param json_body: The body of the request. See
:class:`.ConfirmNewEmailChangeData` for information about the
possible fields. You can provide this data as a
:class:`.ConfirmNewEmailChangeData` or as a dictionary.
:param token: The email change session identifier.
:returns: The new email address.
"""
url = "/api/v1/email_changes/{token}/confirm_new".format(token=token)
params = None
with self.__client as client:
resp = client.http.post(
url=url, json=utils.to_dict(json_body), params=params
)
utils.log_warnings(resp)
if utils.response_code_matches(resp.status_code, 200):
from ..models.email_change_done_response import (
EmailChangeDoneResponse,
)
return parsers.JsonResponseParser(
parsers.ParserFor.make(EmailChangeDoneResponse)
).try_parse(resp)
from ..models.any_error import AnyError
raise utils.get_error(
resp,
(
(
(400, 409, 401, 403, 404, 429, 500),
utils.unpack_union(AnyError),
),
),
)
[docs]
def confirm_old(
self,
json_body: ConfirmOldEmailChangeData,
*,
token: str,
) -> EmailChangeConfirmOldResponse:
"""Verify the old-email code and send a code to the new email.
Only valid for sessions in `pending_old_confirm` status (Path B:
no-password users). On success, transitions to `pending_new_confirm`
and sends a code to the new email. Call `email_change.confirm_new`
next.
Does not require authentication — the token in the URL is the
credential. IP-bound to the initiating client.
:param json_body: The body of the request. See
:class:`.ConfirmOldEmailChangeData` for information about the
possible fields. You can provide this data as a
:class:`.ConfirmOldEmailChangeData` or as a dictionary.
:param token: The email change session identifier.
:returns: An empty response on success.
"""
url = "/api/v1/email_changes/{token}/confirm_old".format(token=token)
params = None
with self.__client as client:
resp = client.http.post(
url=url, json=utils.to_dict(json_body), params=params
)
utils.log_warnings(resp)
if utils.response_code_matches(resp.status_code, 200):
from ..models.email_change_confirm_old_response import (
EmailChangeConfirmOldResponse,
)
return parsers.JsonResponseParser(
parsers.ParserFor.make(EmailChangeConfirmOldResponse)
).try_parse(resp)
from ..models.any_error import AnyError
raise utils.get_error(
resp,
(
(
(400, 409, 401, 403, 404, 429, 500),
utils.unpack_union(AnyError),
),
),
)
[docs]
def create(
self: EmailChangeService[client.AuthenticatedClient],
json_body: CreateEmailChangeData,
) -> EmailChangeResponse:
"""Initiate an email change.
If the user has a password and provides `old_password`, the flow skips
old-email verification and goes straight to new-email verification
(call `email_change.confirm_new`).
If the user has no password (SSO/LTI), old-email verification is
required first (call `email_change.confirm_old`, then
`email_change.confirm_new`).
:param json_body: The body of the request. See
:class:`.CreateEmailChangeData` for information about the possible
fields. You can provide this data as a
:class:`.CreateEmailChangeData` or as a dictionary.
:returns: An email change session identifier.
"""
url = "/api/v1/email_changes/"
params = None
with self.__client as client:
resp = client.http.post(
url=url, json=utils.to_dict(json_body), params=params
)
utils.log_warnings(resp)
if utils.response_code_matches(resp.status_code, 200):
from ..models.email_change_response import EmailChangeResponse
return parsers.JsonResponseParser(
parsers.ParserFor.make(EmailChangeResponse)
).try_parse(resp)
from ..models.any_error import AnyError
raise utils.get_error(
resp,
(
(
(400, 409, 401, 403, 404, 429, 500),
utils.unpack_union(AnyError),
),
),
)