Source code for codegrade._api.pending_registration

"""The endpoints for pending_registration 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_pending_registration_data import (
        ConfirmPendingRegistrationData,
    )
    from ..models.create_pending_registration_data import (
        CreatePendingRegistrationData,
    )
    from ..models.pending_registration_response import (
        PendingRegistrationResponse,
    )
    from ..models.user_login_response import UserLoginResponse


_ClientT = t.TypeVar("_ClientT", bound="client._BaseClient")


[docs] class PendingRegistrationService(t.Generic[_ClientT]): __slots__ = ("__client",) def __init__(self, client: _ClientT) -> None: self.__client = client
[docs] def confirm( self, json_body: ConfirmPendingRegistrationData, *, token: str, ) -> UserLoginResponse: """Verify the email code and finalize the registration. Creates the user in the database with `email_verified=True` and returns a login response. :param json_body: The body of the request. See :class:`.ConfirmPendingRegistrationData` for information about the possible fields. You can provide this data as a :class:`.ConfirmPendingRegistrationData` or as a dictionary. :param token: The pending registration identifier. :returns: A login response with the new user and access token. """ url = "/api/v1/registrations/pending/{token}/confirm".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.user_login_response import UserLoginResponse return parsers.JsonResponseParser( parsers.ParserFor.make(UserLoginResponse) ).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, json_body: CreatePendingRegistrationData, ) -> PendingRegistrationResponse: """Create a pending registration and send a verification code. The user account is NOT created in the database yet. It will be finalized when the email is verified via the confirm endpoint. :param json_body: The body of the request. See :class:`.CreatePendingRegistrationData` for information about the possible fields. You can provide this data as a :class:`.CreatePendingRegistrationData` or as a dictionary. :returns: An identifier for the pending registration. """ url = "/api/v1/registrations/pending" 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.pending_registration_response import ( PendingRegistrationResponse, ) return parsers.JsonResponseParser( parsers.ParserFor.make(PendingRegistrationResponse) ).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), ), ), )