What is the best method for performing a lookup based on an API response? To then modify the JSON data passed to the API based on that lookup?

Question:

I am new to Python, JSON, and API requests. I have spent time the past week doing small exercises and am now ready to dive in(well thought I was anyway). I would love to continue learning about all this to create my own personal projects in the future, but have felt discouraged today just trying to understand how to tackle my first project.

I am working with a GPS application that has an API. The end goal is to take a list of truck numbers given to me by someone in a CSV format, pass each truck numbers to the API or a stored dictionary/list to perform a lookup, as the truck number we use is different than the id of the truck that the API uses to identify the truck, convert the truck number to the ID, then pass that id to a new URL which then applies a tag in the API to that ID.

Generally this would be for around 70-80 trucks each day. I would then do the reverse at the end of each day to remove the tag from each id.

I am successful when simply entering the id number using this code below, and have tried watching several videos and documentation, but cannot wrap my head around how to do this with multiple trucks and converting from truck number to ID. Any and all assistance is appreciated. Apologies for my ignorance up front.

First I needed to find the id number of each truck in the csv based on the truck number. I performed the following code to retrieve all the trucks, and manually reviewed the response to find the id number for one truck.

import json
import requests
url = 'https://YYY.YYY.com/comGpsGate/api/v.1/applications/46/tags/659/users?FromIndex=0&PageSize=1000'
headers = {'Authorization': "YYY"}
r = requests.get(url, headers=headers)
data = json.loads(r.text)
print(data)
###that prints out a list of each user and their variables, it looks like this
[{'trackPoint': {'position': {'altitude': YYYY, 'longitude': -YYYY, 'latitude': YYYY}, 'velocity': {'groundSpeed': 0.0, 'heading': 19.0}, 'utc': '2022-07-28T21:46:52Z', 'valid': True}, 'calculatedSpeed': 0.0, 'deviceActivity': '2022-07-28T21:46:50Z', 'lastTransport': 'udp', 'username': 'SW5151', 'name': '5151', 'surname': '', 'description': 'ASL Truck', 'email': '', 'devices': [{'id': 542, 'created': '0001-01-01T00:00:00', 'name': 'CalAmp LMU2600', 'hidePosition': False, 'proximity': 0.0, 'imei': 'YYYY', 'msisdn': {'raw': ''}, 'email': '', 'apn': '', 'gprsUsername': '', 'gprsPassword': '', 'lastIP': '', 'lastPort': 0, 'staticIP': '', 'staticPort': 0, 'protocolID': 'CalAmp', 'profileId': 0, 'protocolVersionID': 0, 'msgFieldDictionaryID': 39, 'deviceDefinitionID': 21, 'mobileNetworkID': 0, 'longitude': 0.0, 'latitude': 0.0, 'timeStamp': '0001-01-01T00:00:00', 'ownerID': 767, 'ownerUsername': '', 'ownerName': '', 'ownerEmail': '', 'devicePassword': '', 'oneWireVariables': []}], 'userTemplateID': 32, 'icon': {'iconOffsetX': 11, 'iconOffsetY': 19, 'iconGUID': 'c219b87971d82284a812198cb84eec69', 'rotatable': True}, 'id': 767}
###So the truck number is 5151 listed as name, the id is 767.


###Update Truck Tag Code, this code works as intended.
import requests
import json
url = 'https://YYY.YYY.com/comGpsGate/api/v.1/applications/46/tags/2259/users'
headers = {'Authorization': "YYY"}
#id below can be modified using the converted truck number to id
json_data = {
    'id':767,
}
req = requests.post(url, headers=headers, json=json_data).json()
jsstring = json.dumps(req, indent=4)
print(jsstring)

Following along with a video, I was able to create a dictionary or a list? Honestly unsure which it creates. This would allow me to perhaps use a csv file to reference the id based on the truck number in the csv. Unsure on how to do that.

###create a reference of sort, stumped myself on this one
url = 'https://YYY.YYY.com/comGpsGate/api/v.1/applications/46/tags/659/users?FromIndex=0&PageSize=1000'
headers = {'Authorization': "YYY"}
r = requests.get(url, headers=headers)
data = json.loads(r.text)

user_list = []
for user in data:
    name = user['name']
    id = user['id']
    user_conv = {
        'name': name,
        'id': id
    }
    user_list.append(user_conv)
#kinda got lost here on how to use my truck numbers to convert then pass back

From here I though maybe I should maybe a excel workbook to convert based on lookup, but feel I shouldnt, as the truck names could change, and my data would be out of date, vs polling the API first, creating the dict or list and moving from there.

Once again any input is appreciated, even if you want to make fun of me, as it is well deserved.

Asked By: Yorapios

||

Answers:

Sounds like you want to create a mapping between truck names and their associated IDs.

The first API endpoint seems to return a list of trucks, where each truck has a name and an ID. Just hit that endpoint, and use the response to construct a dictionary (what I’m calling trucks in this example) that maps a given truck name to its ID.

url = "https://YYY.YYY.com/comGpsGate/api/v.1/applications/46/tags/659/users?FromIndex=0&PageSize=1000"

headers = {
    "Authorization": "YYY"
}

response = requests.get(url, headers=headers)
response.raise_for_status()

trucks = {truck["name"]: truck["id"] for truck in response.json()}

You can then use trucks to look up the ID of a truck, given its name (I’m just doing a look-up for a single truck with a hardcoded truck name, but you can imagine doing this step in a loop while reading from a CSV of truck names, for example):

for current_truck_name in ["5151"]:
    current_truck_id = trucks[current_truck_name]
    print("The truck with name '{}' has the id '{}'".format(current_truck_name, current_truck_id))

This isn’t that different from what you’re doing in your last snippet. Where are you stuck exactly? How exactly would you use a truck’s ID to make a request to another API endpoint to "apply a tag in the API to that ID"?


EDIT: Here is how you might read from a CSV file. Assuming you have a CSV file named truck_names.csv, which looks like this (a single column, one row per truck name):

truck_name
5151
1234
9999

Then you could do (assuming the trucks dictionary already exists at this point):

from csv import DictReader

with open("truck_names.csv", newline="") as file:
    reader = DictReader(file)
    for row in reader:
        truck_name = row["truck_name"]
        truck_id = trucks[truck_name]
        print(f"Performing tag update on truck with name '{truck_name}' (ID: '{truck_id}')...")

        url = "YYY.YYY.com/comGpsGate/api/v.1/applications/46/tags/2259/users"
        headers = {
            "Authorization": "YYY"
        }

        payload = {
            "id": truck_id
        }
        
        response = requests.post(url, headers=headers, json=payload)
        response.raise_for_status()

        print(json.dumps(response.json(), indent=4))
Answered By: Paul M.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.