mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
完善SDK
This commit is contained in:
@@ -9,21 +9,22 @@ C#对应的SDK,由于本人使用mac开发,因此使用了.Net Core
|
||||
|
||||
比如获取故事信息接口
|
||||
|
||||
- 接口名:alipay.story.find
|
||||
- 接口名:product.get
|
||||
- 版本号:1.0
|
||||
- 参数:name 故事名称
|
||||
- 参数:id
|
||||
- 返回信息
|
||||
|
||||
```
|
||||
{
|
||||
"alipay_story_find_response": {
|
||||
"msg": "Success",
|
||||
"code": "10000",
|
||||
"name": "白雪公主",
|
||||
"id": 1,
|
||||
"gmtCreate": 1554193987378
|
||||
},
|
||||
"sign": "xxxxx"
|
||||
"code": "0",
|
||||
"msg": "success",
|
||||
"sub_code": "",
|
||||
"sub_msg": "",
|
||||
"data": {
|
||||
"id": 1,
|
||||
"name": "冰 箱 -env=gray",
|
||||
"gmt_create": null
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -34,14 +35,15 @@ C#对应的SDK,由于本人使用mac开发,因此使用了.Net Core
|
||||
```
|
||||
namespace SDKCSharp.Model
|
||||
{
|
||||
public class GetStoryModel
|
||||
public class GetProductModel
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 故事名称
|
||||
/// id
|
||||
/// </summary>
|
||||
/// <value>The name.</value>
|
||||
[JsonProperty("name")]
|
||||
public string Name { get; set; }
|
||||
/// <value>The id.</value>
|
||||
[JsonProperty("id")]
|
||||
public int Id { get; set; }
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -49,14 +51,14 @@ namespace SDKCSharp.Model
|
||||
`[JsonProperty("name")]`是Newtonsoft.Json组件中的类,用于Json序列化,括号中的是参数名称。
|
||||
类似于Java中的注解,`@JSONField(name = "xx")`
|
||||
|
||||
2.在`Response`包下新建一个返回类GetStoryResponse,继承`BaseResponse`
|
||||
2.在`Response`包下新建一个返回类GetProductResponse
|
||||
|
||||
里面填写返回的字段
|
||||
|
||||
```
|
||||
namespace SDKCSharp.Response
|
||||
{
|
||||
public class GetStoryResponse: BaseResponse
|
||||
public class GetProductResponse
|
||||
{
|
||||
[JsonProperty("id")]
|
||||
public int Id { get; set; }
|
||||
@@ -69,7 +71,6 @@ namespace SDKCSharp.Response
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
3.在`Request`文件夹下新建一个请求类,继承`BaseRequest`
|
||||
@@ -82,11 +83,11 @@ BaseRequest中有个泛型参数,填`GetStoryResponse`类,表示这个请求
|
||||
```
|
||||
namespace SDKCSharp.Request
|
||||
{
|
||||
public class GetStoryRequest : BaseRequest<GetStoryResponse>
|
||||
public class GetProductRequest : BaseRequest<GetProductResponse>
|
||||
{
|
||||
public override string GetMethod()
|
||||
{
|
||||
return "alipay.story.find";
|
||||
return "product.get";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,63 +119,27 @@ class MainClass
|
||||
private static void TestGet()
|
||||
{
|
||||
// 创建请求对象
|
||||
GetStoryRequest request = new GetStoryRequest();
|
||||
GetProductRequest request = new GetProductRequest();
|
||||
// 请求参数
|
||||
GetStoryModel model = new GetStoryModel();
|
||||
model.Name = "白雪公主";
|
||||
GetProductModel model = new GetProductModel();
|
||||
model.Id = 1;
|
||||
request.BizModel = model;
|
||||
|
||||
// 发送请求
|
||||
GetStoryResponse response = client.Execute(request);
|
||||
|
||||
if (response.IsSuccess())
|
||||
Result<GetProductResponse> result = client.Execute(request);
|
||||
|
||||
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
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user