Skip to main content

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:

  1. Imports:

    • Standard libraries such as requests, json, time, and logging are used for making HTTP requests, managing time, and logging.
    • Local modules such as AuthUser, ApiKey, LongTermModules, and EnglishLevels are used for user authentication, managing API keys, and defining specific constants or data structures used in requests.
  2. Logging Setup:

    • A custom logger is set up using the setup_logger function to log detailed information with color-coded log messages (for different log levels like INFO, DEBUG, ERROR).
    • The log file is named YetiForceLongTermClient.log.
  3. YetiForceLongTermClient Class:

    • Initialization (__init__): Sets up the connection with YetiForce API using base_url, api_key, x_api_key, and api_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_login and refreshes tokens using _RenewToken when necessary.
      • The _requesttoyeti method makes API requests, automatically handling token renewal if necessary.
  4. Core Methods:

    • API Interaction: Methods like get_all_users, get_user_by_query, and get_related_fields are used to retrieve data about users, classrooms, and related records from the YetiForce API.
    • User Management: CreateUsers and CreateApiKey create 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.
  5. Creating and Editing Records:

    • The class provides methods for creating new entries (_create_entry) or editing existing ones (_edit_entry) in different modules like LongTermModules.WeeklyReport and LongTermModules.Classrooms.
  6. Creating a Weekly Report:

    • The create_weekly_report method 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.
  7. Additional Utility Methods:

    • Methods like get_students_classrooms, getAllClassrooms, and get_user_by_query help retrieve specific classroom or user data, which can be used for generating reports or other purposes.
  8. 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 _requesttoyeti method, 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, and create_weekly_report.

  • Error Handling:

    • The class defines custom exceptions like AuthenticationError, TokenError, and APIError to handle various errors that may occur during the interaction with the YetiForce API.
    • Errors during the API calls are logged and handled appropriately.

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.