vars.go 8.5 KB

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