Last active
August 29, 2015 14:24
-
-
Save AlexFrazer/8d4e04e50ba038347e11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import argparse | |
class ApiInterpreter(object): | |
def __init__(self, version="v1"): | |
self.version = version | |
def get(self, endpoint, data={}): | |
return requests.get(self.base_url.format(version=self.version, endpoint=endpoint), data=data, headers=self.headers).json() | |
def post(self, endpoint, data={}): | |
return requests.post(self.base_url.format(version=self.version, endpoint=endpoint), data=data, headers=self.headers).json() | |
def put(self, endpoint, data={}): | |
return requests.put(self.base_url.format(version=self.version, endpoint=endpoint), data=data, headers=self.headers).json() | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description="utility for testing api calls") | |
parser.add_argument('--user', dest='user', help='user to log in as') | |
parser.add_argument('--password', dest='password', help='password for user') | |
args = parser.parse_args() | |
api = ApiInterpreter(args.user, args.password) | |
try: | |
import IPython | |
IPython.embed( | |
banner1='Authenticated as {}\nApi Version: {}'.format(args.user, api.version), | |
banner2='use the api variable to access the api', | |
local_ns={ 'api': api } | |
) | |
except ImportError: | |
import code | |
code.interact(banner='authenticated', local={ 'api': api }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment