'Efficient way to produce snapshots of a number of api endpoints in python using 'requests' or another library

I have a need to create snapshots(json response of an API at a given moment in time) for various endpoints and various REST methods(GET, POST) and save it as a json file, so that I can compare the snapshot content(json file) to the response to some live response from an API. To do this I need to create snapshots(json files) of responses to potentially a relatively large number of endpoints. What's the best way to do this? Preferably using 'requests' module. For a single endpoint one might be able to do this( pseudocode)with something like:

def create_snapshot(request_method, url, params,body=None, filepath):
   if body:
            response = requests.requests(request_method,url,params=params,json=body)
            with open(filepath, 'r+', encoding='utf-8') as f:
               json.dump(response.json(), f, indent=4)
   else:
           # do same but without the body

and do this repeatedly for various endpoints. Is there a better way to do this? N.B. I'm aware that the above may not be exact legal python(not tested it), it's there to convey my current train of thought. Thank you for your help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source