# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

import requests

from polyaxon_client.api.base import BaseApiHandler
from polyaxon_client.exceptions import AuthenticationError, PolyaxonHTTPError
from polyaxon_client.schemas import CredentialsConfig, UserConfig


class AuthApi(BaseApiHandler):
    """
    Auth/User specific api handler.

    Special case client because of the token is not acquired yet,
    so we do not use shared client functions.
    """
    ENDPOINT = "/users"

    def get_user(self, token=None):
        token = token or self.transport.token
        request_url = self._get_http_url()
        response = self.transport.get(request_url,
                                      headers={"Authorization": "{} {}".format(
                                          self.config.authentication_type, token)})
        try:
            user_dict = response.json()
            response.raise_for_status()
        except Exception:
            if response.status_code == 401:
                raise AuthenticationError(
                    request_url,
                    response,
                    "Invalid Token.\nSee http://docs.polyaxon.com/faqs/authentication/ for help",
                    401)
            raise AuthenticationError(
                request_url,
                response,
                "Login failed.\nSee http://docs.polyaxon.com/faqs/authentication/ for help",
                response.status_code)

        return self.prepare_results(response_json=user_dict, config=UserConfig)

    def login(self, credentials):
        credentials = self.validate_config(config=credentials, config_schema=CredentialsConfig)
        request_url = self.build_url(self._get_http_url(), 'token')
        try:
            response = self.transport.session.post(request_url, data=credentials.to_dict())
        except requests.ConnectionError:
            raise PolyaxonHTTPError(
                request_url,
                None,
                "Connection error.",
                None)
        try:
            token_dict = response.json()
            response.raise_for_status()
        except Exception:
            if response.status_code == 401:
                raise AuthenticationError(
                    request_url,
                    response,
                    "Invalid credentials.",
                    401)
            raise AuthenticationError(
                request_url,
                response,
                "Login failed.\nSee http://docs.polyaxon.com/faqs/authentication/ for help",
                response.status_code)

        return token_dict.get('token')

@@ -1566,494 +1566,64 @@
     
-def login(self, credentials):%0A        credentials = self.validate_config(config=credentials, config_schema=CredentialsConfig)%0A        request_url = self.build_url(self._get_http_url(), 'token')%0A        try:%0A            response = self.transport.session.post(request_url, data=credentials.to_dict())%0A        except requests.ConnectionError:%0A            raise PolyaxonHTTPError(%0A                request_url,%0A                None,%0A                %22Connection error.%22,%0A                None)
+@staticmethod%0A    def _get_token(request_url, response):
 %0A   
@@ -2176,32 +2176,32 @@
 e.status_code)%0A%0A
-
         return t
@@ -2223,8 +2223,985 @@
 token')%0A
+%0A    def login(self, credentials):%0A        credentials = self.validate_config(config=credentials, config_schema=CredentialsConfig)%0A        request_url = self.build_url(self._get_http_url(), 'token')%0A        try:%0A            response = self.transport.session.post(request_url, data=credentials.to_dict())%0A        except requests.ConnectionError:%0A            raise PolyaxonHTTPError(%0A                request_url,%0A                None,%0A                %22Connection error.%22,%0A                None)%0A        return self._get_token(request_url=request_url, response=response)%0A%0A    def login_using_ephemeral_token(self, url, ephemeral_token):%0A        try:%0A            response = self.transport.session.post(url, %7B'token': ephemeral_token%7D)%0A        except requests.ConnectionError:%0A            raise PolyaxonHTTPError(%0A                url,%0A                None,%0A                %22Connection error.%22,%0A                None)%0A        return self._get_token(request_url=url, response=response)%0A

Add ephemeral login