import pili.api as api
import base64
import hmac
import hashlib
import json


class RoomClient(object):
    """docstring for RoomClient"""
    def __init__(self, credentials):
        self.__credentials__ = credentials
        self.__auth__ = credentials.__auth__

    def createRoom(self, ownerId, roomName=None, version='v1'):
        res = api.create_room(self.__auth__, ownerId=ownerId, roomName=roomName, version=version)
        return res

    def getRoom(self, roomName, version='v1'):
        res = api.get_room(self.__auth__, roomName=roomName, version=version)
        return res

    def deleteRoom(self, roomName, version='v1'):
        res = api.delete_room(self.__auth__, roomName=roomName, version=version)
        return res

    def roomToken(self, roomName, userId, perm, expireAt, version=None):
        if version == 'v2':
            params = {"version": "2.0", "room_name": roomName,
                      "user_id": userId, "perm": perm,
                      "expire_at": str(expireAt)}
        else:
            params = {"room_name": roomName,
                      "user_id": userId, "perm": perm,
                      "expire_at": str(expireAt)}

        roomAccessString = json.dumps(params)
        encodedRoomAccess = base64.urlsafe_b64encode(roomAccessString)
        hashed = hmac.new(self.__auth__.secret_key, encodedRoomAccess, hashlib.sha1)
        encodedSign = base64.urlsafe_b64encode(hashed.digest())
        return self.__auth__.access_key+":"+encodedSign+":"+encodedRoomAccess

@@ -821,68 +821,18 @@
     
-if version == 'v2':%0A            params = %7B%22version%22: %222.0%22, 
+params = %7B
 %22roo
@@ -940,28 +940,24 @@
 re_at%22: 
-str(
 expireAt
 )%7D%0A     
@@ -948,17 +948,16 @@
 expireAt
-)
 %7D%0A      
@@ -962,12 +962,26 @@
     
-else
+if version == 'v2'
 :%0A  
@@ -1000,140 +1000,38 @@
 rams
- = %7B%22room_name%22: roomName,%0A                      %22user_id%22: userId, %22perm%22: perm,%0A                      %22expire_at%22: str(expireAt)%7D%0A
+%5B%22version%22%5D=%222.0%22%0A            
 %0A   
@@ -1071,16 +1071,37 @@
 s(params
+,separators=(',',':')
 )%0A      

fix roomtoken error