Reply
Contributor II

Using the Meridian API: Caching Beacons Data in a file

To speed up working with the data returned by the Meridian Beacons API I used this Python script to store the response locally in a file.
 
This note assumes you have some basic knowledge of using python. Check out these resources that may help you to get started: Python and REST APIs 1: Building a work environment, Python and REST APIs 2: GET Data and Pythonand REST APIs 3: POST Data.
 
NOTE: It's important to understand the API as much as possible before using it beyond the scope of this article. Data can be overwriten with the API and so caution should be taken. Also, Only documented APIs are supported by Aruba.  https://docs.meridianapps.com/article/369-rest-api 
 
 
To Do:
  1. Download the getBeaconsToFile_lesson.py (it’s zipped).
  2. Open the python file onin your favourite editor (I use PyCharm).
  3. Step through the file with the following breakdown to see what it does.
 
Break it Down:
  • Lines 1 and 2 import the modules requests and json.
  • Line 4 sets the variable for location, this needs to be edited with the appropriate location ID for your environment.
  • Line 5 sets the variable for authtoken, this needs to be edited with an authentication token with appropriate access rights to your location.
  • Line 7 sets the name of the file which will be saved with the data from the API. In this case the name is beacons.json.
  • Line 8 sets up a variable api_call with a value of 1. This variable will count the times the API GET request takes place.
  • Line 9 creates an empty list called beacons_datab. This is the list where all the beacons data will be written once collected and ultimately the source of data that is written out to the beacons.json file.
  • Line 11-13 set up the API URLs.
  • Lines 15-20 set up the formatting for the API request.
  • Line 22 sets the variable location_info and requests basic location API info as the value.
  • Line 23 parses location_info.
  • Line 24 prints the location name.
  • Line 25 prints the Beacon API URL - mainly for troubleshooting purposes and verbosity.
  • Lines 28-39 define the get_beacons function.
    • The get_beacons function requests data from the Beacons API, parses it and stores it in the beacons_datab list with the extend command. 
      • Extend adds to an existing list at the end.
    • The api_call counter is used to display the round of API GET request being processed and its incremented up afterwards.
    • Finally the check_for_more function is called.
  • Lines 42-55 defines the write_beacon_file function.
    • The outfile called beacons.jasn is opened ready to be written to in Line 44.
    • Line 45 writes the beacons_datab by converting the json response to a string.
    • Line 46 just lets you know what is going on.
    • Line 47 closes the file, because that’s the right thing to do. You can’t use it again until it’s closed.
    • Line 50-52 opens the file as read-only, and loads the contents into a variable called beaconlist. Essentially beaconlist is the same as beacons_datab so this step is a bit redundant - it just shows the process.
    • Line 53 counts the number of Beacons in beaconslist and prints it.
  • Lines 58-66 defines the check_for_more function.
    • This function checks the ‘next’ field of the Beacons API response. When it’s “None” there are no more Beacons to get data for but otherwise it will contain a URL for the next page of results. By default only 100 beacons are returned at once but this can be changed in beacons_api.
    • There is an if statement checking next. If next isn’t None then change beacons_api to whatever ‘next’ is and run the get_beacons function.
    • Otherwise (if ‘next’ is None) then inform by printing a note then runt he write_beacons_file function.
      • This will only run once when all the beacons data has been collected.
  • Line 69 calls the get_beacons function. This is where it all beacons, at the end.

Now that you have a file including the data from the Beacons API you can use this as a locally cached version for speedier access in other Python scripts. Accessing the data in this way is much faster than calling on the API every time you need it.

 

For another example of saving data locally like this check out Go here to see the Python example for caching Maps data and Using the Meridian API: Storing the JSON Output (Local Caching)

 

Let me know in the comments below if you would like to see any other examples.

 
Search Airheads
cancel
Showing results for 
Search instead for 
Did you mean: