和小青GPT助理聊天吧

小青是一位基于大语言预训练模型的超级智能AI,熟悉轻易云产品并能为您提供多种数据集成方案和技术支持。您在使用轻易云产品时遇到问题或有关技术咨询,小青都将为您提供专业、即时、高效的帮助和建议。与小青智能对话,您可享受同人类互动一样的使用体验,全面了解最新产品更新、功能特性及相关技术资讯,进一步提升您在数据集成领域的实力。小青是您友好、智能、便捷的助手,与她互动让您的数据集成之路轻松愉快。
轻易云自训练大语言模型小青助理
扫码关注公众号即可开始聊天

适配器SDK实现方法,connection和invoke

适配器SDK实现方法,connection和invoke

SDK是用于实现与软件平台连接、调用的类,被需要的适配器引用并实例化。实例化时,会传入基本的连接参数给SDK构造方法。

namespace Adapter\PlatformName\SDK;

class PlatformNameSDK
{
    protected $connectorId = 'connectorId';
    protected $env = '';
    protected $host = '';
    protected $login = ['appKey' => 'xxxxxx', 'appSecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',];
    protected $token = null;
    protected $client = \GuzzleHttp\Client::class;
    
    public function __construct($connectorId, $params, string $env = '')
    {
        $this->connectorId = $connectorId;
        $this->host = $params['host'];
        $this->login = $params;
        $this->env = $env;
        $this->client = new \GuzzleHttp\Client();
    }

    public function invoke(string $api, $params = [], $method = 'POST')
    {
    }

    public function connection()
    {
    }
}

实现SDK->connection(),连接到目标平台的方法,主要针对需要token鉴权的平台,用于管理token。

public function connection()
{
    $cacheKey = $this->connectorId . $this->env;
    $token = Cache::get($cacheKey);
    
    if ($token) {
        $this->token = $token;
        return ['status' => true, 'token' => $token];
    }

    $url = $this->host . '/open-apis/auth/v3/tenant_access_token/internal';
    $response = $this->client->post($url, ['form_params' => $this->login, 'headers' => ['Content-Type' => 'application/json; charset=utf-8',]]);
    $body = $response->getBody();
    $arr = json_decode((string)$body, true);
    
    if ($arr['code'] == 0) {
        $this->token = $arr['tenant_access_token'];
        Cache::put($cacheKey, $this->token, $arr['expire'] - 100);
    }
    return $arr;
}

实现SDK->invoke(),实现具体接口调用方法。

public function invoke(string $api, $params = [], $method = 'POST')
{
    $url = $this->host . $api;
    $sign = $this->generateSign($params);
    $headers = ['accesstoken' => $this->token, 'sign' => $sign, 'Content-Type' => 'application/json'];

    if ($method === 'get' || $method === 'GET') {
        $response = $this->client->get($url, ['query' => $params,'http_errors' => false,'headers' => $headers]);
    } else {
        $response = $this->client->post($url, ['body'=>json_encode($params),'http_errors' => false,'headers' => $headers]);
    }

    $body = $response->getBody();
    $bodyStr = (string)$body;
    $arr = json_decode($bodyStr,true);
    return $arr;
}

protected function generateSign($params)
{
    $jsonStr = json_encode($params).$this->login['appKey'];
    return md5($jsonStr);
}

热门文章

接口配置打通金蝶云星空和万里牛数据对接

2023-01-26 10:11:43

管易云与金蝶云星空对接集成采购入库查询连通采购入库单新增并审核(金蝶采购入库单——管易采购入库单)

2023-01-26 10:11:42

金蝶云星空对接打通聚水潭供应商查询接口与供应商上传接口

2023-01-26 10:11:41

用友BIP对接外部MySQL系统

2023-01-26 10:11:41

钉钉与轻易云集成平台对接集成获取通讯录打通操作映射数据(更新、新增)

2023-01-26 10:11:40