import requests
import json


# def createToken(url, parms):
def createToken(parms):
    url = "https://api-identity-infrastructure.nhncloudservice.com/v2.0/tokens"
    response = requests.post(url, json=parms)  # 응답요청

    datas = json.loads(response.text)
    # print(datas)
    if "error" in datas:
        errDict = {
            'state' : 'err',
            'code' : datas['error']['code'],
            'errTitle' : datas['error']['title'],
            'errMsg' : datas['error']['message']
        }
        return errDict 
    else:
        getDict = {
            'state' : 'steady',
            'tokenValue' : datas['access']['token']['id']
        }
        # WhatToken = getDict['tokenValue']
        return getDict


if __name__ == "__main__":
    # 예제 내용
    url = "https://api-identity-infrastructure.nhncloudservice.com/v2.0/tokens"
    parms = {
        "auth": {
            "tenantId": "04a2c5b7de7e4d66b970ad950081a7c3",  # kr2
            "passwordCredentials": {
                # "username": "sdjo@injeinc.co.kr",
                # "password": "ijinc123!"
            }
        }
    }

    print(createToken(url, parms))