Location Services

last person joined: 11 days ago 

Location-based mobile app development and Bluetooth-based asset tracking with Meridian. Gathering analytics and business intelligence from Wi-Fi with Analytics and Location Engine (ALE).

Using the Meridian API: Caching Maps Data in a file

This thread has been viewed 1 times
  • 1.  Using the Meridian API: Caching Maps Data in a file

    Posted Apr 23, 2019 05:05 AM
      |   view attached

    To speed up working with the data returned by the Meridian Maps 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 Python and 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


    The Meridian APIs used in this project are:

    Locations Maps API Endpoint https://docs.meridianapps.com/article/484-using-the-maps-api


    To Do:

    1. Download the getBeaconsToFile_lesson.py (it’s zipped).
    2. Open the python file in 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 ‘maps.json'.
    • Lines 9 to 11 set up the API URLs for the location and maps API.
    • Line 12 creates a list object called ‘maps_info’.
    • Lines 14 to 19 set up the formatting for the API request.
    • Lines 21 to 23 requests information from the Location API, parses it and then displays the location name.
    • Line 24 informs that the collection of data from the Maps API is occuring and it may take some time.
    • Line 25 prints a blank line.
    • Lines 28 to 39 sets up the ‘get_maps’ function.
      • Line 31 requests data from the Maps API.
      • Line 32 parses the response.
      • Line 33 adds the parsed data to the ‘maps_info’ list that was created in Line 12.
      • Line 34 is an if statement which tests to see if the ‘next’ field is not equal to ‘None’.
        • When this is the case the ‘maps_api' URL is replaced with the URL found in the ‘next’ field of the response.
        • The function ‘get_maps’ is run again. This time it uses the new ‘maps_api’ URL to request new data.
      • Line 38 is the else part of the if statement from Line 34. If ‘next’ is equal to ‘None’ then the function ‘write_maps_file’ is called.
    • Lines 42 to 55 sets up the ‘write_maps_file’ function.
      • Line 44 opens the outfile ‘maps.json’ as specified in Line 7. The file is set to write.
      • Line 45 dumps the JSON data from ‘maps_info’ in to the ‘maps.json’ file.
      • Line 46 just prints what is going on “Writing data to maps.json”.
      • Line 47 closes the file. That’s important. Line 48 tells us it’s closed.
      • Line 50 opens the same file ‘maps.json’. This time it is opened as read-only.
      • Line 51 loads the contents of the ‘maps.json’ file in to the object 'maplist'.
      • Line 52 closes the file. We don’t need it any more because it’s contents are in memory at ‘maplist’.
        • Funny though, it’s already in ‘maps_info’. It’s redundant but just included for demonstration.
      • Line 53 and 54 prints a status update.
      • Line 55 exits the code at the conclusion of the ‘write_maps_file’ function.
    • Line 58 calls the ‘get_maps’ function. This is where it all begins.
    • Lines 61 and 62 are there for demonstration. They can be uncommented to print a list of map names.


    Now that you have a file including the data from the Maps 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 Using the Meridian API: Caching Beacons Data in a file 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.