login_func.py 5.2 KB
# @Time     :2019/11/26 11:23
# @Author   :dengyuting
# @File     :login_func.py
import json
import os


def teardown_hook_get_access_id(response):
    os.remove('config/id.csv')
    if response.status_code == 200:
        jsondata = json.loads(response.text)
        id =jsondata['data']['list']
    try:
        for i in range(len(id)):
            a1 = str(id[i]['id'])
            with open('config/id.csv','a+') as f:
                f.writelines(a1 + '\n')
            print('写入成功,id:{}'.format(a1))
    except Exception as e:
        print('写入失败', e)
    return a1
#
# def get_id():
#     try:
#         with open('config/id.csv','r+') as f:
#             id1 = f.read()
#             print('读取id成功:{}'.format(id))
#             f.close()
#     except Exception as e:
#         print('读取失败', e)
#     # id = str(id1)
#     return id
# # print(get_id())
def teardown_hook_get_accesstoken(response):
    if response.status_code == 200:
        jsondata = json.loads(response.text)
        access_token =jsondata['data']['accessToken']
    try:
        # 保存token到文件
        with open('config/accessToken.csv','w+') as f:
            f.write(access_token)
            print('写入成功,access_token:{}'.format(access_token))
            f.close()
    except Exception as e:
        print('写入失败', e)
    return access_token


def teardown_hook_get_m_accesstoken(response):
    print(response.status_code)
    if response.status_code == 200:
        jsondata = json.loads(response.text)
        access_token = jsondata['data']['access']['accessToken']
    try:
        # 保存token到文件
        with open('config/m_accessToken.csv','w+') as f:
            f.write(access_token)
            print('写入成功,access_token:{}'.format(access_token))
            f.close()
    except Exception as e:
        print('写入失败', e)
    return access_token


def teardown_hook_get_refreshtoken(response):
    if response.status_code == 200:
        jsondata = json.loads(response.text)
        refresh_token =jsondata['data']['refreshToken']
    try:
        # 保存token到文件
        with open('config/refreshToken.csv','w+') as f:
            f.write(refresh_token)
            print('写入成功,refresh_token:{}'.format(refresh_token))
            f.close()
    except Exception as e:
        print('写入失败', e)
    return refresh_token


def teardown_hook_get_m_refreshtoken(response):
    if response.status_code == 200:
        jsondata = json.loads(response.text)
        m_refresh_token =jsondata['data']['access']['refresh_token']
    try:
        # 保存token到文件
        with open('config/m_refresh_token.csv','w+') as f:
            f.write(m_refresh_token)
            print('写入成功,m_refresh_token:{}'.format(m_refresh_token))
            f.close()
    except Exception as e:
        print('写入失败', e)
    return m_refresh_token


def teardown_hook_get_authcode(response):
    if response.status_code == 200:
        jsondata = json.loads(response.text)
        authCode = jsondata['data']['authCode']
    try:
        # 保存token到文件
        with open('config/authCode.csv','w+') as f:
            f.write(authCode)
            print('写入成功,authCode:{}'.format(authCode))
            f.close()
    except Exception as e:
        print('写入失败', e)
    return authCode


def get_accesstoken():
    try:
        with open('config/accessToken.csv','r') as f:
            accseetoken_value = f.read()
            print('读取accseetoken_value成功:{}'.format(accseetoken_value))
            f.close()
    except Exception as e:
        print('读取失败', e)
    accseetoken_value = str(accseetoken_value)
    return accseetoken_value


def get_m_accesstoken():
    try:
        with open('config/m_accessToken.csv','r') as f:
            accseetoken_value = f.read()
            print('读取accseetoken_value成功:{}'.format(accseetoken_value))
            f.close()
    except Exception as e:
        print('读取失败', e)
    accseetoken_value = str(accseetoken_value)
    authorization = 'bearer ' + accseetoken_value
    return authorization


def get_m_refreshtoken():
    try:
        with open('config/m_refresh_token.csv','r') as f:
            m_refreshtoken_value = f.read()
            print('读取m_refreshtoken_value成功:{}'.format(m_refreshtoken_value))
            f.close()
    except Exception as e:
        print('读取失败', e)
    m_refreshtoken_value = str(m_refreshtoken_value)
    authorization = 'bear ' + m_refreshtoken_value
    return authorization


def get_refreshtoken():
    try:
        with open('config/refreshToken.csv','r') as f:
            refreshtoken_value = f.read()
            print('读取refreshtoken_value成功:{}'.format(refreshtoken_value))
            f.close()
    except Exception as e:
        print('读取失败', e)
    refreshtoken_value = str(refreshtoken_value)
    return refreshtoken_value


def get_authcode():
    try:
        with open('config/authCode.csv','r') as f:
            authcode_value = f.read()
            print('读取authcode_value成功:{}'.format(authcode_value))
            f.close()
    except Exception as e:
        print('读取失败', e)
    authcode_value = str(authcode_value)
    return authcode_value