vars.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package dartgen
  2. import "text/template"
  3. var funcMap = template.FuncMap{
  4. "getBaseName": getBaseName,
  5. "getPropertyFromMember": getPropertyFromMember,
  6. "isDirectType": isDirectType,
  7. "isClassListType": isClassListType,
  8. "getCoreType": getCoreType,
  9. "lowCamelCase": lowCamelCase,
  10. "normalizeHandlerName": normalizeHandlerName,
  11. "hasUrlPathParams": hasUrlPathParams,
  12. "extractPositionalParamsFromPath": extractPositionalParamsFromPath,
  13. "makeDartRequestUrlPath": makeDartRequestUrlPath,
  14. }
  15. const (
  16. apiFileContent = `import 'dart:io';
  17. import 'dart:convert';
  18. import '../vars/kv.dart';
  19. import '../vars/vars.dart';
  20. /// 发送POST请求.
  21. ///
  22. /// data:为你要post的结构体,我们会帮你转换成json字符串;
  23. /// ok函数:请求成功的时候调用,fail函数:请求失败的时候会调用,eventually函数:无论成功失败都会调用
  24. Future apiPost(String path, dynamic data,
  25. {Map<String, String> header,
  26. Function(Map<String, dynamic>) ok,
  27. Function(String) fail,
  28. Function eventually}) async {
  29. await _apiRequest('POST', path, data,
  30. header: header, ok: ok, fail: fail, eventually: eventually);
  31. }
  32. /// 发送GET请求.
  33. ///
  34. /// ok函数:请求成功的时候调用,fail函数:请求失败的时候会调用,eventually函数:无论成功失败都会调用
  35. Future apiGet(String path,
  36. {Map<String, String> header,
  37. Function(Map<String, dynamic>) ok,
  38. Function(String) fail,
  39. Function eventually}) async {
  40. await _apiRequest('GET', path, null,
  41. header: header, ok: ok, fail: fail, eventually: eventually);
  42. }
  43. Future _apiRequest(String method, String path, dynamic data,
  44. {Map<String, String> header,
  45. Function(Map<String, dynamic>) ok,
  46. Function(String) fail,
  47. Function eventually}) async {
  48. var tokens = await getTokens();
  49. try {
  50. var client = HttpClient();
  51. HttpClientRequest r;
  52. if (method == 'POST') {
  53. r = await client.postUrl(Uri.parse(serverHost + path));
  54. } else {
  55. r = await client.getUrl(Uri.parse(serverHost + path));
  56. }
  57. var strData = '';
  58. if (data != null) {
  59. strData = jsonEncode(data);
  60. }
  61. if (method == 'POST') {
  62. r.headers.set('Content-Type', 'application/json; charset=utf-8');
  63. r.headers.set('Content-Length', utf8.encode(strData).length);
  64. }
  65. if (tokens != null) {
  66. r.headers.set('Authorization', tokens.accessToken);
  67. }
  68. if (header != null) {
  69. header.forEach((k, v) {
  70. r.headers.set(k, v);
  71. });
  72. }
  73. r.write(strData);
  74. var rp = await r.close();
  75. var body = await rp.transform(utf8.decoder).join();
  76. print('${rp.statusCode} - $path');
  77. print('-- request --');
  78. print(strData);
  79. print('-- response --');
  80. print('$body \n');
  81. if (rp.statusCode == 404) {
  82. if (fail != null) fail('404 not found');
  83. } else {
  84. Map<String, dynamic> base = jsonDecode(body);
  85. if (rp.statusCode == 200) {
  86. if (base['code'] != 0) {
  87. if (fail != null) fail(base['desc']);
  88. } else {
  89. if (ok != null) ok(base['data']);
  90. }
  91. } else if (base['code'] != 0) {
  92. if (fail != null) fail(base['desc']);
  93. }
  94. }
  95. } catch (e) {
  96. if (fail != null) fail(e.toString());
  97. }
  98. if (eventually != null) eventually();
  99. }
  100. `
  101. apiFileContentV2 = `import 'dart:io';
  102. import 'dart:convert';
  103. import '../vars/kv.dart';
  104. import '../vars/vars.dart';
  105. /// send request with post method
  106. ///
  107. /// data: any request class that will be converted to json automatically
  108. /// ok: is called when request succeeds
  109. /// fail: is called when request fails
  110. /// eventually: is always called until the nearby functions returns
  111. Future apiPost(String path, dynamic data,
  112. {Map<String, String>? header,
  113. Function(Map<String, dynamic>)? ok,
  114. Function(String)? fail,
  115. Function? eventually}) async {
  116. await _apiRequest('POST', path, data,
  117. header: header, ok: ok, fail: fail, eventually: eventually);
  118. }
  119. /// send request with get method
  120. ///
  121. /// ok: is called when request succeeds
  122. /// fail: is called when request fails
  123. /// eventually: is always called until the nearby functions returns
  124. Future apiGet(String path,
  125. {Map<String, String>? header,
  126. Function(Map<String, dynamic>)? ok,
  127. Function(String)? fail,
  128. Function? eventually}) async {
  129. await _apiRequest('GET', path, null,
  130. header: header, ok: ok, fail: fail, eventually: eventually);
  131. }
  132. Future _apiRequest(String method, String path, dynamic data,
  133. {Map<String, String>? header,
  134. Function(Map<String, dynamic>)? ok,
  135. Function(String)? fail,
  136. Function? eventually}) async {
  137. var tokens = await getTokens();
  138. try {
  139. var client = HttpClient();
  140. HttpClientRequest r;
  141. if (method == 'POST') {
  142. r = await client.postUrl(Uri.parse(serverHost + path));
  143. } else {
  144. r = await client.getUrl(Uri.parse(serverHost + path));
  145. }
  146. var strData = '';
  147. if (data != null) {
  148. strData = jsonEncode(data);
  149. }
  150. if (method == 'POST') {
  151. r.headers.set('Content-Type', 'application/json; charset=utf-8');
  152. r.headers.set('Content-Length', utf8.encode(strData).length);
  153. }
  154. if (tokens != null) {
  155. r.headers.set('Authorization', tokens.accessToken);
  156. }
  157. if (header != null) {
  158. header.forEach((k, v) {
  159. r.headers.set(k, v);
  160. });
  161. }
  162. r.write(strData);
  163. var rp = await r.close();
  164. var body = await rp.transform(utf8.decoder).join();
  165. print('${rp.statusCode} - $path');
  166. print('-- request --');
  167. print(strData);
  168. print('-- response --');
  169. print('$body \n');
  170. if (rp.statusCode == 404) {
  171. if (fail != null) fail('404 not found');
  172. } else {
  173. Map<String, dynamic> base = jsonDecode(body);
  174. if (rp.statusCode == 200) {
  175. if (base['code'] != 0) {
  176. if (fail != null) fail(base['desc']);
  177. } else {
  178. if (ok != null) ok(base['data']);
  179. }
  180. } else if (base['code'] != 0) {
  181. if (fail != null) fail(base['desc']);
  182. }
  183. }
  184. } catch (e) {
  185. if (fail != null) fail(e.toString());
  186. }
  187. if (eventually != null) eventually();
  188. }`
  189. tokensFileContent = `class Tokens {
  190. /// 用于访问的token, 每次请求都必须带在Header里面
  191. final String accessToken;
  192. final int accessExpire;
  193. /// 用于刷新token
  194. final String refreshToken;
  195. final int refreshExpire;
  196. final int refreshAfter;
  197. Tokens(
  198. {this.accessToken,
  199. this.accessExpire,
  200. this.refreshToken,
  201. this.refreshExpire,
  202. this.refreshAfter});
  203. factory Tokens.fromJson(Map<String, dynamic> m) {
  204. return Tokens(
  205. accessToken: m['access_token'],
  206. accessExpire: m['access_expire'],
  207. refreshToken: m['refresh_token'],
  208. refreshExpire: m['refresh_expire'],
  209. refreshAfter: m['refresh_after']);
  210. }
  211. Map<String, dynamic> toJson() {
  212. return {
  213. 'access_token': accessToken,
  214. 'access_expire': accessExpire,
  215. 'refresh_token': refreshToken,
  216. 'refresh_expire': refreshExpire,
  217. 'refresh_after': refreshAfter,
  218. };
  219. }
  220. }
  221. `
  222. tokensFileContentV2 = `class Tokens {
  223. /// 用于访问的token, 每次请求都必须带在Header里面
  224. final String accessToken;
  225. final int accessExpire;
  226. /// 用于刷新token
  227. final String refreshToken;
  228. final int refreshExpire;
  229. final int refreshAfter;
  230. Tokens({
  231. required this.accessToken,
  232. required this.accessExpire,
  233. required this.refreshToken,
  234. required this.refreshExpire,
  235. required this.refreshAfter
  236. });
  237. factory Tokens.fromJson(Map<String, dynamic> m) {
  238. return Tokens(
  239. accessToken: m['access_token'],
  240. accessExpire: m['access_expire'],
  241. refreshToken: m['refresh_token'],
  242. refreshExpire: m['refresh_expire'],
  243. refreshAfter: m['refresh_after']);
  244. }
  245. Map<String, dynamic> toJson() {
  246. return {
  247. 'access_token': accessToken,
  248. 'access_expire': accessExpire,
  249. 'refresh_token': refreshToken,
  250. 'refresh_expire': refreshExpire,
  251. 'refresh_after': refreshAfter,
  252. };
  253. }
  254. }
  255. `
  256. )