### Initialize Flespi Client and Fetch Devices Source: https://github.com/goldenm-software/flespi-python/blob/main/README.md Demonstrates how to import the FlespiClient, authenticate with a token, and perform a GET request to retrieve all devices. The client handles the underlying REST communication. ```python from flespi import FlespiClient token = 'your_token' flespi = FlespiClient(token) response = flespi.get('/gw/devices/all') print(response) ``` -------------------------------- ### Install Flespi via pip Source: https://github.com/goldenm-software/flespi-python/blob/main/README.md Command to install the Flespi library using the Python package manager. This is a prerequisite for using the client in any Python project. ```bash $ pip3 install flespi ``` -------------------------------- ### Retrieve Resources via GET Requests Source: https://context7.com/goldenm-software/flespi-python/llms.txt Shows how to perform GET requests to fetch devices, telemetry, and channel information. Includes logic for checking response errors and parsing the returned JSON data. ```python from flespi import FlespiClient flespi = FlespiClient('your_token') response = flespi.get('/gw/devices/all') if response['error']: print(f"Request failed with code: {response.get('code')}") else: devices = response['message']['result'] for device in devices: print(f"Device: {device['name']} (ID: {device['id']})") ``` -------------------------------- ### Perform GET Request Source: https://context7.com/goldenm-software/flespi-python/llms.txt Retrieves data from a Flespi API endpoint using the GET method. The response is a dictionary containing error status, HTTP code, and the message payload. ```python response = flespi.get('/gw/devices/all') ``` -------------------------------- ### Safe API Call Function Source: https://context7.com/goldenm-software/flespi-python/llms.txt A utility function to safely execute Flespi API requests (GET, POST, PUT, DELETE) and handle potential network or API errors gracefully. It returns the API response message on success or None on failure. ```python def safe_api_call(flespi, method, endpoint, params=None): if method == 'GET': response = flespi.get(endpoint) elif method == 'POST': response = flespi.post(endpoint, params) elif method == 'PUT': response = flespi.put(endpoint, params) elif method == 'DELETE': response = flespi.delete(endpoint) if response['error']: if 'reason' in response: # Network/connection error print(f"Connection error: {response['reason']}") else: # API returned an error print(f"API error {response['code']}: {response['message']}") return None return response['message'] # Usage # result = safe_api_call(flespi, 'GET', '/gw/devices/all') # if result: # print(f"Found {len(result['result'])} devices") ``` -------------------------------- ### Initialize FlespiClient for API Authentication Source: https://context7.com/goldenm-software/flespi-python/llms.txt Demonstrates how to instantiate the FlespiClient using a valid Flespi token. It also shows how to enable debug logging to monitor request and response details. ```python from flespi import FlespiClient import logging token = 'your_flespi_token_here' flespi = FlespiClient(token) logging.basicConfig(level=logging.DEBUG) logging.getLogger('flespi.rest.client').setLevel(logging.DEBUG) ``` -------------------------------- ### Create Resources via POST Requests Source: https://context7.com/goldenm-software/flespi-python/llms.txt Demonstrates creating new devices, channels, or triggering commands by sending a JSON payload via a POST request. ```python from flespi import FlespiClient flespi = FlespiClient('your_token') device_params = {'name': 'My GPS Tracker', 'device_type_id': 123, 'configuration': {'ident': 'IMEI123456789'}} response = flespi.post('/gw/devices', device_params) if not response['error']: print(f"Created device with ID: {response['message']['result'][0]['id']}") ``` -------------------------------- ### Initialize Flespi Client Source: https://context7.com/goldenm-software/flespi-python/llms.txt Initializes the FlespiClient with an API token. This client instance is then used to make requests to the Flespi API. ```python from flespi import FlespiClient flespi = FlespiClient('your_token') ``` -------------------------------- ### Remove Resources via DELETE Requests Source: https://context7.com/goldenm-software/flespi-python/llms.txt Demonstrates how to delete resources from the Flespi platform by providing the resource path, including support for bulk deletion via comma-separated IDs. ```python from flespi import FlespiClient flespi = FlespiClient('your_token') response = flespi.delete('/gw/devices/123') if not response['error']: print("Device deleted successfully") # Bulk delete flespi.delete('/gw/devices/123,456,789') ``` -------------------------------- ### Update Resources via PUT Requests Source: https://context7.com/goldenm-software/flespi-python/llms.txt Explains how to update existing Flespi resources such as device configurations or channel settings using the PUT method. ```python from flespi import FlespiClient flespi = FlespiClient('your_token') update_params = {'name': 'Updated Device Name', 'configuration': {'ident': 'IMEI123456789', 'settings_polling': 300}} response = flespi.put('/gw/devices/123', update_params) if not response['error']: print("Device updated successfully") ``` -------------------------------- ### Flespi API Response Structure Source: https://context7.com/goldenm-software/flespi-python/llms.txt Illustrates the expected dictionary format for both successful and error responses from the Flespi API. This structure aids in consistent error handling. ```python # Successful response structure: # { # 'error': False, # 'code': 200, # 'message': {'result': [...]} # } # Error response structure (API error): # { # 'error': True, # 'code': 401, # 'message': {'errors': [{'reason': 'Unauthorized'}]} # } # Error response structure (network/request error): # { # 'error': True, # 'reason': # } ``` -------------------------------- ### Delete Old Device Messages Source: https://context7.com/goldenm-software/flespi-python/llms.txt Deletes messages for a specific device that are older than a given Unix timestamp. This is useful for data retention policies. ```python flespi.delete('/gw/devices/123/messages?data={"ts_lt":1609459200}') ``` -------------------------------- ### Delete Flespi Channel Source: https://context7.com/goldenm-software/flespi-python/llms.txt Deletes a specific channel from Flespi using its ID. This operation requires a valid API token with appropriate permissions. ```python flespi.delete('/gw/channels/456') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.