# coding: utf-8

"""
    JupyterHub

    The REST API for JupyterHub

    The version of the OpenAPI document: 4.0.2
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from datetime import datetime
from typing import Any, ClassVar, Dict, List, Optional
from pydantic import BaseModel, StrictStr
from pydantic import Field
try:
    from typing import Self
except ImportError:
    from typing_extensions import Self

class Token(BaseModel):
    """
    Token
    """ # noqa: E501
    token: Optional[StrictStr] = Field(default=None, description="The token itself. Only present in responses to requests for a new token.")
    id: Optional[StrictStr] = Field(default=None, description="The id of the API token. Used for modifying or deleting the token.")
    user: Optional[StrictStr] = Field(default=None, description="The user that owns a token (undefined if owned by a service)")
    service: Optional[StrictStr] = Field(default=None, description="The service that owns the token (undefined of owned by a user)")
    roles: Optional[List[StrictStr]] = Field(default=None, description="Deprecated in JupyterHub 3, always an empty list. Tokens have 'scopes' starting from JupyterHub 3.")
    scopes: Optional[List[StrictStr]] = Field(default=None, description="List of scopes this token has been assigned. New in JupyterHub 3. In JupyterHub 2.x, tokens were assigned 'roles' insead of scopes.")
    note: Optional[StrictStr] = Field(default=None, description="A note about the token, typically describing what it was created for.")
    created: Optional[datetime] = Field(default=None, description="Timestamp when this token was created")
    expires_at: Optional[datetime] = Field(default=None, description="Timestamp when this token expires. Null if there is no expiry.")
    last_activity: Optional[datetime] = Field(default=None, description="Timestamp of last-seen activity using this token. Can be null if token has never been used. ")
    session_id: Optional[StrictStr] = Field(default=None, description="The session id associated with the token, if any. Only used for tokens set during oauth flows.  Added in 2.0. ")
    __properties: ClassVar[List[str]] = ["token", "id", "user", "service", "roles", "scopes", "note", "created", "expires_at", "last_activity", "session_id"]

    model_config = {
        "populate_by_name": True,
        "validate_assignment": True,
        "protected_namespaces": (),
    }


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Self:
        """Create an instance of Token from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        _dict = self.model_dump(
            by_alias=True,
            exclude={
            },
            exclude_none=True,
        )
        # set to None if session_id (nullable) is None
        # and model_fields_set contains the field
        if self.session_id is None and "session_id" in self.model_fields_set:
            _dict['session_id'] = None

        return _dict

    @classmethod
    def from_dict(cls, obj: Dict) -> Self:
        """Create an instance of Token from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "token": obj.get("token"),
            "id": obj.get("id"),
            "user": obj.get("user"),
            "service": obj.get("service"),
            "roles": obj.get("roles"),
            "scopes": obj.get("scopes"),
            "note": obj.get("note"),
            "created": obj.get("created"),
            "expires_at": obj.get("expires_at"),
            "last_activity": obj.get("last_activity"),
            "session_id": obj.get("session_id")
        })
        return _obj


