REST 接口调用及机器人调用接口

适用版本

适用于RPA9以及以下版本,RPA10及以上版本==> 点击跳转

1. 接口 API

调用方式及字段,请参考论坛手册

http://support.i-search.com.cn:8088/showdoc/web/#/45?page_id=670

2. Post 接口数据组成

URL 源串 + sign+token

2.1 URL 源串构造

下面为源串示例:

action=get-process&param={“proc_code”:""}&timestamp=1361431471

下面分割源串来讲解:

REST 接口调用及机器人调用接口

第一部分:

action= 后的 get-process 为接口类型标志,这个为“获取流程接口”,其它的请参考“接口 API”对应修改。

第二部分:

这部分为 API 接口的请求参数,param= 后按字典方式的写法,并按 KEY 升序依次排列。

第三部分:

时间字段,timestamp= 加上当前时间戳。

2.2 sign

sign 为把 URL 源串经过加密处理后的数据,具体处理步骤如下:

=> 获得源串
=> URL 编码转换(encodeURIComponent)
=> HMAC-SHA1-256 加密算法 (加密密码为 isearch)
=> 进行 base64 编码
=> 将结果去空格处理(\r\n)

2.3 token

在 iS-RPA 管理平台使用Admin用户登录后的 Token 设置页面,点击“生成”,生成 token,多次点击会重新生成,如重新生成,post 接口数据也需要相应修改。

REST 接口调用及机器人调用接口

2.4 接口 URL 示例

下面的 url 中http://192.168.202.150:10080/rapi/rcall.action 为请求地址:
http://192.168.202.150:10080/rapi/rcall.action?action=get-process&param={“proc_code”:""}&timestamp=1361431471&sign=MDJkMWEyZTY2MWZhY2ZmNWE3OTQwNmQ0ZWMwYzNlZDE1YWNjZjY3MGI0MjM1NjZmMmMzYjIyZDY5Y2Y5OGYxYg==&token=2ea7c02f99fb42749629f4414c04c9fa

3. python 调用代码

如果想用机器人来调 rest 接口,可参考以下代码,其它语言逻辑一样:

import json
import time
import requests
from urllib import parse
import hmac
import base64
from hashlib import sha256

def rpa_rest9(host,rest_type,data_json,add_pr,token,port=10080,try_times=2):
    '''
    data_json:字典型,发送的报文数据json格式
    host:地址,str型,示例:'http://192.168.202.150'
    rest_type:接口类型,str型,
    port:http端口,int型
    token:服务平台token,
    try_times:重试次数,int型
    add_pr:URL附加地址,str型, 示例:'/rapi/rcall.action'
    返回值:get_field_json:字典型,
     '''
    count = 0
    #获得sign
    #源串:sign_yc
    timestamp = str(int(time.time()))
    #处理json数据不为str类型的情况
    for key in data_json:
        data_json[key]=str(data_json[key])
    #json排序
    data_json=json.dumps(data_json,sort_keys=True)
    sign_yc='action=' + rest_type + '&param=' + str(data_json) + '&timestamp=' + timestamp
    try:
        #URL编码
        url_bm = parse.quote(sign_yc)
        #sha256加密密码
        byte_key = bytes("isearch", encoding="utf-8")
        byte_url_bm = bytes(url_bm, encoding="utf-8")
        hn256 = hmac.new(byte_key, byte_url_bm, digestmod=sha256)
        hh256 = hn256.hexdigest()
        #Base64编码
        bb64 = base64.b64encode(bytes(hh256, encoding="utf-8"))
        #获取sign
        sign = str(bb64, "utf-8")
        #替换\r\n
        sign=sign.replace('\r\n', '')
    except Exception as e:
        raise e
        print(e)
    print('URL源串:',sign_yc)
    print('sign值:',sign)
    #开始尝试发送post
    print('开始尝试发送post')
    get_field_json={'code': 4, 'msg': 'fail','result':None}
    while True:
        try:
            url = host + ':' + str(port) + add_pr
            header_dict = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
            data= sign_yc + '&sign=' + sign + '&token=' + token
            print('尝试第',count+1,'次请求任务。')
            try:
                print('开始尝试')
                res = requests.post(url, data=data, headers=header_dict)
                print('尝试成功')
            except Exception as e:
                print('打印错误日志:',e)
            #获取返回值
            res_text=res.text
            #转化成json
            get_field_json=json.loads(res_text)
            print('请求成功!')
            break
        except Exception as e:
            print('尝试失败:',e)
            time.sleep(1)
        finally:
            count += 1
            if count >= try_times:
                break
    print('返回值json:',get_field_json)
    return get_field_json

4. 其它平台或客户端调用

4.1 其它平台调用

按照第 3 章的逻辑自行写调用代码即可。

4.2 机器人调用

按照第 3 章添加一个全局函数,在需要调用的地方使用全局函数控件即可。