开发者学堂课程【低代码氚云应用开发课程:氚云丨开发课— 09平台 API 的调用及自定义 API 的编写与调用】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/894/detail/14286
氚云丨开发课— 09平台 API 的调用及自定义 API 的编写与调用
内容简介:
一、平台 API 的调用
二、自定义 API 的编写与调用
一、 平台 API 的调用
氚云提供 OpenApi 接口,里面有8个公共方法以及自定义接口方法,开发者可以借助这些接口能力,实现企业系统与氚云的集成打通。调用氚云接口时,需使用HTTPS 协议、JSON 数据格式、UTF8 编码,访问域名https://www.h3yun.com。
POST 请求请在 HTTP Headers 中设置
Content-Type:application/json 以及身份认证参数 EngineCode、EngineSecret。
第二个是氚云要与氚云的接口进行对接的话,首先必须是开发板,
第二必须要有两个参数:
engine code 和 secret,这两个参数拿到以后才能成功调用接口,因为不可能让所有人随意去调用公司的数据,必须要有一个验证的机制。以创建单条业务数据为例子操作 API 的调用
它的接口协议是:https://www.h3yun.com/OpenApi/Invoke,
调用方式是:POST(HTTPS),然后需要用到的就是把前面的两个参数放到 head里面,然后再进行一个 http 调用。
第一个是 ActionName,意思是标识它进行一个什么操作,是创建对象、是删除对象还是查询对象,这个 SchemeCode 就是要把这个对象创建到哪一个数据表里去。
第三个参数 BizObject 传上去的数据 json 的格式,这是第三个参数。
第四个参数 IsSummit,就是我们前两期讲到的创建 credit server 也会用到这个方法。
二、自定义 API 的编写与调用
CreateBizObject:创建单个数据。
请求方式:POST(HTTPS)
请求地址:https://www.h3yun.com/OpenApi/lnvoke
请求包体:"ActionName":"CreateBizObject",
"SchemaCode":"D000024chuangjian"
BizObject":"(
\"CreatedBy\": \"f3f69a49-edf6-468d-9aee-8cbc82a46662\"
\"Ownerld\":\" f3f69a49-edf6-468d-9aee-8cbc82a46662\"
\"F0000002\":\"123\
\"F0000009\:\"03ea2021-f7d5-4001-b996-
7115e63f4319;6d1af175-a49d-48ad-bc3e-52aa35bb34dfdb4d0ace-9a0c-4c57-bc347138856c6a6\"
\"D000024Fdetail123\":[
代码:
using System;
using System. Collections. Generic;using System. Ling;
using System.Text;
usIng System. Threading.Tasks,using System I0,
using Newtonsoft.Json;using System. Net;
using System. Configuration;
namespace H3.Commons
1个引用
public class H3YunAPIHelp
{
{
0个引用
public H3YunAPIHelpo[
//
TODD: Add constructor logic here
0个引用
pub1ic static string H3YunApi(string ActionName,Dictionary<string,string〉 pargam,out bool sucessfull){
try
string webApiAddress = ConfigurationManager.AppSettings["13YunApiAddress"].ToStringo:string engineCode =
ConfigurationManager.AppSettings["EngineCode"]. ToStringO;
string secret = ConfigurationManager.
AppSettings["EngineSecret"].ToString0;
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(webApiAddress);request. Method- "PoST";
Dictionary<string,object> dicParams = new DictionaryKstring,object>0);dicParams.Add("ActionName". ActionName) :
//自定义参数
if (pargam != null &&pargam.Count > o)
foreach (KeyValuePair<string,string> kv in pargam){
dicParams.Add (kv.Key,kv.Va1ue);
string jsonData =:JsonConvert.Serialize0bject(dicParams) ;byte[] bytes;
bytes = System. Text.Encoding. UTF8.GetBytes(jsonData);request.ContentLength = bytes. Length;
string StrDate = string.Empty;
string strValue = string.Empty;
using (Stream writer = request.GetRequestStreamO)
writer.Write(bytes,0, bytes.Length);
writer. C1oseO ;
using (System,Net. HttpWebResponse response = (System.Net. IittpWebResponse)request.GetResponse())
using (System. I0.Stream s = response.GetResponseStreamO)
using (StreamReader Reader # new StreamReader(s,Encoding.UTF8))
while ((StrDate = Reader. ReadlineO) != nul1)
strValue i= StrDate + "lr\n";
using (Stream writer request.CetRequestStreamO
writer.Write(bytes,0, bytes.Length);
writer. CloseO ;
using (System.Net.HIttpWebResponse response = (System.Net.HlittpWebResponse)request.GetResponseO)
using (System.I0.Stream s = response.GetResponseStreamO)
{
using (StreamReader Reader = new StreamReader(s,Encoding. UTF8)){
while ((StrDate= Reader. ReadLineO)!= nul1)
strvalue i= StrDate + "irln";
sucessfull = true;
return strValue;
catch (System.Web.Services.Protocols.SoapException ee)
sucessfull =false;
return ee.ToStringO;
身份认证:
每一次请求 OpenApi 接口都必须携带身份认证参数:EngineCode、EngineSecret。
注意事项:Secret 重置后,更新接口中的 EngineSecret 参数的值
查询单条业务数据:
LoadBizObject 为加载单个数据,请勿使用该接口来循环加载数据,可以使用LoadBizObjects 来批量加载数据。
请求方式:POST(HTTPS)
请求地址:https://www.h3yun.com/OpenApi/invoke
请求包体:
{
"ActionName":"LoadBizObject",
"SchemaCode":"D000024chuangjian",
"BizObjectId":"fea0ade0-505d-4ffc-bca0-6cbdf3c302c6"
}
请求示例(C#):
string apiAddress = @"https://www.h3yun.com/OpenApi/Invoke";
HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(apiAddress);
request.Method = "POST";
request.ContentType = "application/json";
//身份认证参数
request.Headers.Add("EngineCode", "");
request.Headers.Add("EngineSecret","");
//参数
Dictionary<string, object> dicParams = new Dictionary<string, object>();
dicParams.Add("ActionName", "LoadBizObject");
dicParams.Add("SchemaCode", "D000024chuangjian");
dicParams.Add("BizObjectId","fea0ade0-505d-4ffc-bca0-6cbdf3c302c6");
string jsonData = JsonConvert.SerializeObject(dicParams);
byte[] bytes;
bytes = System.Text.Encoding.UTF8.GetBytes(jsonData);
request.ContentLength = bytes.Length;
using (Stream writer = request.GetRequestStream())
{
writer.Write(bytes, 0, bytes.Length);
writer.Close();
}
string strValue = string.Empty;
using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse())
{
using (System.IO.Stream s = response.GetResponseStream())
{
string StrDate = string.Empty;
using (StreamReader Reader = new StreamReader(s, Encoding.UTF8))
{
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
}
}
}
返回结果:
{
"Successful": true,
"ErrorMessage": null,
"Logined": false,
"ReturnData": {
"BizObject": {
"ObjectId": "fea0ade0-505d-4ffc-bca0-6cbdf3c302c6",
"Name": "123",
"ModifiedBy": "System",
"ModifiedTime": "2019/6/12 14:32:06",
"WorkflowInstanceId": "f6a030fc-1311-4877-bc88-cb2649a8f784",
"Status": 2,
"OwnerId": "System",
"F0000028": null,
"F0000002": "123",
"ModifiedByObject": {
"ObjectId": "18f923a7-5a5e-426d-94ae-a55ad1a4b239",
"Name": "System"
},
"OwnerIdObject": {
"ObjectId": "f3f69a49-edf6-468d-9aee-8cbc82a46662",
"Name": "System"
},
"CreatedByObject": {
"ObjectId": "f3f69a49-edf6-468d-9aee-8cbc82a46662",
"Name": "System"
},
"OwnerDeptIdObject": {
"ObjectId": "04204118-a9d4-42bf-898b-1cb2f2f73e29",
"Name": "System部门"
}
}
},
"DataType": 0
}
创建单条业务数据
CreateBizObject: 创建单个数据。
请求方式:POST(HTTPS)
请求地址:https://www.h3yun.com/OpenApi/invoke
请求包体:
{
"ActionName":"CreateBizObject",
"SchemaCode":"D000024chuangjian",
"BizObject":" {
\"CreatedBy\": \"f3f69a49-edf6-468d-9aee-8cbc82a46662\",
\"OwnerId\": \" f3f69a49-edf6-468d-9aee-8cbc82a46662\",
\"F0000002\": \"123\",
\"F0000009\": \"03ea2021-f7d5-4001-b996-7115e63f4319;6d1af175-a49d-48ad-bc3e-52aa35bb34df;db4d0ace-9a0c-4c57-bc3d-47138856c6a6\",
\"D000024Fdetail123\": [
{
\"zh\": \"123\"
},
{
\"zh\": \"312\"
}
]
}",
"IsSubmit":"true"
}
请求示例(C#):
string apiAddress = @"https://www.h3yun.com/OpenApi/Invoke";
HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(apiAddress);
request.Method = "POST";
request.ContentType = "application/json";
//身份认证参数
request.Headers.Add("EngineCode", "");
request.Headers.Add("EngineSecret","");
//参数
Dictionary<string, object> dicParams = new Dictionary<string, object>();
dicParams.Add("ActionName", "CreateBizObject");
dicParams.Add("SchemaCode", "D000024chuangjian");
dicParams.Add("BizObject","{
\"CreatedBy\": \"f3f69a49-edf6-468d-9aee-8cbc82a46662\",
\"OwnerId\": \" f3f69a49-edf6-468d-9aee-8cbc82a46662\",
\"F0000002\": \"123\",
\"F0000009\": \"03ea2021-f7d5-4001-b996-7115e63f4319;6d1af175-a49d-48ad-bc3e-52aa35bb34df;db4d0ace-9a0c-4c57-bc3d-47138856c6a6\",
\"D000024Fdetail123\": [
{
\"zh\": \"123\"
},
{
\"zh\": \"312\"
}
]
}");
dicParams.Add("IsSubmit", "true");
string jsonData = JsonConvert.SerializeObject(dicParams);
byte[] bytes;
bytes = System.Text.Encoding.UTF8.GetBytes(jsonData);
request.ContentLength = bytes.Length;
using (Stream writer = request.GetRequestStream())
{
writer.Write(bytes, 0, bytes.Length);
writer.Close();
}
string strValue = string.Empty;
using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse())
{
using (System.IO.Stream s = response.GetResponseStream())
{
string StrDate = string.Empty;
using (StreamReader Reader = new StreamReader(s, Encoding.UTF8))
{
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
}
}
}
请求示例(JAVA):
Map<String, String> paramMap = new HashMap();
paramMap.put("ActionName", "CreateBizObject");
paramMap.put("SchemaCode", "D000024chuangjian");
paramMap.put("BizObject","{
\"CreatedBy\": \"f3f69a49-edf6-468d-9aee-8cbc82a46662\",
\"OwnerId\": \" f3f69a49-edf6-468d-9aee-8cbc82a46662\",
\"F0000002\": \"123\",
\"F0000009\": \"03ea2021-f7d5-4001-b996-7115e63f4319;6d1af175-a49d-48ad-bc3e-52aa35bb34df;db4d0ace-9a0c-4c57-bc3d-47138856c6a6\",
\"D000024Fdetail123\": [
{
\"zh\": \"123\"
},
{
\"zh\": \"312\"
}
]
}");
paramMap.put("IsSubmit", "true");
//身份认证参数
Map headers = new HashMap();
headers.put("EngineCode","");
headers.put("EngineSecret","");
Gson gson = new Gson();
String result = HttpRequestUtil.sendPost(url,gson.toJson(paramMap), headers);
返回结果:{
"Successful": true,
"ErrorMessage": null,
"Logined": false,
"ReturnData": {
"BizObjectId": "9adb4077-00a9-4806-8abc-ec753bdab7a6",
"WorkflowInstanceId": "9b0c1c76-395d-4ff1-9807-dfcf887becb6"
},
"DataType": 0
}