From 3fc2df8792cf352ee4630c93d4ff9fb76402b632 Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 4 Apr 2019 21:18:35 +0800 Subject: [PATCH] 1.2.0 --- doc/docs/files/10095_开发SDK.md | 190 +++++++++++++++++++++++- sop-sdk/sdk-csharp/SDKCSharp/Program.cs | 2 +- sop-sdk/sdk-csharp/readme.md | 8 +- 3 files changed, 191 insertions(+), 9 deletions(-) diff --git a/doc/docs/files/10095_开发SDK.md b/doc/docs/files/10095_开发SDK.md index eae72982..b21bbb04 100644 --- a/doc/docs/files/10095_开发SDK.md +++ b/doc/docs/files/10095_开发SDK.md @@ -4,7 +4,7 @@ 开发者可以在此基础上做开发,就拿sdk-java来说,具体步骤如下: -sdk for java +## sdk-java 开放平台对应的sdk,适用于Android。SDK依赖了三个jar包 @@ -12,7 +12,7 @@ sdk for java - fastjson-android.jar 用于json处理 - commons-logging.jar 日志处理 -## 接口封装步骤 +### 接口封装步骤 比如获取故事信息接口 @@ -78,7 +78,7 @@ public class GetStoryRequest extends BaseRequest { ``` -## 使用方式 +### 使用方式 ```java String url = "http://localhost:8081/api"; // zuul @@ -111,7 +111,7 @@ public void testGet() { } ``` -## 使用方式2(懒人版) +### 使用方式2(懒人版) 如果不想添加Request,Response,Model。可以用这种方式,返回body部分是字符串,后续自己处理 @@ -140,3 +140,185 @@ public void testLazy() { } } ``` + +## sdk-.net + + +### 接口封装步骤 + +比如获取故事信息接口 + +- 接口名:alipay.story.find +- 版本号:1.0 +- 参数:name 故事名称 +- 返回信息 + +``` +{ + "alipay_story_find_response": { + "msg": "Success", + "code": "10000", + "name": "白雪公主", + "id": 1, + "gmtCreate": 1554193987378 + }, + "sign": "xxxxx" +} +``` + +针对这个接口,封装步骤如下: + +1.在`Model`包下新建一个类,定义业务参数 + +``` +namespace SDKCSharp.Model +{ + public class GetStoryModel + { + /// + /// 故事名称 + /// + /// The name. + [JsonProperty("name")] + public string Name { get; set; } + } +} +``` + +`[JsonProperty("name")]`是Newtonsoft.Json组件中的类,用于Json序列化,括号中的是参数名称。 +类似于Java中的注解,`@JSONField(name = "xx")` + +2.在`Response`包下新建一个返回类GetStoryResponse,继承`BaseResponse` + +里面填写返回的字段 + +``` +namespace SDKCSharp.Response +{ + public class GetStoryResponse: BaseResponse + { + [JsonProperty("id")] + public int Id { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("gmt_create")] + public string GmtCreate { get; set; } + + } +} + +``` + +3.在`Request`文件夹下新建一个请求类,继承`BaseRequest` + +BaseRequest中有个泛型参数,填`GetStoryResponse`类,表示这个请求对应的返回类。 +重写`GetMethod()`方法,填接口名。 + +如果要指定版本号,可重写`GetVersion()`方法,或者后续使用`request.Version = version`进行设置 + +``` +namespace SDKCSharp.Request +{ + public class GetStoryRequest : BaseRequest + { + public override string GetMethod() + { + return "alipay.story.find"; + } + } + +} +``` + + + +### 使用方式 + +``` +class MainClass +{ + static string url = "http://localhost:8081/api"; // zuul + static string appId = "2019032617262200001"; + // 支付宝私钥 + static string privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYU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XR3KcztL1Qu2k5pB6Si0K/uwQ="; + + + // 声明一个就行 + static OpenClient client = new OpenClient(url, appId, privateKey); + + public static void Main(string[] args) + { + TestGet(); + } + + // 标准用法 + private static void TestGet() + { + // 创建请求对象 + GetStoryRequest request = new GetStoryRequest(); + // 请求参数 + GetStoryModel model = new GetStoryModel(); + model.Name = "白雪公主"; + request.BizModel = model; + + // 发送请求 + GetStoryResponse response = client.Execute(request); + + if (response.IsSuccess()) + { + // 返回结果 + Console.WriteLine("故事名称:{0}", response.Name); + } + else + { + Console.WriteLine("错误, code:{0}, msg:{1}, subCode:{2}, subMsg:{3}", + response.Code, response.Msg, response.SubCode, response.SubMsg); + } + } + + +} +``` + +### 使用方式2(懒人版) + +如果不想添加Request,Response,Model。可以用这种方式,返回data部分是Dictionary,后续自己处理 + +``` +// 懒人版,如果不想添加Request,Response,Model。可以用这种方式,返回Dictionary,后续自己处理 +private static void TestCommon() +{ + // 创建请求对象 + CommonRequest request = new CommonRequest("alipay.story.find"); + // 请求参数 + Dictionary bizModel = new Dictionary + { + ["name"] = "白雪公主" + }; + + request.BizModel = bizModel; + + // 发送请求 + CommonResponse response = client.Execute(request); + + if (response.IsSuccess()) + { + // 返回结果 + string body = response.Body; + Dictionary dict = JsonUtil.ParseToDictionary(body); + + Console.WriteLine("Dictionary内容:"); + foreach (var item in dict) + { + Console.WriteLine("{0}:{1}", item.Key, item.Value); + } + } + else + { + Console.WriteLine("错误, code:{0}, msg:{1}, subCode:{2}, subMsg:{3}", + response.Code, response.Msg, response.SubCode, response.SubMsg); + } +} +``` \ No newline at end of file diff --git a/sop-sdk/sdk-csharp/SDKCSharp/Program.cs b/sop-sdk/sdk-csharp/SDKCSharp/Program.cs index a021617c..b418252f 100644 --- a/sop-sdk/sdk-csharp/SDKCSharp/Program.cs +++ b/sop-sdk/sdk-csharp/SDKCSharp/Program.cs @@ -55,7 +55,7 @@ namespace SDKTest } } - // 懒人版,如果不想添加Request,Response,Model。可以用这种方式,返回全部是String,后续自己处理json + // 懒人版,如果不想添加Request,Response,Model。可以用这种方式,返回Dictionary,后续自己处理 private static void TestCommon() { // 创建请求对象 diff --git a/sop-sdk/sdk-csharp/readme.md b/sop-sdk/sdk-csharp/readme.md index 6e36becd..8d544207 100644 --- a/sop-sdk/sdk-csharp/readme.md +++ b/sop-sdk/sdk-csharp/readme.md @@ -131,11 +131,11 @@ class MainClass if (response.IsSuccess()) { // 返回结果 - Console.WriteLine("故事名称:{0}", response.Name); + Console.WriteLine("成功!response:{0}\n响应原始内容:{1}", JsonUtil.ToJSONString(response), response.Body); } 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); } } @@ -144,12 +144,12 @@ class MainClass } ``` -# 使用方式2(懒人版) +## 使用方式2(懒人版) 如果不想添加Request,Response,Model。可以用这种方式,返回data部分是Dictionary,后续自己处理 ``` -// 懒人版,如果不想添加Request,Response,Model。可以用这种方式,返回全部是String,后续自己处理json +// 懒人版,如果不想添加Request,Response,Model。可以用这种方式,返回Dictionary,后续自己处理 private static void TestCommon() { // 创建请求对象