LongtermClient
The code in the LongtermClient file defines a class YetiForceLongTermClient that interacts with the YetiForce API to manage and retrieve information about users, classrooms, and weekly reports.
Key Components:
-
Imports:
- Standard libraries such as
requests,json,time, andloggingare used for making HTTP requests, managing time, and logging. - Local modules such as
AuthUser,ApiKey,LongTermModules, andEnglishLevelsare used for user authentication, managing API keys, and defining specific constants or data structures used in requests.
- Standard libraries such as
-
Logging Setup:
- A custom logger is set up using the
setup_loggerfunction to log detailed information with color-coded log messages (for different log levels likeINFO,DEBUG,ERROR). - The log file is named
YetiForceLongTermClient.log.
- A custom logger is set up using the
-
YetiForceLongTermClient Class:
-
Initialization (
__init__): Sets up the connection with YetiForce API usingbase_url,api_key,x_api_key, andapi_user. It logs the user in and stores a token for subsequent API requests. -
Authentication and Token Management:
- The class manages authentication with the YetiForce API through
database_user_loginand refreshes tokens using_RenewTokenwhen necessary. - The
_requesttoyetimethod makes API requests, automatically handling token renewal if necessary.
- The class manages authentication with the YetiForce API through
-
-
Core Methods:
- API Interaction: Methods like
get_all_users,get_user_by_query, andget_related_fieldsare used to retrieve data about users, classrooms, and related records from the YetiForce API. - User Management:
CreateUsersandCreateApiKeycreate user and API key instances for authentication and interaction with the API. - Error Handling: The methods raise custom exceptions (
AuthenticationError,TokenError,APIError) when something goes wrong.
- API Interaction: Methods like
-
Creating and Editing Records:
- The class provides methods for creating new entries (
_create_entry) or editing existing ones (_edit_entry) in different modules likeLongTermModules.WeeklyReportandLongTermModules.Classrooms.
- The class provides methods for creating new entries (
-
Creating a Weekly Report:
- The
create_weekly_reportmethod constructs a weekly report using student information (SID, PIN), the English level of the student, test results, and other feedback fields, and sends it to the YetiForce API to create a new report.
- The
-
Additional Utility Methods:
- Methods like
get_students_classrooms,getAllClassrooms, andget_user_by_queryhelp retrieve specific classroom or user data, which can be used for generating reports or other purposes.
- Methods like
-
Error Handling and Logging:
- Each method includes detailed logging (e.g., logging success, errors, and warnings) to trace the flow of data and troubleshoot issues.
- Errors are raised if tokens are missing or invalid, or if there's a failure in making API requests.
Functionality in Detail:
-
Authentication and Token Management: The class starts by logging in to the YetiForce API using provided credentials. If the token expires, it automatically renews the token for future requests.
-
API Requests: The class interacts with various YetiForce modules like "Users", "Classrooms", and "WeeklyReport" using the
_requesttoyetimethod, which makes HTTP requests with the appropriate method (GET,POST, etc.) and headers. -
Creating and Fetching Data: The client can create new records (like users, API keys, weekly reports) and fetch existing records from the YetiForce API. This is done through methods such as
get_all_users,get_user_by_query, andcreate_weekly_report. -
Error Handling:
- The class defines custom exceptions like
AuthenticationError,TokenError, andAPIErrorto handle various errors that may occur during the interaction with the YetiForce API. - Errors during the API calls are logged and handled appropriately.
- The class defines custom exceptions like
Summary:
In essence, the YetiForceLongTermClient class acts as a wrapper for interacting with the YetiForce API. It handles user authentication, token management, error handling, and various CRUD operations on user data, classrooms, and reports. The class is designed to work with long-term data for users in the YetiForce CRM system, making it useful for managing user information, generating reports, and performing other routine administrative tasks via API.