### Development Setup and Testing (Bash) Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Provides essential bash commands for setting up the development environment for the mistapi_python project. This includes cloning the repository, installing dependencies, running tests, and performing linting. ```bash # Clone the repository git clone https://github.com/tmunzer/mistapi_python.git cd mistapi_python # Install with development dependencies pip install -e ".[dev]" # Run tests pytest # Run tests with coverage pytest --cov=src/mistapi --cov-report=html # Run linting ruff check src/ ``` -------------------------------- ### Create and Get Site Statistics (Python) Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Provides Python code examples for creating a new site within an organization and retrieving statistical data for a given site. This involves defining site attributes and using specific API endpoints. ```python # Create a new site site_data = { "name": "New Branch Office", "country_code": "US", "timezone": "America/New_York" } new_site = mistapi.api.v1.orgs.sites.createOrgSite(apisession, org_id, body=site_data) # Get site statistics site_stats = mistapi.api.v1.sites.stats.getSiteStats(apisession, site_id) ``` -------------------------------- ### Install MISTAPI Package Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Installs the MISTAPI Python package using pip. Supports Linux/macOS and Windows environments, as well as installation with development dependencies. ```Bash # Linux/macOS python3 -m pip install mistapi # Windows py -m pip install mistapi # Install with development dependencies (for contributors) pip install mistapi[dev] ``` -------------------------------- ### Configure MistAPI with Environment Variables Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md This example shows a typical `.env` file format for configuring the MistAPI connection. It specifies the MIST_HOST and MIST_APITOKEN, which are essential for establishing a connection to the Mist Cloud. ```bash MIST_HOST=api.mist.com MIST_APITOKEN=your_api_token_here ``` -------------------------------- ### Pagination: Get All Pages Automatically Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Illustrates how to automatically fetch all pages of paginated API results using the `mistapi.get_all` function. This simplifies handling large datasets. ```Python # Get all pages automatically response = mistapi.api.v1.orgs.clients.searchOrgClientsEvents(apisession, org_id, duration="1d") print(f"First page: {len(response.data['results'])} results") all_data = mistapi.get_all(apisession, response) print(f"Total results across all pages: {len(all_data)}") ``` -------------------------------- ### Pagination: Get Next Page of Results Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Demonstrates how to retrieve paginated results from an API call. It shows fetching the first page and then using `mistapi.get_next` to retrieve the subsequent page. ```Python # Get first page response = mistapi.api.v1.orgs.clients.searchOrgClientsEvents(apisession, org_id, duration="1d") print(f"First page: {len(response.data['results'])} results") print(f"Next page URL: {response.next}") # Get next page response_2 = mistapi.get_next(apisession, response) print(f"Second page: {len(response_2.data['results'])} results") ``` -------------------------------- ### List, Get, and Update Devices (Python) Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Demonstrates how to interact with the Mist API to list all devices within an organization, retrieve details for a specific device, and update a device's configuration. Requires an active APISession object and organization ID. ```python # List all devices in an organization devices = mistapi.api.v1.orgs.devices.listOrgDevices(apisession, org_id) # Get specific device details device = mistapi.api.v1.orgs.devices.getOrgDevice(apisession, org_id, device_id) # Update device configuration update_data = {"name": "New Device Name"} result = mistapi.api.v1.orgs.devices.updateOrgDevice(apisession, org_id, device_id, body=update_data) ``` -------------------------------- ### API Usage: Get Device Models Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Shows how to retrieve a list of device models (constants) using the Mist API after successful authentication. It prints the status code, URL, and the number of models found. ```Python # Get device models (constants) device_models = mistapi.api.v1.const.device_models.getDeviceModels(apisession) print(f"Status: {device_models.status_code}") print(f"URL: {device_models.url}") print(f"Data: {len(device_models.data)} models") ``` -------------------------------- ### API Usage: Get Organization Statistics Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Demonstrates how to fetch statistics for a specific organization, such as the number of sites it contains. This requires the organization ID. ```Python # Get organization statistics org_stats = mistapi.api.v1.orgs.stats.getOrgStats(apisession, org_id) print(f"Organization has {org_stats.data['num_sites']} sites") ``` -------------------------------- ### Search Wireless Clients and Get Events (Python) Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Illustrates how to search for wireless clients within an organization over a specified duration and retrieve events associated with a particular client MAC address. Requires an APISession and organization ID. ```python # Search for wireless clients clients = mistapi.api.v1.orgs.clients.searchOrgWirelessClients( apisession, org_id, duration="1d", limit=100 ) # Get client events events = mistapi.api.v1.orgs.clients.searchOrgClientsEvents( apisession, org_id, duration="1h", client_mac="aa:bb:cc:dd:ee:ff" ) ``` -------------------------------- ### Error Handling: Catching API Exceptions Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Demonstrates how to handle potential errors during API calls using a try-except block. This example catches exceptions when an invalid organization ID is used. ```Python try: org_info = mistapi.api.v1.orgs.orgs.getOrg(apisession, "invalid-org-id") except Exception as e: print(f"API Error: {e}") ``` -------------------------------- ### Initialize APISession in Python Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Demonstrates how to initialize the APISession class from the mistapi library. This class can be configured with various optional parameters for authentication, host, proxy settings, and logging levels. ```python import mistapi apisession = mistapi.APISession() ``` -------------------------------- ### API Help Function Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Shows how to access help documentation for a specific Mist API function, such as `getOrgStats`. This provides details on parameters, return values, and usage. ```Python help(mistapi.api.v1.orgs.stats.getOrgStats) ``` -------------------------------- ### CLI Site Selection Prompt Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Displays the interactive prompt for selecting a site within an organization using the CLI helper. It lists available sites and requests user input. ```Shell Available sites: 0) Headquarters (id: f5fcbee5-xxxx-xxxx-xxxx-1619ede87879) 1) Branch Office (id: a8b2c3d4-xxxx-xxxx-xxxx-987654321abc) ... Select a Site (0 to 1, or q to exit): 0 ``` -------------------------------- ### Initialize and Authenticate MistAPI Session Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md This Python code demonstrates how to initialize an APISession object and authenticate with the Mist API. It then proceeds to fetch device models and print the count. Finally, it shows how to interactively select an organization and retrieve its information. ```python import mistapi # Initialize session apisession = mistapi.APISession() # Authenticate apisession.login() # Use the API device_models = mistapi.api.v1.const.device_models.getDeviceModels(apisession) print(f"Found {len(device_models.data)} device models") # Interactive org selection org_id = mistapi.cli.select_org(apisession)[0] # Get organization information org_info = mistapi.api.v1.orgs.orgs.getOrg(apisession, org_id) print(f"Organization: {org_info.data['name']}") ``` -------------------------------- ### Interactive Authentication and Cloud Selection Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Illustrates the interactive authentication process when credentials are not provided. This includes selecting a Mist cloud from a list and entering login credentials, followed by a two-factor authentication code. ```Shell ----------------------------- Mist Cloud Selection ----------------------------- 0) APAC 01 (host: api.ac5.mist.com) 1) APAC 03 (host: api.gc7.mist.com) 2) EMEA 01 (host: api.eu.mist.com) 3) EMEA 02 (host: api.gc3.mist.com) 4) EMEA 03 (host: api.ac6.mist.com) 5) EMEA 04 (host: api.gc6.mist.com) 6) Global 01 (host: api.mist.com) 7) Global 02 (host: api.gc1.mist.com) 8) Global 03 (host: api.ac2.mist.com) 9) Global 04 (host: api.gc2.mist.com) 10) Global 05 (host: api.gc4.mist.com) Select a Cloud (0 to 10, or q to exit): ``` ```Shell --------------------------- Login/Pwd authentication --------------------------- Login: user@example.com Password: [ INFO ] Authentication successful! Two Factor Authentication code required: 123456 [ INFO ] 2FA authentication succeeded -------------------------------- Authenticated --------------------------------- Welcome Thomas Munzer! ``` -------------------------------- ### Automatic Authentication with Environment File Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Demonstrates how to authenticate with the Mist API automatically using credentials stored in an environment file. It initializes an APISession and logs in, then prints the authentication status. ```Python import mistapi apissession = mistapi.APISession(env_file="~/.mist_env") apissession.login() # Output: # -------------------------------- Authenticated --------------------------------- # Welcome Thomas Munzer! print(apisession.get_authentication_status()) # True ``` -------------------------------- ### Configure MISTAPI with .env File Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Demonstrates how to configure the MISTAPI package by specifying the path to an .env file when initializing the APISession class. This allows for secure credential management. ```Python import mistapiapisession = mistapi.APISession(env_file="path/to/the/.env") ``` -------------------------------- ### CLI Helper: Select Site within Organization Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Illustrates how to use a CLI helper function to select a specific site within a given organization. It requires the `apisession` and `org_id` as parameters. ```Python # Select site within an organization site_ids = mistapi.cli.select_site(apisession, org_id="203d3d02-xxxx-xxxx-xxxx-76896a3330f4") print(f"Selected site: {site_ids[0]}") ``` -------------------------------- ### API Usage: Search for Devices Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Illustrates how to search for devices within an organization, filtering by type (e.g., 'ap'). It prints the number of results found. ```Python # Search for devices devices = mistapi.api.v1.orgs.devices.searchOrgDevices(apisession, org_id, type="ap") print(f"Found {len(devices.data['results'])} access points") ``` -------------------------------- ### CLI Organization Selection Prompt Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Shows the interactive prompt presented to the user when selecting an organization via the CLI helper function. It lists available organizations and asks for input. ```Shell Available organizations: 0) Acme Corp (id: 203d3d02-xxxx-xxxx-xxxx-76896a3330f4) 1) Demo Lab (id: 6374a757-xxxx-xxxx-xxxx-361e45b2d4ac) ... Select an Org (0 to 2, or q to exit): 0 ``` -------------------------------- ### Upgrade MISTAPI Package Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Upgrades the MISTAPI Python package to the latest version using pip. Supports Linux/macOS and Windows environments. ```Bash # Linux/macOS python3 -m pip install --upgrade mistapi # Windows py -m pip install --upgrade mistapi ``` -------------------------------- ### CLI Helper: Select Multiple Organizations Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Demonstrates using a CLI helper function to select multiple organizations. The `allow_many=True` parameter enables this functionality. ```Python # Select multiple organizations org_ids = mistapi.cli.select_org(apisession, allow_many=True) print(f"Selected {len(org_ids)} organizations") ``` -------------------------------- ### CLI Helper: Select Single Organization Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Provides a CLI helper function to select a single organization from a list. It displays available organizations with their IDs and prompts the user for a selection. ```Python # Select single organization org_ids = mistapi.cli.select_org(apisession) print(f"Selected org: {org_ids[0]}") ``` -------------------------------- ### Error Handling: Checking Response Status Code Source: https://github.com/tmunzer/mistapi_python/blob/main/README.md Illustrates how to check the status code of an API response to determine success or failure. It prints a success message with the number of organizations or an error message with the status code. ```Python # Check response status response = mistapi.api.v1.orgs.orgs.listOrgs(apisession) if response.status_code == 200: print(f"Success: {len(response.data)} organizations") else: print(f"Error {response.status_code}: {response.data}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.