# AUTO-GENERATED FILE - DO NOT EDIT
# This file was automatically generated by the XDK build tool.
# Any manual changes will be overwritten on the next generation.

"""
Auto-generated communities client for the X API.

This module provides a client for interacting with the communities endpoints of the X API.

All methods, parameters, and response models are generated from the OpenAPI specification.

Generated automatically - do not edit manually.
"""

from __future__ import annotations
from typing import Dict, List, Optional, Any, Union, cast, TYPE_CHECKING
import requests
import time


if TYPE_CHECKING:
    from ..client import Client
from .models import (
    GetByIdResponse,
    SearchResponse,
)


class CommunitiesClient:
    """Client for communities operations"""


    def __init__(self, client: Client):
        self.client = client


    def get_by_id(self, id: Any, community_fields: List = None) -> GetByIdResponse:
        """
        Get Community by ID
        Retrieves details of a specific Community by its ID.
        Args:
            id: The ID of the Community.
            community_fields: A comma separated list of Community fields to display.
            Returns:
            GetByIdResponse: Response data
        """
        url = self.client.base_url + "/2/communities/{id}"
        url = url.replace("{id}", str(id))
        if self.client.bearer_token:
            self.client.session.headers["Authorization"] = (
                f"Bearer {self.client.bearer_token}"
            )
        elif self.client.access_token:
            self.client.session.headers["Authorization"] = (
                f"Bearer {self.client.access_token}"
            )
        # Ensure we have a valid access token
        if self.client.oauth2_auth and self.client.token:
            # Check if token needs refresh
            if self.client.is_token_expired():
                self.client.refresh_token()
        params = {}
        if community_fields is not None:
            params["community.fields"] = ",".join(
                str(item) for item in community_fields
            )
        headers = {}
        # Prepare request data
        json_data = None
        # Make the request
        response = self.client.session.get(
            url,
            params=params,
            headers=headers,
        )
        # Check for errors
        response.raise_for_status()
        # Parse the response data
        response_data = response.json()
        # Convert to Pydantic model if applicable
        return GetByIdResponse.model_validate(response_data)


    def search(
        self,
        query: str,
        max_results: int = None,
        next_token: Any = None,
        pagination_token: Any = None,
        community_fields: List = None,
    ) -> SearchResponse:
        """
        Search Communities
        Retrieves a list of Communities matching the specified search query.
        Args:
            query: Query to search communities.
            max_results: The maximum number of search results to be returned by a request.
            next_token: This parameter is used to get the next 'page' of results. The value used with the parameter is pulled directly from the response provided by the API, and should not be modified.
            pagination_token: This parameter is used to get the next 'page' of results. The value used with the parameter is pulled directly from the response provided by the API, and should not be modified.
            community_fields: A comma separated list of Community fields to display.
            Returns:
            SearchResponse: Response data
        """
        url = self.client.base_url + "/2/communities/search"
        # Ensure we have a valid access token
        if self.client.oauth2_auth and self.client.token:
            # Check if token needs refresh
            if self.client.is_token_expired():
                self.client.refresh_token()
        params = {}
        if query is not None:
            params["query"] = query
        if max_results is not None:
            params["max_results"] = max_results
        if next_token is not None:
            params["next_token"] = next_token
        if pagination_token is not None:
            params["pagination_token"] = pagination_token
        if community_fields is not None:
            params["community.fields"] = ",".join(
                str(item) for item in community_fields
            )
        headers = {}
        # Prepare request data
        json_data = None
        # Make the request
        if self.client.oauth2_session:
            response = self.client.oauth2_session.get(
                url,
                params=params,
                headers=headers,
            )
        else:
            response = self.client.session.get(
                url,
                params=params,
                headers=headers,
            )
        # Check for errors
        response.raise_for_status()
        # Parse the response data
        response_data = response.json()
        # Convert to Pydantic model if applicable
        return SearchResponse.model_validate(response_data)
