LaVOZs

The World’s Largest Online Community for Developers

'; python - Reading a single user entry from REST GET if one can only GET a full user list - LavOzs.Com

I use software that only lets me fetch information on all users, while my project requires me to get only one specific user. I only need his ID and I'm going to find it out by his e-mail (because it's unique).

The program creates an user (via POST) and stores his data (like email) in variables, then reads a provided list of devices that will be assigned to said user. To do this, the program has to:

  1. Fetch all users (why software author didn't allow to fetch a single user is beyond me)
  2. Filter users so it finds my newly created user <- here's my issue
  3. Fetch all devices...
  4. Filter devices...
  5. Finally create a permission relationship between found user and device IDs

This is what I came up with so far:

inputText = self.imeiInput.GetValue().splitlines() # reads input and creates a list
url = "https://example.com/api/users/"
userList = requests.get(url, auth=(login, password)).content
foundUser = re.findall(givenMail, str(userList)) # givenMail provided by function argument
print(userList)
print(foundUser) # prints only searched mail
for imei in inputText:
    self.TrackerAssign(imei, foundUser) # do your stuff

But it only confirms that there indeed is my email in the userbase, there's no other info (like ID I'm interested in). Here's a sample userList output (except that originally it's all in one line):

b'[{
    "id":1,
    "attributes": {
    ...
    },
    "name":"username",
    "login":"",
    "email":"test@example.com",
    "phone":"999999999",...
},
{
    "id":2,
    "attributes": {
    ...
    },
    "name":"username2",
    "login":"",
    "email":"test2@exmaple.com",
    "phone":"888888888",...
},
...]'

Then there's also a question how to only read the desired ID. Sorry if this is confusing, I barely know what I'm doing myself.

From your example it seems like you get a JSON response from the endpoint. This is great, because this JSON can be parsed to a list/dictionary!

Your approach can look something like this. In broad terms, this is a possible strategy:

  1. Get all users from the users endpoint.
  2. Parse the response JSON to a list of dictionaries.
  3. Loop over all users, breaking the loop when you find your user.
  4. Do something with the ID of the user that you found in your search.
response = requests.get(url, auth=(login, password))  # receive a Response instance
user_list = response.json()  # parse the body to a dictionary

for user in user_list:
    # Compare the email of this user with the target, using get to catch users with no email specified.
    if user.get("email") == given_mail:
        desired_user = user
        break  # if we find the correct user, we exit the loop
else:
    # If we never find the desired user, we raise an exception.
    raise ValueError("There is no user with email %s", given_email")

print(f"The ID of the user I am looking for is {desired_user["id"]}.")
Related
Python read a single character from the user
How can I get a list of locally installed Python modules?
Jira Rest Api for getting full user list
How can I get list of values from dict?
Get list from pandas DataFrame column headers
get list from pandas dataframe column
How can I get the named parameters from a URL using Flask?
Return full entries or only id and title per entry from REST /entries endpoint?