vars.go 8.1 KB

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