Alvaro
Alvaro

Reputation: 59

Variable in Python being deleted after object being deleted?

I’m new in Python. Maybe someone more experienced than me can help me.

I’m trying to delete a user from Okta using an API call. In order to delete a user from Okta you need to do the same DELETE request twice. The first one disables the account and the second one removes it.

I have two functions: one for retrieving the user ID using the email address and then the function to delete the user using the ID.

If I go to Postman and run the API call twice it just works. The first one disables the account and the second deletes it.

I’m trying to accomplish that in Python by doing this:

okta_user = okta.user_search(login)
okta_id = okta_user[0]["id"]
for _ in range(2):
    okta.user_delete(okta_id)

But I get this response:

DEBUG: https://fakeoktaaddress.com:443 "DELETE //api/v1/users/00u7ngjk6u2fkJR7o1d7 HTTP/1.1" 204 0
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./offboard.py", line 51, in <module>
    okta.user_delete(okta_id)
  File "/calls/calls.py", line 151, in user_delete
    logging.info(response.json())
                 ^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

As you can see the first call runs perfectly and returns a 204 but the second time seems to be failing.

My theory is that somehow in the second run, python tries to get the value for okta_id again by running one or both of these functions again:

okta_user = okta.user_search(login)
okta_id = okta_user[0]["id"]

If this is indeed happening, then okta_user = okta.user_search(login) will fail, since you can’t search by email a deactivated user.

Could it be that the information that I’ve retrieved and stored in a variable is deleted after the object from the call that created it in the first place is deleted? What am I doing wrong and how could I work around this?

Thanks!

Tried a tuple instead of a regular variable. Also tried a global variable instead of a reusable.

Upvotes: 1

Views: 297

Answers (0)

Related Questions