# Copyright 2016 Yuji Azama
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import json

from soracomclient.client import ClientBase


class Auth(ClientBase):

    path = "/auth"

    def __init__(self, email, password, api_key=None, operator_id=None,
                 token=None, timeout=None):
        super(Auth, self).__init__(api_key, token)
        self._email = email
        self._password = password
        if api_key is None:
            self._api_key, self._operator_id, self._token = self.auth(email,
                                                                      password,
                                                                      timeout)
        else:
            self._api_key = api_key
            self._operator_id = operator_id
            self._token = token

    @property
    def email(self):
        return self._email

    @property
    def password(self):
        return self._password

    @property
    def api_key(self):
        return self._api_key

    @property
    def operator_id(self):
        return self._operator_id

    @property
    def token(self):
        return self._token

    def auth(self, email, password, timeout):
        body = {"email": email,
                "password": password}
        if timeout:
            body["tokenTimeoutSeconds"] = timeout
        response, body = self.post(self.path, body=body)
        #body = json.loads(body)
        return body['apiKey'], body['operatorId'], body['token']

@@ -783,20 +783,21 @@
 timeout=
-None
+86400
 ):%0A     
@@ -860,21 +860,31 @@
 lf._
+auth = %7B'
 email
- =
+':
  email
+,
 %0A   
@@ -888,30 +888,39 @@
         
-self._
+              '
 password
  = passw
@@ -911,18 +911,18 @@
 password
- =
+':
  passwor
@@ -922,16 +922,59 @@
 password
+,%0A                      'timeout': timeout%7D
 %0A       
@@ -1017,223 +1017,34 @@
 f._a
-pi_key, self._operator_id, self._token = self.auth(email,%0A                                                                      password,%0A                                                                      timeo
+uth = self.auth(self._a
 ut
+h
 )%0A  
@@ -1074,22 +1074,29 @@
  self._a
-pi_key
+uth%5B'apiKey'%5D
  = api_k
@@ -1116,24 +1116,30 @@
   self._
+auth%5B'
 operator
 _id = op
@@ -1130,19 +1130,20 @@
 operator
-_id
+Id'%5D
  = opera
@@ -1167,21 +1167,29 @@
   self._
+auth%5B'
 token
+'%5D
  = token
@@ -1246,21 +1246,29 @@
 n self._
+auth%5B'
 email
+'%5D
 %0A%0A    @p
@@ -1320,24 +1320,30 @@
 n self._
+auth%5B'
 password
 %0A%0A    @p
@@ -1334,16 +1334,18 @@
 password
+'%5D
 %0A%0A    @p
@@ -1397,22 +1397,29 @@
  self._a
-pi_key
+uth%5B'apiKey'%5D
 %0A%0A    @p
@@ -1474,24 +1474,30 @@
 n self._
+auth%5B'
 operator
 _id%0A%0A   
@@ -1488,19 +1488,20 @@
 operator
-_id
+Id'%5D
 %0A%0A    @p
@@ -1550,21 +1550,29 @@
 n self._
+auth%5B'
 token
+'%5D
 %0A%0A    de
@@ -1588,32 +1588,12 @@
 lf, 
-email, password, timeo
+a
 ut
+h
 ):%0A 
@@ -1611,22 +1611,30 @@
  = %7B
-%22
+'
 email
-%22
+'
 : 
+auth%5B'
 email
+'%5D
 ,%0A  
@@ -1651,17 +1651,17 @@
     
-%22
+'
 password
 %22: p
@@ -1660,19 +1660,25 @@
 word
-%22
+'
 : 
+auth%5B'
 password
 %7D%0A  
@@ -1677,30 +1677,17 @@
 word
-%7D
+'%5D,
 %0A     
-   if timeout:%0A
     
@@ -1697,15 +1697,9 @@
     
- body%5B%22
+'
 toke
@@ -1717,28 +1717,35 @@
 onds
-%22%5D = 
+': auth%5B'
 timeout
+'%5D%7D
 %0A       
  res
@@ -1740,16 +1740,24 @@
 %0A       
+ status,
  respons
@@ -1757,22 +1757,16 @@
 response
-, body
  = self.
@@ -1804,94 +1804,46 @@
     
-#body = json.loads(body)%0A        return body%5B'apiKey'%5D, body%5B'operatorId'%5D, body%5B'token'%5D
+auth.update(response)%0A        return auth
 %0A

Refactor the code for authentication.