
    ic                    d    S r SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  \" \
5      r " S S5      rg)	zClient for tab-api-ingest service.

Provides async HTTP client for consuming racing data from the tab-api-ingest
REST API instead of calling the TAB Affiliates API directly.
    )annotations)AnyN)
get_logger)get_settingsc                      \ rS rSrSrSSS jjrS rS r S       SS jjr   S       SS jjr	SS	 jr
SS
 jr     S           SS jjrSrg)IngestServiceClient   a  Async HTTP client for tab-api-ingest REST API.

Fetches meetings, races, and runner data from the tab-api-ingest service
which sources data from the TAB/HRNZ APIs and stores it in its own DB.

Example:
    >>> async with IngestServiceClient() as client:
    >>>     meetings = await client.get_meetings(date="2024-01-01")
    >>>     for meeting in meetings:
    >>>         races = await client.get_races(meeting_id=meeting["meeting"])
Nc                    [        5       nU=(       d3    [        USS5      =(       a    UR                  R                  =(       d    SU l        SU l        g)zInitialize the ingest service client.

Args:
    base_url: tab-api-ingest service base URL. Defaults to the
              INGEST_SERVICE_URL setting or http://localhost:9090.
ingest_serviceNzhttp://localhost:9090)r   getattrr   urlbase_url_client)selfr   settingss      B/root/tipsharks/tipsharks-elo-api/packages/ingest_client/client.py__init__IngestServiceClient.__init__    sN      > 'x!148 ,''++' '	 	 26    c                @   #    [         R                  " SS9U l        U $ 7f)u8   Async context manager entry — creates the HTTP client.      >@timeout)httpxAsyncClientr   )r   s    r   
__aenter__IngestServiceClient.__aenter__0   s     ((6s   c                ~   #    U R                   b*  U R                   R                  5       I Sh  vN   SU l         gg N7f)u6   Async context manager exit — closes the HTTP client.N)r   aclose)r   exc_typeexc_valexc_tbs       r   	__aexit__IngestServiceClient.__aexit__5   s4     <<#,,%%'''DL $'s   +=;=c                   #    U R                   c  [        R                  " SS9U l         U R                   R                  XUS9I Sh  vN nUR	                  5         UR                  5       $  N$7f)zMake an HTTP request with the internal client.

Args:
    method: HTTP method
    url: Full URL
    params: Query parameters

Returns:
    Response JSON data

Raises:
    httpx.HTTPError: On request failure
Nr   r   )params)r   r   r   requestraise_for_statusjson)r   methodr   r&   responses        r   _requestIngestServiceClient._request;   s]     & << ,,T:DL--f&-II!!#}} Js   AA-A+%A-c                  #    0 nU(       a  XS'   U(       a  X$S'   U(       a  X4S'   [         R                  SSU0S9  U R                  SU R                   S3U5      I S	h  vN n[         R                  S
[	        U5       S35        U$  N(7f)zFetch meetings from the ingest service.

Args:
    date: Optional ISO date filter (YYYY-MM-DD)
    country: Optional country code filter (e.g. NZ, AUS)
    category: Optional racing category (T, H, G)

Returns:
    List of meeting data dictionaries
datecountrycategoryz%Fetching meetings from ingest servicer&   extraGETz/api/meetingsNzFound z meetings from ingest service)loggerinfor,   r   len)r   r/   r0   r1   r&   datas         r   get_meetings IngestServiceClient.get_meetingsT   s       "$!6N '9!):3V$ 	 	

 ]]5T]]O=*I6RRfSYK'DEF Ss   AB
B )B
c                   #    [         R                  SU S35        U R                  SU R                   SU 35      I Sh  vN $  N7f)z{Fetch a single race from the ingest service.

Args:
    race_id: Race/event ID (string)

Returns:
    Race data dictionary
zFetching race  from ingest servicer4   /api/races/Nr5   r6   r,   r   r   race_ids     r   get_raceIngestServiceClient.get_raceu   sC      	nWI-ABC]]5T]]O;wi*PQQQQs   >A AAc                   #    [         R                  SU S35        U R                  SU R                   SU S35      I Sh  vN $  N7f)zFetch runners for a race from the ingest service.

Args:
    race_id: Race/event ID (string)

Returns:
    List of runner data dictionaries
zFetching runners for race r<   r4   r=   z/runnersNr>   r?   s     r   get_runnersIngestServiceClient.get_runners   sP      	0	9MNO]]}}o[	:
 
 	
 
s   ?AAAc                   #    XES.nU(       a  XS'   U(       a  X&S'   U(       a  X6S'   [         R                  SSU0S9  U R                  SU R                   S	3U5      I S
h  vN $  N7f)a*  Fetch races from the ingest service.

Args:
    date: Optional ISO date filter (YYYY-MM-DD)
    meeting_id: Optional meeting ID filter
    status: Optional status filter
    limit: Maximum results (default 100)
    offset: Pagination offset (default 0)

Returns:
    List of race data dictionaries
)limitoffsetr/   	meetingIdstatusz"Fetching races from ingest servicer&   r2   r4   z
/api/racesNr>   )r   r/   
meeting_idrJ   rG   rH   r&   s          r   	get_racesIngestServiceClient.get_races   sq     ( ,1!C!6N",;%80V$ 	 	

 ]]5T]]O:*FOOOOs   AA(!A&"A()r   r   )N)r   
str | None)r*   strr   rO   r&   zdict[str, Any] | Nonereturnr   )NNN)r/   rN   r0   rN   r1   rN   rP   list[dict[str, Any]])r@   rO   rP   zdict[str, Any])r@   rO   rP   rQ   )NNNd   r   )r/   rN   rK   rN   rJ   rN   rG   intrH   rS   rP   rQ   )__name__
__module____qualname____firstlineno____doc__r   r   r#   r,   r9   rA   rD   rL   __static_attributes__ r   r   r   r      s    
6 
  )-	  &	
 
6  "#	  	
 
B
R
"  !%!!P!P !P 	!P
 !P !P 
!P !Pr   r   )rX   
__future__r   typingr   r   packages.core.common.loggingr   packages.core.common.settingsr   rT   r5   r   rZ   r   r   <module>r_      s3    #   3 6	H	^P ^Pr   