1+ /**
2+ * @description
3+ * HTTP code snippet generator for Dart http package.
4+ *
5+ * @author
6+ * @AI -Generated
7+ *
8+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+ */
10+
11+ import { CodeBuilder } from '../../../helpers/code-builder' ;
12+ import { escapeForSingleQuotes } from '../../../helpers/escape' ;
13+ import { Client } from '../../targets' ;
14+
15+ export interface DartHttpOptions {
16+ showBoilerplate ?: boolean ;
17+ checkErrors ?: boolean ;
18+ printBody ?: boolean ;
19+ timeout ?: number ;
20+ insecureSkipVerify ?: boolean ;
21+ }
22+
23+ export const http : Client < DartHttpOptions > = {
24+ info : {
25+ key : 'http' ,
26+ title : 'HTTP' ,
27+ link : 'https://pub.dev/packages/http' ,
28+ description : 'Dart HTTP client request using the http package' ,
29+ } ,
30+ convert : ( { postData, method, allHeaders, fullUrl } , options = { } ) => {
31+ const { blank, push, join } = new CodeBuilder ( { indent : ' ' } ) ;
32+
33+ const {
34+ showBoilerplate = true ,
35+ checkErrors = false ,
36+ printBody = true ,
37+ timeout = - 1 ,
38+ insecureSkipVerify = false ,
39+ } = options ;
40+
41+ const indent = showBoilerplate ? 1 : 0 ;
42+
43+ // Create boilerplate
44+ if ( showBoilerplate ) {
45+ push ( 'import \'package:http/http.dart\' as http;' ) ;
46+
47+ blank ( ) ;
48+ push ( 'void main() async {' ) ;
49+ blank ( ) ;
50+ }
51+
52+ // Create client with timeout if specified
53+ if ( timeout > 0 ) {
54+ push ( 'final client = http.Client();' , indent ) ;
55+ push ( `client.timeout = Duration(seconds: ${ timeout } );` , indent ) ;
56+ blank ( ) ;
57+ }
58+
59+ // Add headers setup
60+ if ( Object . keys ( allHeaders ) . length ) {
61+ push ( 'final headers = {' , indent ) ;
62+ Object . keys ( allHeaders ) . forEach ( key => {
63+ push ( `'${ key } ': '${ escapeForSingleQuotes ( allHeaders [ key ] ) } ',` , indent + 1 ) ;
64+ } ) ;
65+ push ( '};' , indent ) ;
66+ blank ( ) ;
67+ }
68+
69+ // Prepare request
70+ const headersVar = Object . keys ( allHeaders ) . length ? 'headers' : '{}' ;
71+
72+ if ( postData . text ) {
73+ push ( `final response = await http.${ method . toLowerCase ( ) } (` , indent ) ;
74+ push ( ` Uri.parse('${ fullUrl } '),` , indent ) ;
75+ push ( ` headers: ${ headersVar } ,` , indent ) ;
76+ push ( ` body: ${ JSON . stringify ( postData . text ) } ,` , indent ) ;
77+ push ( ');' , indent ) ;
78+ } else {
79+ push ( `final response = await http.${ method . toLowerCase ( ) } (` , indent ) ;
80+ push ( ` Uri.parse('${ fullUrl } '),` , indent ) ;
81+ push ( ` headers: ${ headersVar } ,` , indent ) ;
82+ push ( ');' , indent ) ;
83+ }
84+
85+ // Print response
86+ blank ( ) ;
87+ push ( 'print(response.statusCode);' , indent ) ;
88+
89+ if ( printBody ) {
90+ push ( 'print(response.body);' , indent ) ;
91+ }
92+
93+ // End main block
94+ if ( showBoilerplate ) {
95+ blank ( ) ;
96+ push ( '}' ) ;
97+ }
98+
99+ return join ( ) ;
100+ } ,
101+ } ;
0 commit comments