*** Settings ***
Library           RequestsLibrary

*** Variables ***
${URL}            http://bitagent.sit.ninetechone.com
${kfz}            api_tester
${zh}             api_test_admin

*** Keywords ***
创建会话
    Create Session    URI    ${URL}
    Comment    生成token信息

参数生成
    [Arguments]    ${json_str}
    Comment    ${str}    Set Variable    {"option":"3","pageSize":"5","pageIndex":"1"}
    ${params}    Evaluate    json.loads('${json_str}')    json
    [Return]    ${params}

GET请求
    [Arguments]    ${PATH}    ${PARAMS}    ${token}    ${status_code}=200
    &{header}    Create Dictionary    Authorization=${token}
    ${resp}    Get On Session    URI    ${PATH}    params=${PARAMS}    headers=${header}    expected_status=${status_code}
    Should Be Equal As Integers    ${resp.status_code}    ${status_code}
    log    ${resp}
    ${content}    Set Variable    ${resp.json()}
    [Return]    ${content}

POST请求
    [Arguments]    ${PATH}    ${PARAMS}    ${token}    ${status_code}=200
    &{header}    Create Dictionary    Authorization=${token}    Content-Type=application/json
    ${resp}    Post On Session    URI    ${PATH}    json=${PARAMS}    headers=${header}    expected_status=${status_code}
    Should Be Equal As Integers    ${resp.status_code}    ${status_code}
    ${content}    Set Variable    ${resp.json()}
    [Return]    ${content}

POST请求_params
    [Arguments]    ${PATH}    ${PARAMS}    ${token}    ${status_code}=200
    &{header}    Create Dictionary    Authorization=${token}    Content-Type=application/json
    ${resp}    Post On Session    URI    ${PATH}    params=${PARAMS}    headers=${header}    expected_status=${status_code}
    Should Be Equal As Integers    ${resp.status_code}    ${status_code}
    ${content}    Set Variable    ${resp.json()}
    [Return]    ${content}

PUT请求_json
    [Arguments]    ${PATH}    ${PARAMS}    ${token}    ${status_code}=200
    &{header}    Create Dictionary    Authorization=${token}    Content-Type=application/json
    ${resp}    Put On Session    URI    ${PATH}    json=${PARAMS}    headers=${header}    expected_status=${status_code}
    Should Be Equal As Integers    ${resp.status_code}    ${status_code}
    ${content}    Set Variable    ${resp.json()}
    [Return]    ${content}

PUT请求_params
    [Arguments]    ${PATH}    ${PARAMS}    ${token}    ${status_code}=200
    &{header}    Create Dictionary    Authorization=${token}    Content-Type=application/json
    ${resp}    Put On Session    URI    ${PATH}    params=${PARAMS}    headers=${header}    expected_status=${status_code}
    Should Be Equal As Integers    ${resp.status_code}    ${status_code}
    ${content}    Set Variable    ${resp.json()}
    [Return]    ${content}

DEL请求
    [Arguments]    ${PATH}    ${PARAMS}    ${token}    ${status_code}=200
    &{header}    Create Dictionary    Authorization=${token}    Content-Type=application/json
    ${resp}    Delete On Session    URI    ${PATH}    json=${PARAMS}    headers=${header}    expected_status=${status_code}
    Should Be Equal As Integers    ${resp.status_code}    ${status_code}
    ${content}    Set Variable    ${resp.json()}
    [Return]    ${content}

生成token信息
    Create Session    URL    https://api.sit.ninetechone.com
    ${params}    参数生成    {"username":"${kfz}","password":"Ninetech@123","redirect_uri":"http://studioweb.sit.ninetechone.com/#/designer","response_type":"token","scope":"all","state":"xyz","client_id":"4"}
    ${resp}    post On Session    URL    /api/accounts/login    params=${params}
    ${content}    Set Variable    ${resp.json()}
    Set Global Variable    ${token}    Bearer ${content["access_token"]}
    ${params1}    参数生成    {"username":"${zh}","password":"Ninetech@123","redirect_uri":"http://studioweb.sit.ninetechone.com/#/designer","response_type":"token","scope":"all","state":"xyz","client_id":"4"}
    ${resp1}    post On Session    URL    /api/accounts/login    params=${params1}
    ${content1}    Set Variable    ${resp1.json()}
    Set Global Variable    ${zh_token}    Bearer ${content1["access_token"]}

机器人GET请求
    [Arguments]    ${PATH}    ${PARAMS}    ${robot_token}    ${status_code}=200
    &{header}    Create Dictionary    Authorization=${robot_token}
    ${resp}    Get On Session    URI    ${PATH}    params=${PARAMS}    headers=${header}    expected_status=${status_code}
    Should Be Equal As Integers    ${resp.status_code}    ${status_code}
    log    ${resp}
    ${content}    Set Variable    ${resp.json()}
    [Return]    ${content}
