3

I want to start a python interpreter after performing a few operations to make it more easy for the user. So far, I have a skeleton of what I want.

class ApiManager(object):
  def __init__(self, username, password, version='v1'):
    self.base_url = 'http://localhost:3000/api/' + version + '/{endpoint}'

    login = requests.post(self.base_url.format(endpoint='login'), {
      'user': username, 'password': password
    }).json()['data']

    self.headers = {
      'X-Auth-Token': login['authToken'],
      'X-User-Id': login['userId']
    }

  def get(self, endpoint):
    " example method "
    return requests.get(self.base_url.format(endpoint))

if __name__ == '__main__':
  Api = ApiManager('[email protected]', 'password')
  code.interact(banner="use Api.<Crud Command> to do a method")

However, what I am wondering is, how can I import this local into my code.interact?

I've tried local=Api but that does not work, saying TypeError: exec() arg 2 must be a dict, not ApiInterpreter. I've tried using Api.__dict__() but that does not work either. I was also thinking it might be possible to subclass code.InteractiveConsole

1 Answer 1

1

Try:

code.interact(banner="use Api.<Crud Command> to do a method", local={"Api": Api})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.