Skip to main content

Python package to simplify the Mist System APIs usage

Project description

MISTAPI - Python Package to use MIST API

MIT LICENSE

Copyright (c) 2023 Thomas Munzer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Features

This package is build from the Mist OpenAPI specifications and is designed to simplify the use of the Mist APIs with Python scripts.

  • Manage Mist Authentication with login/password (and 2FA if required) or API Token
  • Provide interactive user inputs for login and org/site selections
  • Provide easy access to Mist APIs endpoints and documentation

Installation

This Python Package can be install with pip:

# Linux/macOS
python3 -m pip install mistapi

# Windows
py -m pip install mistapi

Upgrade

# Linux/macOS
python3 -m pip install --upgrade mistapi

# Windows
py -m pip install --upgrade mistapi

Configuration

Configuration is optional. All the required information can be passed as APISession parameter. However, it is possible to set them in an .env file. The location of this file must be provided during when calling the APISession class with the env_file parameter:

> import mistapi
> apisession = mistapi.APISession(enf_file="path/to/the/.env")

Environment Variables

Variable Name Type Default Comment
MIST_HOST string None The Mist Cloud to use. It must be the "api" one (e.g. api.mist.com, api.eu.mist.com, ...)
MIST_APITOKEN string None The API Token to use.
MIST_USER string None The login to use if no API Token is provided (apitoken use is prefered)
MIST_PASSWORD string None The password to use if no API Token is provided (apitoken use is prefered)
CONSOLE_LOG_LEVEL int 20 The minimum log level to display on the console, using logging schema (0 = Disabled, 10 = Debug, 20 = Info, 30 = Warning, 40 = Error, 50 = Critical)
LOGGING_LOG_LEVEL int 10 The minimum log level to log on the file, using logging schema (0 = Disabled, 10 = Debug, 20 = Info, 30 = Warning, 40 = Error, 50 = Critical). This is only used when the script calling mistapi is using Python logging package and is configured to log to a file

An example of the environment file content is:

MIST_HOST = api.mist.com
MIST_APITOKEN = xxxxxx

Usage

Usage examples are available in the mist_library repository.

To use it,

1. APISession must be instanciated:

>>> import mistapi
>>> apisession = mistapi.APISession()

This class accepts different parameters, all optionals:

Parameter Name Type Default Comment
email str None used if login/password is used. Can be defined later
password str None used if login/password is used. Can be defined later
apitoken str None used if API Token is used. Can de defined later
host str None Mist Cloud to reach (e.g. "api.mist.com"). Can de defined later
env_file str None path to the env file to load. See README.md for allowed variables
console_log_leve int 20 The minimum log level to display on the console, using logging schema (0 = Disabled, 10 = Debug, 20 = Info, 30 = Warning, 40 = Error, 50 = Critical)
logging_log_leve int 10 The minimum log level to log on the file, using logging schema (0 = Disabled, 10 = Debug, 20 = Info, 30 = Warning, 40 = Error, 50 = Critical). This is only used when the script calling mistapi is using Python logging package and is configured to log to a file

2. login() function must be called to validate the authentication.

2.1. If the env file is provided and all the required information are valid, the session is validated:

>>> import mistapi
>>> apisession = mistapi.APISession(env_file="~/.mist_env")
>>> apisession.login()

-------------------------------- Authenticated ---------------------------------

Welcome Thomas Munzer!

>>> apisession.get_authentication_status()
True

2.2. If the env file is not provided or does not contain the required valid information, the missing information will be requested:

  • If no host has been configured, an interactive input will ask for it.
>>> apisession.login()

----------------------------- Mist Cloud Selection -----------------------------

0) Europe 01 (host: api.eu.mist.com)
1) Global 01 (host: api.mist.com)
2) Global 02 (host: api.gc1.mist.com)
3) Global 03 (host: api.ac2.mist.com)
4) Global 04 (host: api.gc2.mist.com)

Select a Cloud (0 to 5, or q to exit): 
  • if not authentication (apitoken or email/password) has been configured, an interactive input will ask for it. If login/password authentication is used and 2FA is requested by the Mist Cloud, the 2FA code will be asked.
>>> apisession.login()

--------------------------- Login/Pwd authentication ---------------------------

Login: tmunzer@juniper.net
Password: 
[  INFO   ] Authentication successful!

Two Factor Authentication code required: 122749
[  INFO   ] 2FA authentication successed

-------------------------------- Authenticated ---------------------------------

Welcome Thomas Munzer!

3. It is now possible to request Mist APIs

>>> device_models = mistapi.api.v1.const.device_models.getDeviceModels(apisession)
>>> device_models.url
'https://api.mist.com/api/v1/const/device_models'
>>> device_models.status_code
200
>>> device_models.data
[{'model': 'AP41', 'type': 'ap', 'ap_type': 'aph', 'description': 'AP-41', 'display': 'AP41', 'has_wifi_band5': True, 'has_wifi_band24': True, 'has_scanning_radio': True, 'has_usb': True, 'has_vble': True, 'vble': {'power': 8, 'beacon_rate': 4, 'beams': 8}, 'band24': {'max_clients': 128, 'max_power': 19, 'min_power': 8}, 'fcc_dfs_ok': ...

Usefull functions

  • easily find an Org Id from the current account with mistapi.cli.select_org(apisession)
>>> mistapi.cli.select_org(apisession)

Available organizations:
0) 000_TM-LAB (id: 6374a757-xxxx-xxxx-xxxx-361e45b2d4ac)
...
41) TM-LAB (id: 203d3d02-xxxx-xxxx-xxxx-76896a3330f4)
...

Select an Org (0 to 44, or q to exit): 41
['203d3d02-xxxx-xxxx-xxxx-76896a3330f4']
  • easily find a Site Id from an org with mistapi.cli.select_org(apisession)
>>> mistapi.cli.select_site(apisession, org_id="203d3d02-xxxx-xxxx-xxxx-76896a3330f4")

Available sites:
0) HLAB (id: f5fcbee5-xxxx-xxxx-xxxx-1619ede87879)
...

Select a Site (0 to 6, or q to exit): 0
['f5fcbee5-xxxx-xxxx-xxxx-1619ede87879']
  • get the next page or all the pages from a request For some requests, the Mist Cloud is using pagination to limit the size of the response. The required information the find the next page can either in the HTTP header (headers X-Page-Total, X-Page-Limit and X-Page-Page) or with the next key in the json document. To make it easier to request the next page or all the pages, the mistapi package is prossessing the response to extract or generate the URI to retrieve the next page.
>>> response = mistapi.api.v1.orgs.clients.searchOrgClientsEvents(apisession, my_org_id, duration="1d")
>>> len(response.data["results"])
100
>>> response.next
'/api/v1/orgs/203d3d02-xxxx-xxxx-xxxx-76896a3330f4/clients/events/search?end=1676966894&limit=100&search_after=%5B1676966519626%5D&start=1676880494'

To get the next page, use the mistapi.get_next() function. The returned response will be the same format as the previous one:

>>> response_1 = mistapi.api.v1.orgs.clients.searchOrgClientsEvents(apisession, my_org_id, duration="1d")
>>> len(response_1.data["results"])
100
>>> response_1.next
'/api/v1/orgs/203d3d02-xxxx-xxxx-xxxx-76896a3330f4/clients/events/search?end=1676966894&limit=100&search_after=%5B1676966519626%5D&start=1676880494'
>>> response_2 = response.get_next(apisession, response_1)
>>> response_2.next
'/api/v1/orgs/203d3d02-xxxx-xxxx-xxxx-76896a3330f4/clients/events/search?end=1676966894&limit=100&search_after=%5B1676966204625%5D&start=1676880494'
>>> len(response_2.data["results"])
100

To retrieve all the pages, use the mistapi.get_all() function. The returned response will be a list with the concatained data from all the Mist responses:

>>> response = mistapi.api.v1.orgs.clients.searchOrgClientsEvents(apisession, my_org_id, duration="1d")
>>> len(response.data["results"])
100
>>> response.next
'/api/v1/orgs/203d3d02-xxxx-xxxx-xxxx-76896a3330f4/clients/events/search?end=1676966894&limit=100&search_after=%5B1676966519626%5D&start=1676880494'
>>> data = mistapi.get_all(apisession, response)
>>> len(data)
26027
  • get help on a specific function
>>> help(mistapi.api.v1.orgs.stats.getOrgStats)
Help on function getOrgStats in module mistapi.api.v1.orgs.stats:

getOrgStats(mist_session: mistapi.__api_session.APISession, org_id: str, page: int = 1, limit: int = 100, start: int = None, end: int = None, duration: str = '1d') -> mistapi.__api_response.APIResponse
    API doc: https://doc.mist-lab.fr/#operation/getOrgStats
    
    PARAMS
    -----------
    :param APISession mist_session - mistapi session including authentication and Mist host information
    
    PATH PARAMS
    -----------
    :param str org_id        
    
    QUERY PARAMS
    ------------
    :param int page
    :param int limit
    :param int start
    :param int end
    :param str duration(1d, 1h, 10m)
    
    RETURN
    -----------
    :return APIResponse - response from the API call

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mistapi-0.44.3.tar.gz (77.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mistapi-0.44.3-py3-none-any.whl (215.8 kB view details)

Uploaded Python 3

File details

Details for the file mistapi-0.44.3.tar.gz.

File metadata

  • Download URL: mistapi-0.44.3.tar.gz
  • Upload date:
  • Size: 77.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for mistapi-0.44.3.tar.gz
Algorithm Hash digest
SHA256 3741addd56254872dccd206e2d5230d29a98584e55b0fc5fbe003c6d7f8c2bff
MD5 95448ba2611b0f4cdd80a5f947e21490
BLAKE2b-256 e5c3b83ea247af66171a3acc9e64f2c7e644e82fff0847e4aaba3acf3013d0b5

See more details on using hashes here.

File details

Details for the file mistapi-0.44.3-py3-none-any.whl.

File metadata

  • Download URL: mistapi-0.44.3-py3-none-any.whl
  • Upload date:
  • Size: 215.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for mistapi-0.44.3-py3-none-any.whl
Algorithm Hash digest
SHA256 577ebd99a63434a5d0abcc7aa5534f59cc71ad896d76a5c7cd36001f06105365
MD5 49a9b4d7b81dca92941cf7ab694d9dab
BLAKE2b-256 1c96b8f6a0d9f69457aa3b21fe6b61a07330989808cd50dbabeace9116d3b493

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page