New-ish to python and I'm stumped on this. I literally cannot seem to find a clear and correct answer anywhere online. I honestly feel stupid because this is a pretty basic python skill.
My issue: I've been tasked with writing a python test suite of asyncronous api requests chained together. I need to fire the requests, save values from the response json, then pass those values into subsequent requests.
Below is a list of what I WANT the test to do, and what I would expect it to do based on how I've written the test file:
- Send
GETto/usersendpoint - Save first
customerIdfrom response payload ascustomer_id - Send
POSTto/customer/{customer_id}/addressesendpoint to create new customer address - Save
addressIdfrom response payload asaddress_id - Send
PUTto/orders/{customer_id}endpoint to update order with theaddress_idfrom step #3.
Example Code: (This is the same code I have on my machine, but I've sanitized the file with fake info.
import requests
import json
def first_request_get(self):
url = 'https://api.example.net/users'
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer asdfasdfasdfasdfasdfasdfasdf'
}
payload = {}
resp = requests.get(url, headers=headers, data=json.dumps(payload))
json_str = json.dumps(payload)
resp_body = resp.json()
customer_id = resp_body['customer'][0]['id']
assert resp_body['customer'][0]['id'] is not None
assert resp.status_code == 200
return customer_id
def second_request_post(self):
customer_id = self.first_request_get()
url = 'https://api.example.net/customers/{}/addresses'.format(customer_id)
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer asdfasdfasdfasdfasdfasdfasdf'
}
payload = {"address1": "285 Beandip Ln",
"city": "Lincoln",
"province": "Nebraska",
"zip": "68510",
}
resp = requests.post(url, headers=headers, data=json.dumps(payload))
resp_body = resp.json()
address_id = resp_body['address']['id']
print('address_id == ')
print(address_id)
assert resp.status_code == 200
assert resp_body['address']['id'] is not None
return address_id
def third_request_put(self):
address_id = self.second_request_post()
customer_id = self.first_request_get()
url = 'https://api.example.net/orders/{}'.format(customer_id)
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer asdfasdfasdfasdfasdfasdfasdf'
}
payload = {"address_id": address_id,
"next_charge_scheduled_at": "2022-01-31T00:00:00",
"price": 6.85,
"quantity": 2,
}
resp = requests.put(url, headers=headers, data=json.dumps(payload))
resp_body = resp.json()
subscription_id = resp_body['order']['id']
print('order_id == ')
print(subscription_id)
assert resp_body['order']['id'] is not None
assert resp.status_code == 200
return subscription_id
The result of executing this test file ends up looping through the file in a weird order rather than doing what I would expect it to do. Why do I have to call an earlier function just to access the variable I've already extracted from the json and assigned to a variable in an earlier function?