How to Implement POST Method for Unifi Controller API in Grafana Using Infinity Data Source?

Question:
I’ve successfully implemented the GET method of the Unifi Controller API in Grafana using the Infinity data source. However, I’m facing difficulties with using the POST method.

Here is my current Python Flask server code, which handles the login and fetches data using GET and POST methods:

import requests
from requests.exceptions import SSLError, RequestException

app = Flask(__name__)

# Disable SSL warnings from urllib3
requests.packages.urllib3.disable_warnings()

# Constants for API endpoints and credentials
LOGIN_URL = "https://unifi.website.com/api/login"
DEVICE_STATS_URL = "https://unifi.website.com/api/s/lp0csd81/stat/device"
STA_STATS_URL = "https://unifi.website.com/api/s/lp0csd81/stat/sta"
USERNAME = "username"
PASSWORD = "password"

def perform_login():
    login_payload = {
        "strict": True,
        "password": PASSWORD,
        "remember": True,
        "username": USERNAME
    }
    login_headers = {
        'Accept': 'application/json, text/plain, */*',
        'Content-Type': 'application/json',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
    }

    try:
        response = requests.post(LOGIN_URL, headers=login_headers, json=login_payload, verify=False)
        response.raise_for_status()
        return response.cookies.get_dict() if response.status_code == 200 else None

    except (SSLError, RequestException) as err:
        print(f"Error occurred: {err}")
        return None

@app.route('/fetch_device_stats', methods=['GET'])
def fetch_device_stats():
    cookies = perform_login()
    if cookies:
        csrf_token = cookies.get('csrf_token', '')
        unifises = cookies.get('unifises', '')
        headers = {
            'Accept': 'application/json, text/plain, */*',
            'X-Csrf-Token': csrf_token,
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
            'Cookie': f'csrf_token={csrf_token}; unifises={unifises}'
        }

        try:
            response = requests.get(DEVICE_STATS_URL, headers=headers, verify=False)
            response.raise_for_status()
            return jsonify(response.json()) if response.status_code == 200 else f"Failed to fetch device stats. Status code: {response.status_code}"

        except RequestException as err:
            print(f"Error occurred: {err}")
            return f"Request Exception occurred: {err}"

    return "Login failed. Unable to fetch device stats."

@app.route('/fetch_sta_stats', methods=['POST'])
def fetch_sta_stats():
    cookies = perform_login()
    if cookies:
        csrf_token = cookies.get('csrf_token', '')
        unifises = cookies.get('unifises', '')
        headers = {
            'Accept': 'application/json, text/plain, */*',
            'X-Csrf-Token': csrf_token,
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
            'Cookie': f'csrf_token={csrf_token}; unifises={unifises}'
        }

        try:
            response = requests.post(STA_STATS_URL, headers=headers, verify=False)
            response.raise_for_status()
            return jsonify(response.json()) if response.status_code == 200 else f"Failed to fetch STA stats. Status code: {response.status_code}"

        except RequestException as err:
            print(f"Error occurred: {err}")
            return f"Request Exception occurred: {err}"

    return "Login failed. Unable to fetch STA stats."

if __name__ == '__main__':
    app.run(debug=True)

I got to the point where I want to use the POST method of the Unifi Controller API. I saw a request payload indicating the following structure

Here is how the payload looks

{
  "macs": ["b2:1c:b6:25:76:41"]
}

with additional information

0: "b2:1c:b6:25:76:41"

Grafana Infinity Data Source Error Log

Should I create my own payload in this format to use it as a time series but based on MAC addresses? How should I structure my request payload and integrate it with Grafana using the Infinity data source to achieve this?

Any guidance or examples would be greatly appreciated!

Additional Context:

  • I am using Grafana with the Infinity data source. The GET method was
    straightforward to implement, but the POST method is causing issues.
    I want to visualize data based on MAC addresses in a time series
    format.
  • Gunicorn in Flask Server

Thank you in advance for your help!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật