# coding: utf-8

"""
    Test Results API

    Get test result metrics for Network and Application Synthetics tests.

    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 pydantic import BaseModel, ConfigDict, Field, StrictFloat, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional, Union
from thousandeyes_sdk.test_results.models.api_request_detail_assertion import ApiRequestDetailAssertion
from thousandeyes_sdk.test_results.models.api_request_step_type import ApiRequestStepType
from typing import Optional, Set
from typing_extensions import Self

class ApiRequestDetail(BaseModel):
    """
    ApiRequestDetail
    """ # noqa: E501
    api_call_time: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Total time specific to the API call.", alias="apiCallTime")
    assert_error_count: Optional[StrictInt] = Field(default=None, description="Count of assertion errors.", alias="assertErrorCount")
    blocked_time: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Time to establish a socket connection for this request.", alias="blockedTime")
    connect_time: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Time to establish a TCP connection.", alias="connectTime")
    completion: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="100 if the request responded with no assertion errors, otherwise 0.")
    dns_time: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Time for the agent to perform a DNS resolution of the hostname in the URL.", alias="dnsTime")
    name: Optional[StrictStr] = Field(default=None, description="Name of the API step.")
    processing_time: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Time for the agent to process the API step, including the waitTimeMs delay specified in the post request options.", alias="processingTime")
    receive_time: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Time to receive the response from the server.", alias="receiveTime")
    response_time: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Time for server to send the response.", alias="responseTime")
    send_time: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Time to send the request.", alias="sendTime")
    step_number: Optional[StrictInt] = Field(default=None, description="Index of the API step within requests, starting at 1.", alias="stepNumber")
    step_time: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Total time for an API step, including API call time and processing time.", alias="stepTime")
    step_type: Optional[ApiRequestStepType] = Field(default=None, alias="stepType")
    url: Optional[StrictStr] = Field(default=None, description="URL of request")
    wait_time: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Total time between when the agent completes sending the HTTP request to the web server and when the agent receives the first byte of the response from the web server.", alias="waitTime")
    assertions: Optional[List[ApiRequestDetailAssertion]] = None
    __properties: ClassVar[List[str]] = ["apiCallTime", "assertErrorCount", "blockedTime", "connectTime", "completion", "dnsTime", "name", "processingTime", "receiveTime", "responseTime", "sendTime", "stepNumber", "stepTime", "stepType", "url", "waitTime", "assertions"]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
        extra="allow",
    )


    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 self.model_dump_json(by_alias=True, exclude_unset=True, exclude_none=True)

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of ApiRequestDetail 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.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of each item in assertions (list)
        _items = []
        if self.assertions:
            for _item in self.assertions:
                if _item:
                    _items.append(_item.to_dict())
            _dict['assertions'] = _items
        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of ApiRequestDetail from a dict"""
        if obj is None:
            return None

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

        _obj = cls.model_validate({
            "apiCallTime": obj.get("apiCallTime"),
            "assertErrorCount": obj.get("assertErrorCount"),
            "blockedTime": obj.get("blockedTime"),
            "connectTime": obj.get("connectTime"),
            "completion": obj.get("completion"),
            "dnsTime": obj.get("dnsTime"),
            "name": obj.get("name"),
            "processingTime": obj.get("processingTime"),
            "receiveTime": obj.get("receiveTime"),
            "responseTime": obj.get("responseTime"),
            "sendTime": obj.get("sendTime"),
            "stepNumber": obj.get("stepNumber"),
            "stepTime": obj.get("stepTime"),
            "stepType": obj.get("stepType"),
            "url": obj.get("url"),
            "waitTime": obj.get("waitTime"),
            "assertions": [ApiRequestDetailAssertion.from_dict(_item) for _item in obj["assertions"]] if obj.get("assertions") is not None else None
        })
        return _obj


