pythonaruba

how do you pull Aruba reports using API calls via Python


I need to login to aruba appliance and get report data using the following api at aruba-airwave endpoint.

I have this code so far:

import xml.etree.ElementTree as ET # libxml2 and libxslt
import requests                    # HTTP requests
import csv
import sys

# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------

# Login/password for Airwave (read-only account)
LOGIN = 'operator'
PASSWD = 'verylongpasswordforyourreadonlyaccount'

# URL for REST API
LOGIN_URL = 'https://aruba-airwave.example.com/LOGIN'
AP_LIST_URL = 'https://aruba-airwave.example.com/report_detail?id=100'

# Delimiter for CSV output
DELIMITER = ';'

# HTTP headers for each HTTP request
HEADERS = {
    'Content-Type' : 'application/x-www-form-urlencoded',
    'Cache-Control' : 'no-cache'
}

# ---------------------------------------------------------------------------
# Fonctions
# ---------------------------------------------------------------------------

def open_session():
    """Open HTTPS session with login"""

    ampsession = requests.Session()
    data = 'credential_0={0}&credential_1={1}&destination=/&login=Log In'.format(LOGIN, PASSWD)
    loginamp = ampsession.post(LOGIN_URL, headers=HEADERS, data=data)
    return {'session' : ampsession, 'login' : loginamp}

def get_ap_list(session):
      output=session.get(AP_LIST_URL, headers=HEADERS, verify=False)
      ap_list_output=output.content
      return ap_list_output

def main():
   session=open_session()
   get_ap_list(session)
main()

I get this error:

get() takes no keyword arguments

any ideas what I am doing wrong here or suggestion for another method?


Solution

  • The problem you are running into is because your open_session() function returns a dict and a dicts args are typically for accessing a key within the dict.

    since your session parameter is a dict in this case, if you want to use the elements in session, you need to access them by their key that you defined in open_session().

    # session['session'].get(include your arguments for the get request here) or
    # session['login'].get()
    # although the data attributed to your already obtained session
    # should be handled by the session hence storing the login result
    # isn't really necessary unless you are checking to see if the
    # request was successful but that can be done other ways without
    # actually storing the result.
    
    def get_ap_list(session):
          output=session['session'].get(AP_LIST_URL, headers=HEADERS, verify=False)
          ap_list_output=output.content
          return ap_list_output
    

    I haven't verified that this code works however it does address the error you are currently receiving.