�
I?�g4 � � � d Z ddlZddlZddlZddlZddlZddlmZmZm Z m
Z
mZ ej ej �� ej e� Z G d� d� Zy)ze
SafetyCulture API Client
This module provides a client for interacting with the SafetyCulture API.
� N)�Dict�List�Optional�Any�Union)�levelc � � e Zd ZdZdZdZddee fd�Zdeddfd�Z deddfd �Z
defd
�Z ddee dee d
ee
eej f dee
eej f dedededeeeef fd�Zdedeeef fd�Z ddee d
ee
eej f dee
eej f dedeeeef f
d�Zy)�SafetyCultureClientz!Client for the SafetyCulture API.zhttps://api.safetyculture.io�feedN�api_keyc �l � || _ t j � | _ |r| j |� yy)z�
Initialize the SafetyCulture API client.
Args:
api_key: Optional API key for authentication
N)r �requests�Session�session�_set_auth_header��selfr s �QC:\Users\zerub\Desktop\Coding\Personal\mcp-python\src\safetyculture_api\client.py�__init__zSafetyCultureClient.__init__ s1 � � ����'�'�)�����!�!�'�*� � �returnc �^ � || _ | j |� t j d� y)zw
Set the API key for authentication.
Args:
api_key: SafetyCulture API key
zAPI key setN)r r �logger�infor s r �set_api_keyzSafetyCultureClient.set_api_key% s% � � ������g�&����M�"r c � � |rt |� nd}t j d|� d�� | j j j d|� �dd�� y)z�
Set the authorization header with the API key.
Args:
api_key: SafetyCulture API key
r z*Setting auth header with API key (length: �)zBearer zapplication/json)�
Authorization�acceptN)�lenr �debugr �headers�update)r r �
key_lengths r r z$SafetyCultureClient._set_auth_header0 sS � � &-�S��\�!�
����A�*��Q�O�P�
�����#�#�&�w�i�0�(�%
� r c � � | j s t j d� t d� �| j � d| j
� d�}t j
d|� �� | j j |� }t j
d|j � �� |j dk( rt j
d� y |j d
v rOt j d|j � �� t j d� t d
|j � �� �| j � d| j
� d�}t j
d|� �� | j j |� }|j dk( rt j
d� y |j d
v r:t j d|j � �� t d
|j � �� �t j d|j � d|j � �� t d|j � �� �# t j j $ r=}t j dt |� � �� t dt |� � �� �d}~ww xY w)z�
Test the connection to the SafetyCulture API.
Returns:
True if the connection is successful, False otherwise
Raises:
Exception: If the API key is not set or the connection fails
zAPI key not set�-API key not set. Please set an API key first.�/�/inspectionszTesting connection to �API response status code: �� z,Successfully connected to SafetyCulture API!T)i� i� z#Authentication failed with status: zBThis could indicate an invalid API key or insufficient permissionszAuthentication failed: Status �/actionszTrying alternative endpoint: zCSuccessfully connected to SafetyCulture API using actions endpoint!zFailed to connect: Status �, Response: z/Failed to connect to SafetyCulture API: Status z/Network error connecting to SafetyCulture API: N)r r �error� Exception�BASE_URL� FEED_PATHr r �get�status_code�warning�textr �
exceptions�RequestException�str)r �endpoint�response�actions_endpoint�actions_response�es r �test_connectionz#SafetyCultureClient.test_connection@ s9 � � �|�|��L�L�*�+��K�L�L� �m�m�_�A�d�n�n�%5�\�B�� ���,�X�J�7�8� X��|�|�'�'��1�H��K�K�4�X�5I�5I�4J�K�L��#�#�s�*����J�K���%�%��3����!D�X�EY�EY�DZ�[�\����c�d��"@��AU�AU�@V� W�X�X� '+�m�m�_�A�d�n�n�5E�X�#N� ����;�<L�;M�N�O�#'�<�<�#3�#3�4D�#E� �#�/�/�3�6��K�K� e�f��%�1�1�Z�?��N�N�%H�IY�Ie�Ie�Hf�#g�h�#�&D�EU�Ea�Ea�Db�$c�d�d����9�(�:N�:N�9O�|�\d�\i�\i�[j�k�l��"Q�RZ�Rf�Rf�Qg� h�i�i���"�"�3�3� X��L�L�J�3�q�6�(�S�T��M�c�RS�f�X�V�W�W�� X�s'