Skip to content

Commit 6c8fac2

Browse files
committed
fix
1 parent 7db2a53 commit 6c8fac2

File tree

6 files changed

+1351
-0
lines changed

6 files changed

+1351
-0
lines changed

_example/cs/api.cs

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
2+
3+
4+
5+
6+
/**
7+
* 动物接口
8+
*/
9+
public class AnimalController
10+
{
11+
12+
private IApiClient client;
13+
14+
public AnimalController(IApiClient client) {
15+
this.client = client;
16+
}
17+
18+
19+
/**
20+
* 响应long
21+
*
22+
*/
23+
public Task<int> addCat( Cat body) {
24+
25+
return client.Post<,>("/animal/cat/add",body);
26+
}
27+
28+
29+
/**
30+
* 路径+Body
31+
*
32+
*/
33+
public Task<Dog> addDog( string kind, Dog body) {
34+
35+
return client.Post<,>(string.Format("/animal/add/{0}/dog",kind),body);
36+
}
37+
38+
39+
/**
40+
* 响应list
41+
*
42+
*/
43+
public Task<List<Cat>> cats() {
44+
var param = new Dictionary<string,dynamic>();
45+
return client.Get<List<Cat>>("/animal/cats",param);
46+
}
47+
48+
49+
/**
50+
* 响应对象
51+
*
52+
*/
53+
public Task<Dog> dog( int id) {
54+
var param = new Dictionary<string,dynamic>();
55+
return client.Get<Dog>(string.Format("/animal/dog/{0}",id),param);
56+
}
57+
58+
59+
/**
60+
* 响应map
61+
* 其他
62+
*/
63+
public Task<Dictionary<string,Cat>> mapCats() {
64+
var param = new Dictionary<string,dynamic>();
65+
return client.Get<Dictionary<string,Cat>>("/animal/map/cats",param);
66+
}
67+
68+
69+
/**
70+
* 响应Map泛型
71+
*
72+
*/
73+
public Task<MapResultDog> mapDog() {
74+
var param = new Dictionary<string,dynamic>();
75+
return client.Get<MapResultDog>("/animal/map/dogs",param);
76+
}
77+
78+
79+
/**
80+
* 响应Array泛型
81+
*
82+
*/
83+
public Task<PageDataDog> pageDogs() {
84+
var param = new Dictionary<string,dynamic>();
85+
return client.Get<PageDataDog>("/animal/page/dogs",param);
86+
}
87+
88+
89+
/**
90+
* 路径+Query
91+
*
92+
*/
93+
public Task<Dog> searchDog( string color, string kind, string name) {
94+
var param = new Dictionary<string,dynamic>();
95+
param["color"] = color;
96+
param["kind"] = kind;
97+
param["name"] = name;
98+
99+
return client.Get<Dog>(string.Format("/animal/query/{0}/dog",kind),param);
100+
}
101+
102+
103+
}
104+
105+
106+
107+
/**
108+
* 其他接口
109+
*/
110+
public class OtherApi
111+
{
112+
113+
private IApiClient client;
114+
115+
public OtherApi(IApiClient client) {
116+
this.client = client;
117+
}
118+
119+
120+
/**
121+
* 响应基础类型
122+
*
123+
*/
124+
public Task<string> get() {
125+
var param = new Dictionary<string,dynamic>();
126+
return client.Get<string>("/other/long",param);
127+
}
128+
129+
130+
/**
131+
* 响应基础类型
132+
*
133+
*/
134+
public Task<int> getContent() {
135+
var param = new Dictionary<string,dynamic>();
136+
return client.Get<int>("/other/string",param);
137+
}
138+
139+
140+
/**
141+
* 多路径参数
142+
*
143+
*/
144+
public Task<List<Dog>> multiplePathVariable( string id, string type) {
145+
var param = new Dictionary<string,dynamic>();
146+
return client.Get<List<Dog>>(string.Format("/other/boolean/{0}/{1}",type,id),param);
147+
}
148+
149+
150+
}
151+
152+
153+
154+
/**
155+
* 用户接口
156+
*/
157+
public class UserApi
158+
{
159+
160+
private IApiClient client;
161+
162+
public UserApi(IApiClient client) {
163+
this.client = client;
164+
}
165+
166+
167+
/**
168+
* 添加用户
169+
*
170+
*/
171+
public Task<ApiResultLong> add( User body) {
172+
173+
return client.Post<,>("/user/add",body);
174+
}
175+
176+
177+
/**
178+
* 删除用户
179+
*
180+
*/
181+
public Task<ApiResultBoolean> delete( long id) {
182+
var param = new Dictionary<string,dynamic>();
183+
return client.Delete<,>(string.Format("/user/delete/{0}",id),param);
184+
}
185+
186+
187+
/**
188+
* 修改用户
189+
*
190+
*/
191+
public Task<ApiResultLong> edit( long id, User body) {
192+
193+
return client.Put<,>(string.Format("/user/edit/{0}",id),body);
194+
}
195+
196+
197+
/**
198+
* 查询用户
199+
*
200+
*/
201+
public Task<ApiResultUser> edit_1( long id, string keyword) {
202+
var param = new Dictionary<string,dynamic>();
203+
param["id"] = id;
204+
param["keyword"] = keyword;
205+
206+
return client.Get<ApiResultUser>(string.Format("/user/detail/{0}",id),param);
207+
}
208+
209+
210+
/**
211+
* 用户列表
212+
*
213+
*/
214+
public Task<ApiResultListUser> list() {
215+
var param = new Dictionary<string,dynamic>();
216+
return client.Get<ApiResultListUser>("/user/list",param);
217+
}
218+
219+
220+
/**
221+
* 用户分页
222+
*
223+
*/
224+
public Task<ApiResultPageDataUser> page( int page, int size) {
225+
var param = new Dictionary<string,dynamic>();
226+
param["page"] = page;
227+
param["size"] = size;
228+
229+
return client.Get<ApiResultPageDataUser>("/user/page",param);
230+
}
231+
232+
233+
}
234+
235+
236+
237+
238+
239+
240+
241+
242+
243+
244+

_example/cs/client.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
3+
using System;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Text.Json;
7+
using System.Threading.Tasks;
8+
using System.Collections.Generic;
9+
using System.Web;
10+
11+
public interface IApiClient
12+
{
13+
public Task<T> Get<T>(string path, Dictionary<string, dynamic> parameters) where T : class;
14+
15+
public Task<T> Post<T, P>(string path, P parameters) where T : class where P : class;
16+
17+
public Task<T> Put<T, P>(string path, P parameters) where T : class where P : class;
18+
19+
public Task<T> Delete<T, P>(string path, P parameters) where T : class where P : class;
20+
}
21+
22+
public class DefaultApiClient : IApiClient
23+
{
24+
private readonly HttpClient _httpClient;
25+
private readonly JsonSerializerOptions _jsonOptions;
26+
27+
public DefaultApiClient(HttpClient httpClient)
28+
{
29+
_httpClient = httpClient;
30+
_jsonOptions = new JsonSerializerOptions
31+
{
32+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
33+
};
34+
}
35+
36+
public async Task<T> Get<T>(string path, Dictionary<string, dynamic> parameters) where T : class
37+
{
38+
var uriBuilder = new UriBuilder(_httpClient.BaseAddress + path);
39+
var query = HttpUtility.ParseQueryString(string.Empty);
40+
41+
foreach (var kvp in parameters)
42+
{
43+
query[kvp.Key] = kvp.Value.ToString();
44+
}
45+
46+
uriBuilder.Query = query.ToString();
47+
var response = await _httpClient.GetAsync(uriBuilder.ToString());
48+
return await DeserializeResponse<T>(response);
49+
}
50+
51+
public async Task<T> Post<T, P>(string path, P parameters) where T : class where P : class
52+
{
53+
var content = SerializeContent(parameters);
54+
var response = await _httpClient.PostAsync(path, content);
55+
return await DeserializeResponse<T>(response);
56+
}
57+
58+
public async Task<T> Put<T, P>(string path, P parameters) where T : class where P : class
59+
{
60+
var content = SerializeContent(parameters);
61+
var response = await _httpClient.PutAsync(path, content);
62+
return await DeserializeResponse<T>(response);
63+
}
64+
65+
public async Task<T> Delete<T, P>(string path, P parameters) where T : class where P : class
66+
{
67+
var request = new HttpRequestMessage
68+
{
69+
Method = HttpMethod.Delete,
70+
RequestUri = new Uri(_httpClient.BaseAddress + path),
71+
Content = SerializeContent(parameters)
72+
};
73+
var response = await _httpClient.SendAsync(request);
74+
return await DeserializeResponse<T>(response);
75+
}
76+
77+
private HttpContent SerializeContent<T>(T obj)
78+
{
79+
var json = JsonSerializer.Serialize(obj, _jsonOptions);
80+
return new StringContent(json, Encoding.UTF8, "application/json");
81+
}
82+
83+
private async Task<T> DeserializeResponse<T>(HttpResponseMessage response) where T : class
84+
{
85+
response.EnsureSuccessStatusCode();
86+
var json = await response.Content.ReadAsStringAsync();
87+
return JsonSerializer.Deserialize<T>(json, _jsonOptions);
88+
}
89+
}

0 commit comments

Comments
 (0)