vars.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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('https://' + serverHost + path));
  54. } else {
  55. r = await client.getUrl(Uri.parse('https://' + serverHost + path));
  56. }
  57. r.headers.set('Content-Type', 'application/json; charset=utf-8');
  58. if (tokens != null) {
  59. r.headers.set('Authorization', tokens.accessToken);
  60. }
  61. if (header != null) {
  62. header.forEach((k, v) {
  63. r.headers.set(k, v);
  64. });
  65. }
  66. var strData = '';
  67. if (data != null) {
  68. strData = jsonEncode(data);
  69. }
  70. r.write(strData);
  71. var rp = await r.close();
  72. var body = await rp.transform(utf8.decoder).join();
  73. print('${rp.statusCode} - $path');
  74. print('-- request --');
  75. print(strData);
  76. print('-- response --');
  77. print('$body \n');
  78. if (rp.statusCode == 404) {
  79. if (fail != null) fail('404 not found');
  80. } else {
  81. Map<String, dynamic> base = jsonDecode(body);
  82. if (rp.statusCode == 200) {
  83. if (base['code'] != 0) {
  84. if (fail != null) fail(base['desc']);
  85. } else {
  86. if (ok != null) ok(base['data']);
  87. }
  88. } else if (base['code'] != 0) {
  89. if (fail != null) fail(base['desc']);
  90. }
  91. }
  92. } catch (e) {
  93. if (fail != null) fail(e.toString());
  94. }
  95. if (eventually != null) eventually();
  96. }
  97. `
  98. apiFileContentV2 = `import 'dart:io';
  99. import 'dart:convert';
  100. import '../vars/kv.dart';
  101. import '../vars/vars.dart';
  102. /// send request with post method
  103. ///
  104. /// data: any request class that will be converted to json automatically
  105. /// ok: is called when request succeeds
  106. /// fail: is called when request fails
  107. /// eventually: is always called until the nearby functions returns
  108. Future apiPost(String path, dynamic data,
  109. {Map<String, String>? header,
  110. Function(Map<String, dynamic>)? ok,
  111. Function(String)? fail,
  112. Function? eventually}) async {
  113. await _apiRequest('POST', path, data,
  114. header: header, ok: ok, fail: fail, eventually: eventually);
  115. }
  116. /// send request with get method
  117. ///
  118. /// ok: is called when request succeeds
  119. /// fail: is called when request fails
  120. /// eventually: is always called until the nearby functions returns
  121. Future apiGet(String path,
  122. {Map<String, String>? header,
  123. Function(Map<String, dynamic>)? ok,
  124. Function(String)? fail,
  125. Function? eventually}) async {
  126. await _apiRequest('GET', path, null,
  127. header: header, ok: ok, fail: fail, eventually: eventually);
  128. }
  129. Future _apiRequest(String method, String path, dynamic data,
  130. {Map<String, String>? header,
  131. Function(Map<String, dynamic>)? ok,
  132. Function(String)? fail,
  133. Function? eventually}) async {
  134. var tokens = await getTokens();
  135. try {
  136. var client = HttpClient();
  137. HttpClientRequest r;
  138. if (method == 'POST') {
  139. r = await client.postUrl(Uri.parse('https://' + serverHost + path));
  140. } else {
  141. r = await client.getUrl(Uri.parse('https://' + serverHost + path));
  142. }
  143. r.headers.set('Content-Type', 'application/json; charset=utf-8');
  144. if (tokens != null) {
  145. r.headers.set('Authorization', tokens.accessToken);
  146. }
  147. if (header != null) {
  148. header.forEach((k, v) {
  149. r.headers.set(k, v);
  150. });
  151. }
  152. var strData = '';
  153. if (data != null) {
  154. strData = jsonEncode(data);
  155. }
  156. r.write(strData);
  157. var rp = await r.close();
  158. var body = await rp.transform(utf8.decoder).join();
  159. print('${rp.statusCode} - $path');
  160. print('-- request --');
  161. print(strData);
  162. print('-- response --');
  163. print('$body \n');
  164. if (rp.statusCode == 404) {
  165. if (fail != null) fail('404 not found');
  166. } else {
  167. Map<String, dynamic> base = jsonDecode(body);
  168. if (rp.statusCode == 200) {
  169. if (base['code'] != 0) {
  170. if (fail != null) fail(base['desc']);
  171. } else {
  172. if (ok != null) ok(base['data']);
  173. }
  174. } else if (base['code'] != 0) {
  175. if (fail != null) fail(base['desc']);
  176. }
  177. }
  178. } catch (e) {
  179. if (fail != null) fail(e.toString());
  180. }
  181. if (eventually != null) eventually();
  182. }`
  183. tokensFileContent = `class Tokens {
  184. /// 用于访问的token, 每次请求都必须带在Header里面
  185. final String accessToken;
  186. final int accessExpire;
  187. /// 用于刷新token
  188. final String refreshToken;
  189. final int refreshExpire;
  190. final int refreshAfter;
  191. Tokens(
  192. {this.accessToken,
  193. this.accessExpire,
  194. this.refreshToken,
  195. this.refreshExpire,
  196. this.refreshAfter});
  197. factory Tokens.fromJson(Map<String, dynamic> m) {
  198. return Tokens(
  199. accessToken: m['access_token'],
  200. accessExpire: m['access_expire'],
  201. refreshToken: m['refresh_token'],
  202. refreshExpire: m['refresh_expire'],
  203. refreshAfter: m['refresh_after']);
  204. }
  205. Map<String, dynamic> toJson() {
  206. return {
  207. 'access_token': accessToken,
  208. 'access_expire': accessExpire,
  209. 'refresh_token': refreshToken,
  210. 'refresh_expire': refreshExpire,
  211. 'refresh_after': refreshAfter,
  212. };
  213. }
  214. }
  215. `
  216. tokensFileContentV2 = `class Tokens {
  217. /// 用于访问的token, 每次请求都必须带在Header里面
  218. final String accessToken;
  219. final int accessExpire;
  220. /// 用于刷新token
  221. final String refreshToken;
  222. final int refreshExpire;
  223. final int refreshAfter;
  224. Tokens({
  225. required this.accessToken,
  226. required this.accessExpire,
  227. required this.refreshToken,
  228. required this.refreshExpire,
  229. required this.refreshAfter
  230. });
  231. factory Tokens.fromJson(Map<String, dynamic> m) {
  232. return Tokens(
  233. accessToken: m['access_token'],
  234. accessExpire: m['access_expire'],
  235. refreshToken: m['refresh_token'],
  236. refreshExpire: m['refresh_expire'],
  237. refreshAfter: m['refresh_after']);
  238. }
  239. Map<String, dynamic> toJson() {
  240. return {
  241. 'access_token': accessToken,
  242. 'access_expire': accessExpire,
  243. 'refresh_token': refreshToken,
  244. 'refresh_expire': refreshExpire,
  245. 'refresh_after': refreshAfter,
  246. };
  247. }
  248. }
  249. `
  250. )