<reponame>abhitopia/TypeGenieApiClient
from datetime import datetime

from typegenie import authenticator, User, Event, EventType, Author

# Authentication
# USER_ACCESS_TOKEN = '<KEY>'
USER_ACCESS_TOKEN = '<KEY>'


DEPLOYMENT_ACCESS_TOKEN = None
ACCOUNT_USERNAME = None
ACCOUNT_PASSWORD = None

if USER_ACCESS_TOKEN is not None:
    authenticator.authenticate_user(token=USER_ACCESS_TOKEN)
elif DEPLOYMENT_ACCESS_TOKEN is not None:
    authenticator.authenticate_deployment(token=DEPLOYMENT_ACCESS_TOKEN)
    # Then you can fallback to higher level API automatically by running following command
    authenticator.enable_auto_fallback()
elif ACCOUNT_USERNAME is not None and ACCOUNT_PASSWORD is not None:
    authenticator.authenticate_account(username=ACCOUNT_USERNAME, password=<PASSWORD>)
    # Then you can fallback to higher level API automatically by running following command
    authenticator.enable_auto_fallback()
else:
    raise RuntimeError('You must either have a user/deployment access token or account credentials')

# Ignore this. This is to run the sandbox environment
authenticator.enable_sandbox()

# Furthermore, since the access token expires automatically after a while, you can enable token auto renew using
authenticator.enable_auto_renew()

# Assuming that the user with id `my-new-user` exists.
user_id = 'my-new-user'
deployment_id = 'my-new-deployment'

user = User.get(user_id=user_id, deployment_id=deployment_id)
print('User: ', user)

# Create a new session
session_id = user.create_session()
print('Session: ', session_id)

# Get predictions
events = [Event(author_id='lost-soul-visitor',
                value='What is love?',
                event=EventType.MESSAGE,
                timestamp=datetime.utcnow(),
                author=Author.USER),
          Event(author_id='my-new-user',  # Note this is an agent already added as user to deployment
                value="Oh baby, don't hurt me",
                event=EventType.MESSAGE,
                timestamp=datetime.utcnow(),  # This should be time at which the event happened
                author=Author.AGENT)
          ]

print(user.get_completions(session_id=session_id, events=events, query="Don"))
