Further on my CLI tool to replace Postman GET requests
I had another great evening coding on this project.
Pulled out all the security info and the main domain into a separate module, allowing me to create a GitHub Repo for the core of the code without security concerns. That was the simple piece.
Now that I had the replies printing to console I wanted them in files. Files that were easy to read. The replies were in XML this meant the first task was to convert them into JSON and make them 'pretty'.
Library: xmltodict
This was the tool pf choice for the conversion. Nothing too fancy.
obj = xmltodict.parse(object)
Next, it was to get the object into a readable JSON format making use of the library json. The params sort out the readable part.
obj2 = json.dumps(obj, indent=4, sort_keys=True)
The last part was to get this object into a file but I wanted to make sure that this file wasn't overwritten easily and if I looked at the filename know when it was written. I just wanted the file written into path but anyone else using this code could easily point this to somewhere else.
Library datetime
I was easily able to create unique file names.
from datetime import datetime
object_name = "Extraction-" + datetime.today().strftime('%Y-%m-%d') + '_' + \
datetime.now().strftime("%H_%M_%S")+'.json'
Then use this object_name to write the file
with open(object_name, 'w') as f:
f.write(obj2)
Now, I did remember especially when including times in filenames.
You cannot use the colon ':' as the code with error out.
The code is running nicely. Now the only thing I have to do on my next visit to the IDE is to get the code to return to the main menu rather than quietly close. Ensuring the user can bash through plenty of examples.
Future feature: Do away with the user input if they want to run the same GET request with a list of items.