make central function to encode the api_key, and to process the response for validity.
added __main__ section for easier testing.
Reformatted the code