o
    ȳg                     @   sX   d Z ddlZddlmZmZmZ ddlZddlmZ ddl	m
Z
 dZG dd de
ZdS )	zp
Util that calls several of financial datasets stock market REST APIs.
Docs: https://docs.financialdatasets.ai/
    N)AnyListOptional)get_from_dict_or_env)	BaseModelz!https://api.financialdatasets.ai/c                	       s   e Zd ZU dZdZee ed< def fddZ	e
defdd	Zd
ededee dee fddZd
ededee dee fddZd
ededee dee fddZded
ededefddZ  ZS )FinancialDatasetsAPIWrapperz#Wrapper for financial datasets API.Nfinancial_datasets_api_keydatac                    s$   t  jdi | t|dd| _d S )Nr   FINANCIAL_DATASETS_API_KEY )super__init__r   r   )selfr	   	__class__r   l/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain_community/utilities/financial_datasets.pyr      s   
z$FinancialDatasetsAPIWrapper.__init__returnc                 C   s   | j d u r	td| j S )Na#  API key is required for the FinancialDatasetsAPIWrapper. Please provide the API key by either:
1. Manually specifying it when initializing the wrapper: FinancialDatasetsAPIWrapper(financial_datasets_api_key='your_api_key')
2. Setting it as an environment variable: FINANCIAL_DATASETS_API_KEY)r   
ValueError)r   r   r   r   _api_key   s
   
z$FinancialDatasetsAPIWrapper._api_keytickerperiodlimitc                 C   N   t  d| d| d|r|nd }d| ji}tj||d}| }|ddS )	av  
        Get the income statements for a stock `ticker` over a `period` of time.

        :param ticker: the stock ticker
        :param period: the period of time to get the balance sheets for.
            Possible values are: annual, quarterly, ttm.
        :param limit: the number of results to return, default is 10
        :return: a list of income statements
        z%financials/income-statements/?ticker=&period=&limit=
   	X-API-KEYheadersincome_statementsNFINANCIAL_DATASETS_BASE_URLr   requestsgetjsonr   r   r   r   urlr   responser	   r   r   r   get_income_statements'      

z1FinancialDatasetsAPIWrapper.get_income_statementsc                 C   r   )	ap  
        Get the balance sheets for a stock `ticker` over a `period` of time.

        :param ticker: the stock ticker
        :param period: the period of time to get the balance sheets for.
            Possible values are: annual, quarterly, ttm.
        :param limit: the number of results to return, default is 10
        :return: a list of balance sheets
        z"financials/balance-sheets/?ticker=r   r   r   r   r   balance_sheetsNr    r%   r   r   r   get_balance_sheetsF   r)   z.FinancialDatasetsAPIWrapper.get_balance_sheetsc                 C   r   )	a|  
        Get the cash flow statements for a stock `ticker` over a `period` of time.

        :param ticker: the stock ticker
        :param period: the period of time to get the balance sheets for.
            Possible values are: annual, quarterly, ttm.
        :param limit: the number of results to return, default is 10
        :return: a list of cash flow statements
        z(financials/cash-flow-statements/?ticker=r   r   r   r   r   cash_flow_statementsNr    r%   r   r   r   get_cash_flow_statementse   s   

z4FinancialDatasetsAPIWrapper.get_cash_flow_statementsmodekwargsc                 K   s   |dkr| dd}| dd}t| |||S |dkr4| dd}| dd}t| |||S |dkrN| dd}| dd}t| |||S td| d	)
Nr(   r   annualr   r   r+   r-   zInvalid mode z for financial datasets API.)r#   r$   dumpsr(   r+   r-   r   )r   r.   r   r/   r   r   r   r   r   run   s   zFinancialDatasetsAPIWrapper.run)__name__
__module____qualname____doc__r   r   str__annotations__r   r   propertyr   intdictr(   r   r+   r-   r2   __classcell__r   r   r   r   r      sD   
 


" r   )r6   r$   typingr   r   r   r"   langchain_core.utilsr   pydanticr   r!   r   r   r   r   r   <module>   s    