Recursively Iterate an API with python until all value are found

39 views Asked by At

I am using a SAP Business Object Rest API to find all sub group of a group.

I want to find a group, if the group has sub groups I want to find them, if a sub group has a sub sub groups I want to get them and so on until I find all groups that have a sub group. (I need all nested group of a group so long there is a group that has a sub group)

example:

mygroup
---group_1
---group_2
---group_3
------group_3_a
------group_3_b
---------group_3_a_a
---group_4

what I want at the end are "mygroup, group_3, group_3_b"

Here the output of the API query from postman:

* https://{{STAGE}}/biprws/v1/usergroups/19791/usergroups
{
  "entries": [
    {
      "__metadata": {
        "uri": "https://{{STAGE}}/biprws/v1/usergroups/19791/usergroups"
      },
      "cuid": "AVULJ1f4xTJJkSt.N33vH0w",
      "keywords": "",
      "created": "",
      "name": "SUPERVISOR",
      "description": "",
      "id": "7467291",
      "parentid": "20"
    },
    {
      "__metadata": {
        "uri": "https://{{STAGE}}/biprws/v1/usergroups/19791/usergroups"
      },
      "cuid": "Q1hAIaoABJhzAYIAkgAAW9cLRkE1NzY1NEM",
      "keywords": "",
      "created": "",
      "name": "Support",
      "description": "",
      "id": "891887",
      "parentid": "20"
    }
  ]
}

Now I want to iterate through the two id: 7467291 / 891887 with the same query but different id:

  • https://{{STAGE}}/biprws/v1/usergroups/7467291/usergroups

Now if the query has no entries go on ...

  • https://{{STAGE}}/biprws/v1/usergroups/891887/usergroups

If the query has entries than loop over the group id and find if they have other sub groups.

Here what I tried to do:

# my group 
my_group = '19786'
# Show related group of my choosen group
the_page_url = f"{base_url}/v1/usergroups/{my_group}/usergroups"
payload = {}
headers = {
    'content-type': "application/json",
    'accept': "application/json",
    'x-sap-logontoken': sap_logon_token
    }

response = requests.request("GET", the_page_url, json=payload, headers=headers, verify=False)
j_response = response.json()

print('ZZZ--- ', j_response['entries'])

# Now I loop through the subgroup of my choosen group recursively. So long that j_response['entries'] is not empty the loop go on.
while j_response['entries']:
    try:
        for entry in j_response['entries']:
            si_name = entry['name']
            si_id = entry['id']
            si_parent_id = entry['parentid']
            the_page_url = f"{base_url}/v1/usergroups/{si_id}/usergroups"
            response = requests.request("GET", the_page_url, json=payload, headers=headers, verify=False)
            j_response = response.json()
            print('XXX---',si_name, "\n", j_response['entries'])
            # print(si_id, si_name, si_parent_id)
    except Exception as e:
        print(f"There was an error! Exiting Loop: {e}")
        break


The problem is that in this way I am able to iterate only one level down but I want to get all group that contain a subgroup recursively.

Could you please help me to solve the problem?

0

There are 0 answers