I've created a basic Python CLI script, which takes a Github username, and returns some basic information associated with said username. I use the requests for HTTP requests (I'm new to this area) and docopt for command-line handling.
#!/usr/bin/env python3
"""Github User Info
Get basic information on a Github user
Usage:
github_user_info.py USERNAME
github_user_info.py (-h | --help)
github_user_info.py (-v | --version)
Arguments:
USERNAME Github username
Options:
-h --help Show this screen.
-v --version Show version.
"""
from docopt import docopt
import requests
import sys
def main():
if len(sys.argv) == 1:
sys.argv.append('-h') # Show help screen if no arguments are passed
cmd_arguments = docopt(__doc__, version='Github User Info v1.0')
username = cmd_arguments["USERNAME"]
if len(username.split()) != 1:
print('Github usernames must be one word in length! Exiting!')
return
response = requests.get(f'https://api.github.com/users/{username}')
if response.status_code != requests.codes.ok:
if response.status_code == 404: # No such user was found
print(f'No Github user was found with username {username}!')
else:
print(f'An unexpected error occured! Exiting with error code: '
f'{response.status_code}')
return
responses_json = response.json()
print(f'The following information was found on user: {username}',
end='\n\n')
print(f'Name: {responses_json["name"]}')
print(f'Bio: {responses_json["bio"]}')
print(f'Followers: {responses_json["followers"]}')
print(f'Following: {responses_json["following"]}')
if __name__ == '__main__':
main()
I'd mainly like to know if this is the correct way to handle HTTP requests (in terms of getting them, error handling, etc.). I'd also like to make sure that I follow good Python conventions.