Skip to content

Client

client

Client for Yahoo Finance.

YahooClient(timeout: float = 30.0, requestlog: bool = False)

Yahoo Finance HTTP client.

Source code in src/equities_classifier/clients/yahoo/client.py
def __init__(
    self,
    timeout: float = 30.0,
    requestlog: bool = False,
) -> None:
    """Initialize Yahoo Finance client."""

    _client: httpx.Client | None

    self._client = httpx.Client(
        base_url=self._BASE_URL,
        timeout=timeout,
        follow_redirects=True,
        event_hooks={"request": [log_request], "response": [log_response]} if requestlog else None,
    )

__enter__() -> Self

Source code in src/equities_classifier/clients/yahoo/client.py
def __enter__(self) -> Self:
    return self

__exit__(*_: object) -> None

Source code in src/equities_classifier/clients/yahoo/client.py
def __exit__(self, *_: object) -> None:
    self._close()

read_provider_profile_data(source_identifiers: Sequence[SecurityIdentifier], raise_error: bool = False) -> list[YahooRecord]

Read profile data for identifiers from Yahoo Finance.

Source code in src/equities_classifier/clients/yahoo/client.py
def read_provider_profile_data(
    self,
    source_identifiers: Sequence[SecurityIdentifier],
    raise_error: bool = False,
) -> list[YahooRecord]:
    """Read profile data for identifiers from Yahoo Finance."""

    records: list[YahooRecord] = []

    for source_identifier in source_identifiers:

        if not self.supports_identifier_type(source_identifier.type):
            ClientHelperErrorHandler.invalid_security_type(DataSourceID.YAHOO, source_identifier)
            continue

        response_data = self._execute_search_request(source_identifier)
        search_results = self._parse_search_results(source_identifier, response_data, raise_error)
        if search_results is None or len(search_results) == 0:
            continue

        record = self._parse_record(source_identifier, search_results[0])
        # check for duplicates
        if record:
            duplicate = False
            for existing_record in records:
                if record.name == existing_record.name:
                    duplicate = True
                    for identifier in record.identifiers:
                        if not existing_record.has_identifier(identifier.type):
                            existing_record.identifiers.append(identifier)
            if not duplicate:
                records.append(record)

    return records

supports_identifier_type(identifier_type: SecurityIdentifierType) -> bool classmethod

Check if identifier type is supported.

Source code in src/equities_classifier/clients/yahoo/client.py
@classmethod
def supports_identifier_type(cls, identifier_type: SecurityIdentifierType) -> bool:
    """Check if identifier type is supported."""
    return identifier_type in cls._YAHOO_IDENTIFIER_TYPES.values()

YahooResponseError

Yahoo Finance returned an invalid response.