mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 12:56:28 +08:00
完善SDK
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace SDKCSharp.Model
|
namespace SDKCSharp.Model
|
||||||
{
|
{
|
||||||
public class GetStoryModel
|
public class GetProductModel
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
@@ -44,7 +44,7 @@ namespace SDKTest
|
|||||||
// 创建请求对象
|
// 创建请求对象
|
||||||
GetProductRequest request = new GetProductRequest();
|
GetProductRequest request = new GetProductRequest();
|
||||||
// 请求参数
|
// 请求参数
|
||||||
GetStoryModel model = new GetStoryModel();
|
GetProductModel model = new GetProductModel();
|
||||||
model.Id = 1;
|
model.Id = 1;
|
||||||
request.BizModel = model;
|
request.BizModel = model;
|
||||||
|
|
||||||
|
15
sop-sdk/sdk-csharp/SDKCSharp/Request/GetProductRequest.cs
Normal file
15
sop-sdk/sdk-csharp/SDKCSharp/Request/GetProductRequest.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using SDKCSharp.Common;
|
||||||
|
using SDKCSharp.Response;
|
||||||
|
|
||||||
|
namespace SDKCSharp.Request
|
||||||
|
{
|
||||||
|
public class GetProductRequest : BaseRequest<GetProductResponse>
|
||||||
|
{
|
||||||
|
public override string GetMethod()
|
||||||
|
{
|
||||||
|
return "product.get";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
18
sop-sdk/sdk-csharp/SDKCSharp/Response/GetProductResponse.cs
Normal file
18
sop-sdk/sdk-csharp/SDKCSharp/Response/GetProductResponse.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace SDKCSharp.Response
|
||||||
|
{
|
||||||
|
public class GetProductResponse
|
||||||
|
{
|
||||||
|
[JsonProperty("id")]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("name")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("gmt_create")]
|
||||||
|
public string GmtCreate { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -9,21 +9,22 @@ C#对应的SDK,由于本人使用mac开发,因此使用了.Net Core
|
|||||||
|
|
||||||
比如获取故事信息接口
|
比如获取故事信息接口
|
||||||
|
|
||||||
- 接口名:alipay.story.find
|
- 接口名:product.get
|
||||||
- 版本号:1.0
|
- 版本号:1.0
|
||||||
- 参数:name 故事名称
|
- 参数:id
|
||||||
- 返回信息
|
- 返回信息
|
||||||
|
|
||||||
```
|
```
|
||||||
{
|
{
|
||||||
"alipay_story_find_response": {
|
"code": "0",
|
||||||
"msg": "Success",
|
"msg": "success",
|
||||||
"code": "10000",
|
"sub_code": "",
|
||||||
"name": "白雪公主",
|
"sub_msg": "",
|
||||||
|
"data": {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"gmtCreate": 1554193987378
|
"name": "冰 箱 -env=gray",
|
||||||
},
|
"gmt_create": null
|
||||||
"sign": "xxxxx"
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -34,14 +35,15 @@ C#对应的SDK,由于本人使用mac开发,因此使用了.Net Core
|
|||||||
```
|
```
|
||||||
namespace SDKCSharp.Model
|
namespace SDKCSharp.Model
|
||||||
{
|
{
|
||||||
public class GetStoryModel
|
public class GetProductModel
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 故事名称
|
/// id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The name.</value>
|
/// <value>The id.</value>
|
||||||
[JsonProperty("name")]
|
[JsonProperty("id")]
|
||||||
public string Name { get; set; }
|
public int Id { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -49,14 +51,14 @@ namespace SDKCSharp.Model
|
|||||||
`[JsonProperty("name")]`是Newtonsoft.Json组件中的类,用于Json序列化,括号中的是参数名称。
|
`[JsonProperty("name")]`是Newtonsoft.Json组件中的类,用于Json序列化,括号中的是参数名称。
|
||||||
类似于Java中的注解,`@JSONField(name = "xx")`
|
类似于Java中的注解,`@JSONField(name = "xx")`
|
||||||
|
|
||||||
2.在`Response`包下新建一个返回类GetStoryResponse,继承`BaseResponse`
|
2.在`Response`包下新建一个返回类GetProductResponse
|
||||||
|
|
||||||
里面填写返回的字段
|
里面填写返回的字段
|
||||||
|
|
||||||
```
|
```
|
||||||
namespace SDKCSharp.Response
|
namespace SDKCSharp.Response
|
||||||
{
|
{
|
||||||
public class GetStoryResponse: BaseResponse
|
public class GetProductResponse
|
||||||
{
|
{
|
||||||
[JsonProperty("id")]
|
[JsonProperty("id")]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
@@ -69,7 +71,6 @@ namespace SDKCSharp.Response
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
3.在`Request`文件夹下新建一个请求类,继承`BaseRequest`
|
3.在`Request`文件夹下新建一个请求类,继承`BaseRequest`
|
||||||
@@ -82,11 +83,11 @@ BaseRequest中有个泛型参数,填`GetStoryResponse`类,表示这个请求
|
|||||||
```
|
```
|
||||||
namespace SDKCSharp.Request
|
namespace SDKCSharp.Request
|
||||||
{
|
{
|
||||||
public class GetStoryRequest : BaseRequest<GetStoryResponse>
|
public class GetProductRequest : BaseRequest<GetProductResponse>
|
||||||
{
|
{
|
||||||
public override string GetMethod()
|
public override string GetMethod()
|
||||||
{
|
{
|
||||||
return "alipay.story.find";
|
return "product.get";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,63 +119,27 @@ class MainClass
|
|||||||
private static void TestGet()
|
private static void TestGet()
|
||||||
{
|
{
|
||||||
// 创建请求对象
|
// 创建请求对象
|
||||||
GetStoryRequest request = new GetStoryRequest();
|
GetProductRequest request = new GetProductRequest();
|
||||||
// 请求参数
|
// 请求参数
|
||||||
GetStoryModel model = new GetStoryModel();
|
GetProductModel model = new GetProductModel();
|
||||||
model.Name = "白雪公主";
|
model.Id = 1;
|
||||||
request.BizModel = model;
|
request.BizModel = model;
|
||||||
|
|
||||||
// 发送请求
|
// 发送请求
|
||||||
GetStoryResponse response = client.Execute(request);
|
Result<GetProductResponse> result = client.Execute(request);
|
||||||
|
|
||||||
if (response.IsSuccess())
|
if (result.IsSuccess())
|
||||||
{
|
{
|
||||||
// 返回结果
|
// 返回结果
|
||||||
Console.WriteLine("成功!response:{0}\n响应原始内容:{1}", JsonUtil.ToJSONString(response), response.Body);
|
Console.WriteLine("成功!response:{0}\n响应原始内容:{1}", JsonUtil.ToJSONString(result), result.Data);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Console.WriteLine("错误, code:{0}, msg:{1}, subCode:{2}, subMsg:{3}",
|
Console.WriteLine("错误, code:{0}, msg:{1}, subCode:{2}, subMsg:{3}",
|
||||||
response.Code, response.Msg, response.SubCode, response.SubMsg);
|
result.Code, result.Msg, result.SubCode, result.SubMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 使用方式2(懒人版)
|
|
||||||
|
|
||||||
如果不想添加Request,Response,Model。可以用这种方式,返回data部分是Dictionary<string, object>,后续自己处理
|
|
||||||
|
|
||||||
```
|
|
||||||
// 懒人版,如果不想添加Request,Response,Model。可以用这种方式,返回Dictionary<string, object>,后续自己处理
|
|
||||||
private static void TestCommon()
|
|
||||||
{
|
|
||||||
// 创建请求对象
|
|
||||||
CommonRequest request = new CommonRequest("alipay.story.find");
|
|
||||||
// 请求参数
|
|
||||||
Dictionary<string, string> bizModel = new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
["name"] = "白雪公主"
|
|
||||||
};
|
|
||||||
|
|
||||||
request.BizModel = bizModel;
|
|
||||||
|
|
||||||
// 发送请求
|
|
||||||
CommonResponse response = client.Execute(request);
|
|
||||||
|
|
||||||
if (response.IsSuccess())
|
|
||||||
{
|
|
||||||
// 返回结果
|
|
||||||
string body = response.Body;
|
|
||||||
Dictionary<string, object> dict = JsonUtil.ParseToDictionary(body);
|
|
||||||
Console.WriteLine(dict.ToString());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine("错误, code:{0}, msg:{1}, subCode:{2}, subMsg:{3}",
|
|
||||||
response.Code, response.Msg, response.SubCode, response.SubMsg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
@@ -1,33 +1,35 @@
|
|||||||
# sdk-java
|
# sdk-java
|
||||||
|
|
||||||
|
开放平台把接口开发完毕后,一般需要开发对应的SDK,提供给ISV。SOP提供了一个基础的SDK开发包
|
||||||
|
|
||||||
sdk for java
|
开发者可以在此基础上做开发,就拿sdk-java来说,具体步骤如下:
|
||||||
|
|
||||||
SDK只依赖了三个jar包
|
## sdk-java
|
||||||
|
SDK依赖了三个jar包
|
||||||
|
|
||||||
- okhttp.jar 用于网络请求
|
+ okhttp.jar 用于网络请求
|
||||||
- fastjson.jar 用于json处理
|
+ fastjson.jar 用于json处理
|
||||||
- commons-logging.jar 日志处理
|
+ commons-logging.jar 日志处理
|
||||||
|
|
||||||
## 接口封装步骤
|
|
||||||
|
|
||||||
|
### 接口封装步骤
|
||||||
比如获取故事信息接口
|
比如获取故事信息接口
|
||||||
|
|
||||||
- 接口名:alipay.story.find
|
+ 接口名:story.get
|
||||||
- 版本号:1.0
|
+ 版本号:1.0
|
||||||
- 参数:name 故事名称
|
+ 参数:id
|
||||||
- 返回信息
|
+ 返回信息
|
||||||
|
|
||||||
```
|
```json
|
||||||
{
|
{
|
||||||
"alipay_story_find_response": {
|
"subCode": "",
|
||||||
"msg": "Success",
|
"subMsg": "",
|
||||||
"code": "10000",
|
"code": "0",
|
||||||
"name": "白雪公主",
|
"msg": "success",
|
||||||
"id": 1,
|
"data": {
|
||||||
"gmtCreate": 1554193987378
|
"addTime": "2024-11-08 10:21:23",
|
||||||
},
|
"name": "乌鸦喝水",
|
||||||
"sign": "xxxxx"
|
"id": 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -36,25 +38,25 @@ SDK只依赖了三个jar包
|
|||||||
1.在`model`包下新建一个类,定义业务参数
|
1.在`model`包下新建一个类,定义业务参数
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@Data
|
@Data
|
||||||
public class GetStoryModel {
|
public class GetStoryModel {
|
||||||
|
|
||||||
@JSONField(name = "name")
|
private Integer id;
|
||||||
private String name;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
2.在`response`包下新建一个返回类GetStoryResponse,继承`BaseResponse`
|
2.在`response`包下新建一个返回类GetStoryResponse
|
||||||
|
|
||||||
里面填写返回的字段
|
里面填写返回的字段
|
||||||
|
|
||||||
```
|
```plain
|
||||||
@Data
|
@Data
|
||||||
public class GetStoryResponse extends BaseResponse {
|
public class GetStoryResponse {
|
||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
private Date gmtCreate;
|
private Date addTime;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -69,14 +71,24 @@ BaseRequest中有个泛型参数,填`GetStoryResponse`类,表示这个请求
|
|||||||
public class GetStoryRequest extends BaseRequest<GetStoryResponse> {
|
public class GetStoryRequest extends BaseRequest<GetStoryResponse> {
|
||||||
@Override
|
@Override
|
||||||
protected String method() {
|
protected String method() {
|
||||||
return "alipay.story.find";
|
return "story.get";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 使用方式
|
可重写`getRequestMethod()`方法指定HTTP请求method,默认是POST。
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Override
|
||||||
|
protected RequestMethod getRequestMethod() {
|
||||||
|
return RequestMethod.GET;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**建议读请求用GET,写请求用POST**,
|
||||||
|
|
||||||
|
### 使用方式
|
||||||
```java
|
```java
|
||||||
String url = "http://localhost:8081/api";
|
String url = "http://localhost:8081/api";
|
||||||
String appId = "2019032617262200001";
|
String appId = "2019032617262200001";
|
||||||
@@ -85,55 +97,25 @@ String privateKey = "你的私钥";
|
|||||||
// 声明一个就行
|
// 声明一个就行
|
||||||
OpenClient client = new OpenClient(url, appId, privateKey);
|
OpenClient client = new OpenClient(url, appId, privateKey);
|
||||||
|
|
||||||
// 标准用法
|
|
||||||
@Test
|
@Test
|
||||||
public void testGet() {
|
public void testGet() {
|
||||||
// 创建请求对象
|
// 创建请求对象
|
||||||
GetStoryRequest request = new GetStoryRequest();
|
GetStoryRequest request = new GetStoryRequest();
|
||||||
// 请求参数
|
// 请求参数
|
||||||
GetStoryModel model = new GetStoryModel();
|
GetStoryModel model = new GetStoryModel();
|
||||||
model.setName("白雪公主");
|
model.setId(1);
|
||||||
|
|
||||||
request.setBizModel(model);
|
request.setBizModel(model);
|
||||||
|
|
||||||
// 发送请求
|
// 发送请求
|
||||||
GetStoryResponse response = client.execute(request);
|
Result<GetStoryResponse> result = client.execute(request);
|
||||||
|
|
||||||
if (response.isSuccess()) {
|
if (result.isSuccess()) {
|
||||||
|
GetStoryResponse response = result.getData();
|
||||||
// 返回结果
|
// 返回结果
|
||||||
System.out.println(response);
|
System.out.println(String.format("response:%s",
|
||||||
|
JSON.toJSONString(response)));
|
||||||
} else {
|
} else {
|
||||||
System.out.println(response);
|
System.out.println("错误,subCode:" + result.getSubCode() + ", subMsg:" + result.getSubMsg());
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 使用方式2(懒人版)
|
|
||||||
|
|
||||||
如果不想添加Request,Response,Model。可以用这种方式,返回body部分是字符串,后续自己处理
|
|
||||||
|
|
||||||
body对应的是alipay_story_find_response部分
|
|
||||||
|
|
||||||
```java
|
|
||||||
@Test
|
|
||||||
public void testLazy() {
|
|
||||||
// 创建请求对象
|
|
||||||
CommonRequest request = new CommonRequest("alipay.story.find");
|
|
||||||
// 请求参数
|
|
||||||
Map<String, Object> bizModel = new HashMap<>();
|
|
||||||
bizModel.put("name", "白雪公主");
|
|
||||||
request.setBizModel(bizModel);
|
|
||||||
|
|
||||||
// 发送请求
|
|
||||||
CommonResponse response = client.execute(request);
|
|
||||||
|
|
||||||
if (response.isSuccess()) {
|
|
||||||
// 返回结果,body对应的是alipay_story_find_response部分
|
|
||||||
String body = response.getBody();
|
|
||||||
JSONObject jsonObject = JSON.parseObject(body);
|
|
||||||
System.out.println(jsonObject);
|
|
||||||
} else {
|
|
||||||
System.out.println(response);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user