mirror of
https://github.com/foss42/apidash.git
synced 2025-05-24 17:56:44 +08:00
Merge pull request #289 from nikkivirtuoso/updating_dart_dio
fix: testcases and dart dio codegen
This commit is contained in:
@ -55,12 +55,17 @@ class DartDioCodeGen {
|
|||||||
final List<Map<String,String>> formDataList = ${json.encode(formData)};
|
final List<Map<String,String>> formDataList = ${json.encode(formData)};
|
||||||
for (var formField in formDataList) {
|
for (var formField in formDataList) {
|
||||||
if (formField['type'] == 'file') {
|
if (formField['type'] == 'file') {
|
||||||
formData.files.add(MapEntry(
|
if (formField['value'] != null) {
|
||||||
formField['name'],
|
data.files.add(MapEntry(
|
||||||
await MultipartFile.fromFile(formField['value'], filename: formField['value']),
|
formField['name']!,
|
||||||
|
await dio.MultipartFile.fromFile(formField['value']!,
|
||||||
|
filename: formField['value']!),
|
||||||
));
|
));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
formData.fields.add(MapEntry(formField['name'], formField['value']));
|
if (formField['value'] != null) {
|
||||||
|
data.fields.add(MapEntry(formField['name']!, formField['value']!));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
''');
|
''');
|
||||||
@ -79,16 +84,16 @@ class DartDioCodeGen {
|
|||||||
dataExp = declareFinal('data').assign(strContent);
|
dataExp = declareFinal('data').assign(strContent);
|
||||||
// when add new type of [ContentType], need update [dataExp].
|
// when add new type of [ContentType], need update [dataExp].
|
||||||
case ContentType.formdata:
|
case ContentType.formdata:
|
||||||
dataExp = declareFinal('data').assign(refer('FormData()'));
|
dataExp = declareFinal('data').assign(refer('dio.FormData()'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final responseExp = declareFinal('response').assign(InvokeExpression.newOf(
|
final responseExp = declareFinal('response').assign(InvokeExpression.newOf(
|
||||||
refer('dio.Dio'),
|
refer('dio.Dio()'),
|
||||||
[literalString(url)],
|
[literalString(url)],
|
||||||
{
|
{
|
||||||
if (queryParamExp != null) 'queryParameters': refer('queryParams'),
|
if (queryParamExp != null) 'queryParameters': refer('queryParams'),
|
||||||
if (headerExp != null)
|
if (headerExp != null)
|
||||||
'options': refer('Options').newInstance(
|
'options': refer('dio.Options').newInstance(
|
||||||
[],
|
[],
|
||||||
{'headers': refer('headers')},
|
{'headers': refer('headers')},
|
||||||
),
|
),
|
||||||
@ -112,7 +117,7 @@ class DartDioCodeGen {
|
|||||||
refer('print').call([refer('response').property('data')]),
|
refer('print').call([refer('response').property('data')]),
|
||||||
],
|
],
|
||||||
onError: {
|
onError: {
|
||||||
'DioException': [
|
'dio.DioException': [
|
||||||
refer('print').call([
|
refer('print').call([
|
||||||
refer('e').property('response').nullSafeProperty('statusCode'),
|
refer('e').property('response').nullSafeProperty('statusCode'),
|
||||||
]),
|
]),
|
||||||
|
@ -12,10 +12,10 @@ void main() {
|
|||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
try {
|
try {
|
||||||
final response = await dio.Dio.get('https://api.apidash.dev');
|
final response = await dio.Dio().get('https://api.apidash.dev');
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -36,13 +36,13 @@ void main() async {
|
|||||||
void main() async {
|
void main() async {
|
||||||
try {
|
try {
|
||||||
final queryParams = {'code': 'US'};
|
final queryParams = {'code': 'US'};
|
||||||
final response = await dio.Dio.get(
|
final response = await dio.Dio().get(
|
||||||
'https://api.apidash.dev/country/data',
|
'https://api.apidash.dev/country/data',
|
||||||
queryParameters: queryParams,
|
queryParameters: queryParams,
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -63,13 +63,13 @@ void main() async {
|
|||||||
void main() async {
|
void main() async {
|
||||||
try {
|
try {
|
||||||
final queryParams = {'code': 'IND'};
|
final queryParams = {'code': 'IND'};
|
||||||
final response = await dio.Dio.get(
|
final response = await dio.Dio().get(
|
||||||
'https://api.apidash.dev/country/data?code=US',
|
'https://api.apidash.dev/country/data?code=US',
|
||||||
queryParameters: queryParams,
|
queryParameters: queryParams,
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -96,13 +96,13 @@ void main() async {
|
|||||||
'add_space': 'true',
|
'add_space': 'true',
|
||||||
'trailing_zeros': 'true',
|
'trailing_zeros': 'true',
|
||||||
};
|
};
|
||||||
final response = await dio.Dio.get(
|
final response = await dio.Dio().get(
|
||||||
'https://api.apidash.dev/humanize/social',
|
'https://api.apidash.dev/humanize/social',
|
||||||
queryParameters: queryParams,
|
queryParameters: queryParams,
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -123,13 +123,13 @@ void main() async {
|
|||||||
void main() async {
|
void main() async {
|
||||||
try {
|
try {
|
||||||
final headers = {'User-Agent': 'Test Agent'};
|
final headers = {'User-Agent': 'Test Agent'};
|
||||||
final response = await dio.Dio.get(
|
final response = await dio.Dio().get(
|
||||||
'https://api.github.com/repos/foss42/apidash',
|
'https://api.github.com/repos/foss42/apidash',
|
||||||
options: Options(headers: headers),
|
options: dio.Options(headers: headers),
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -151,14 +151,14 @@ void main() async {
|
|||||||
try {
|
try {
|
||||||
final queryParams = {'raw': 'true'};
|
final queryParams = {'raw': 'true'};
|
||||||
final headers = {'User-Agent': 'Test Agent'};
|
final headers = {'User-Agent': 'Test Agent'};
|
||||||
final response = await dio.Dio.get(
|
final response = await dio.Dio().get(
|
||||||
'https://api.github.com/repos/foss42/apidash',
|
'https://api.github.com/repos/foss42/apidash',
|
||||||
queryParameters: queryParams,
|
queryParameters: queryParams,
|
||||||
options: Options(headers: headers),
|
options: dio.Options(headers: headers),
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -178,10 +178,10 @@ void main() async {
|
|||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
try {
|
try {
|
||||||
final response = await dio.Dio.get('https://api.apidash.dev');
|
final response = await dio.Dio().get('https://api.apidash.dev');
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -203,14 +203,14 @@ void main() async {
|
|||||||
try {
|
try {
|
||||||
final queryParams = {'raw': 'true'};
|
final queryParams = {'raw': 'true'};
|
||||||
final headers = {'User-Agent': 'Test Agent'};
|
final headers = {'User-Agent': 'Test Agent'};
|
||||||
final response = await dio.Dio.get(
|
final response = await dio.Dio().get(
|
||||||
'https://api.github.com/repos/foss42/apidash',
|
'https://api.github.com/repos/foss42/apidash',
|
||||||
queryParameters: queryParams,
|
queryParameters: queryParams,
|
||||||
options: Options(headers: headers),
|
options: dio.Options(headers: headers),
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -234,13 +234,13 @@ void main() async {
|
|||||||
'num': '8700000',
|
'num': '8700000',
|
||||||
'add_space': 'true',
|
'add_space': 'true',
|
||||||
};
|
};
|
||||||
final response = await dio.Dio.get(
|
final response = await dio.Dio().get(
|
||||||
'https://api.apidash.dev/humanize/social',
|
'https://api.apidash.dev/humanize/social',
|
||||||
queryParameters: queryParams,
|
queryParameters: queryParams,
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -261,13 +261,13 @@ void main() async {
|
|||||||
void main() async {
|
void main() async {
|
||||||
try {
|
try {
|
||||||
final headers = {'User-Agent': 'Test Agent'};
|
final headers = {'User-Agent': 'Test Agent'};
|
||||||
final response = await dio.Dio.get(
|
final response = await dio.Dio().get(
|
||||||
'https://api.apidash.dev/humanize/social',
|
'https://api.apidash.dev/humanize/social',
|
||||||
options: Options(headers: headers),
|
options: dio.Options(headers: headers),
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -296,14 +296,14 @@ void main() async {
|
|||||||
'digits': '3',
|
'digits': '3',
|
||||||
};
|
};
|
||||||
final headers = {'User-Agent': 'Test Agent'};
|
final headers = {'User-Agent': 'Test Agent'};
|
||||||
final response = await dio.Dio.get(
|
final response = await dio.Dio().get(
|
||||||
'https://api.apidash.dev/humanize/social',
|
'https://api.apidash.dev/humanize/social',
|
||||||
queryParameters: queryParams,
|
queryParameters: queryParams,
|
||||||
options: Options(headers: headers),
|
options: dio.Options(headers: headers),
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -323,10 +323,10 @@ void main() async {
|
|||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
try {
|
try {
|
||||||
final response = await dio.Dio.get('https://api.apidash.dev/humanize/social');
|
final response = await dio.Dio().get('https://api.apidash.dev/humanize/social');
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -348,10 +348,10 @@ void main() async {
|
|||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
try {
|
try {
|
||||||
final response = await dio.Dio.head('https://api.apidash.dev');
|
final response = await dio.Dio().head('https://api.apidash.dev');
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -371,10 +371,10 @@ void main() async {
|
|||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
try {
|
try {
|
||||||
final response = await dio.Dio.head('http://api.apidash.dev');
|
final response = await dio.Dio().head('http://api.apidash.dev');
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -399,13 +399,13 @@ void main() async {
|
|||||||
final data = r'''{
|
final data = r'''{
|
||||||
"text": "I LOVE Flutter"
|
"text": "I LOVE Flutter"
|
||||||
}''';
|
}''';
|
||||||
final response = await dio.Dio.post(
|
final response = await dio.Dio().post(
|
||||||
'https://api.apidash.dev/case/lower',
|
'https://api.apidash.dev/case/lower',
|
||||||
data: data,
|
data: data,
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -434,13 +434,13 @@ void main() async {
|
|||||||
"no": 1.2,
|
"no": 1.2,
|
||||||
"arr": ["null", "true", "false", null]
|
"arr": ["null", "true", "false", null]
|
||||||
}''');
|
}''');
|
||||||
final response = await dio.Dio.post(
|
final response = await dio.Dio().post(
|
||||||
'https://api.apidash.dev/case/lower',
|
'https://api.apidash.dev/case/lower',
|
||||||
data: data,
|
data: data,
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -465,14 +465,14 @@ void main() async {
|
|||||||
final data = convert.json.decode(r'''{
|
final data = convert.json.decode(r'''{
|
||||||
"text": "I LOVE Flutter"
|
"text": "I LOVE Flutter"
|
||||||
}''');
|
}''');
|
||||||
final response = await dio.Dio.post(
|
final response = await dio.Dio().post(
|
||||||
'https://api.apidash.dev/case/lower',
|
'https://api.apidash.dev/case/lower',
|
||||||
options: Options(headers: headers),
|
options: dio.Options(headers: headers),
|
||||||
data: data,
|
data: data,
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -486,8 +486,326 @@ void main() async {
|
|||||||
codeGen.getCode(CodegenLanguage.dartDio, requestModelPost3, "https"),
|
codeGen.getCode(CodegenLanguage.dartDio, requestModelPost3, "https"),
|
||||||
expectedCode);
|
expectedCode);
|
||||||
});
|
});
|
||||||
|
test('POST 4', () {
|
||||||
|
const expectedCode = r"""import 'package:dio/dio.dart' as dio;
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
try {
|
||||||
|
final data = dio.FormData();
|
||||||
|
final List<Map<String, String>> formDataList = [
|
||||||
|
{"name": "text", "value": "API", "type": "text"},
|
||||||
|
{"name": "sep", "value": "|", "type": "text"},
|
||||||
|
{"name": "times", "value": "3", "type": "text"}
|
||||||
|
];
|
||||||
|
for (var formField in formDataList) {
|
||||||
|
if (formField['type'] == 'file') {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.files.add(MapEntry(
|
||||||
|
formField['name']!,
|
||||||
|
await dio.MultipartFile.fromFile(formField['value']!,
|
||||||
|
filename: formField['value']!),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.fields
|
||||||
|
.add(MapEntry(formField['name']!, formField['value']!));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final response = await dio.Dio().post(
|
||||||
|
'https://api.apidash.dev/io/form',
|
||||||
|
data: data,
|
||||||
|
);
|
||||||
|
print(response.statusCode);
|
||||||
|
print(response.data);
|
||||||
|
} on dio.DioException catch (e, s) {
|
||||||
|
print(e.response?.statusCode);
|
||||||
|
print(e.response?.data);
|
||||||
|
print(s);
|
||||||
|
} catch (e, s) {
|
||||||
|
print(e);
|
||||||
|
print(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(CodegenLanguage.dartDio, requestModelPost4, "https"),
|
||||||
|
expectedCode);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('POST 5', () {
|
||||||
|
const expectedCode = r"""import 'package:dio/dio.dart' as dio;
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
try {
|
||||||
|
final headers = {'User-Agent': 'Test Agent'};
|
||||||
|
final data = dio.FormData();
|
||||||
|
final List<Map<String, String>> formDataList = [
|
||||||
|
{"name": "text", "value": "API", "type": "text"},
|
||||||
|
{"name": "sep", "value": "|", "type": "text"},
|
||||||
|
{"name": "times", "value": "3", "type": "text"}
|
||||||
|
];
|
||||||
|
for (var formField in formDataList) {
|
||||||
|
if (formField['type'] == 'file') {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.files.add(MapEntry(
|
||||||
|
formField['name']!,
|
||||||
|
await dio.MultipartFile.fromFile(formField['value']!,
|
||||||
|
filename: formField['value']!),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.fields
|
||||||
|
.add(MapEntry(formField['name']!, formField['value']!));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final response = await dio.Dio().post(
|
||||||
|
'https://api.apidash.dev/io/form',
|
||||||
|
options: dio.Options(headers: headers),
|
||||||
|
data: data,
|
||||||
|
);
|
||||||
|
print(response.statusCode);
|
||||||
|
print(response.data);
|
||||||
|
} on dio.DioException catch (e, s) {
|
||||||
|
print(e.response?.statusCode);
|
||||||
|
print(e.response?.data);
|
||||||
|
print(s);
|
||||||
|
} catch (e, s) {
|
||||||
|
print(e);
|
||||||
|
print(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(CodegenLanguage.dartDio, requestModelPost5, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('POST 6', () {
|
||||||
|
const expectedCode = r"""import 'package:dio/dio.dart' as dio;
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
try {
|
||||||
|
final data = dio.FormData();
|
||||||
|
final List<Map<String, String>> formDataList = [
|
||||||
|
{"name": "token", "value": "xyz", "type": "text"},
|
||||||
|
{
|
||||||
|
"name": "imfile",
|
||||||
|
"value": "/Documents/up/1.png",
|
||||||
|
"type": "file"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
for (var formField in formDataList) {
|
||||||
|
if (formField['type'] == 'file') {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.files.add(MapEntry(
|
||||||
|
formField['name']!,
|
||||||
|
await dio.MultipartFile.fromFile(formField['value']!,
|
||||||
|
filename: formField['value']!),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.fields
|
||||||
|
.add(MapEntry(formField['name']!, formField['value']!));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final response = await dio.Dio().post(
|
||||||
|
'https://api.apidash.dev/io/img',
|
||||||
|
data: data,
|
||||||
|
);
|
||||||
|
print(response.statusCode);
|
||||||
|
print(response.data);
|
||||||
|
} on dio.DioException catch (e, s) {
|
||||||
|
print(e.response?.statusCode);
|
||||||
|
print(e.response?.data);
|
||||||
|
print(s);
|
||||||
|
} catch (e, s) {
|
||||||
|
print(e);
|
||||||
|
print(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(CodegenLanguage.dartDio, requestModelPost6, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('POST 7', () {
|
||||||
|
const expectedCode = r"""import 'package:dio/dio.dart' as dio;
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
try {
|
||||||
|
final data = dio.FormData();
|
||||||
|
final List<Map<String, String>> formDataList = [
|
||||||
|
{"name": "token", "value": "xyz", "type": "text"},
|
||||||
|
{
|
||||||
|
"name": "imfile",
|
||||||
|
"value": "/Documents/up/1.png",
|
||||||
|
"type": "file"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
for (var formField in formDataList) {
|
||||||
|
if (formField['type'] == 'file') {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.files.add(MapEntry(
|
||||||
|
formField['name']!,
|
||||||
|
await dio.MultipartFile.fromFile(formField['value']!,
|
||||||
|
filename: formField['value']!),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.fields
|
||||||
|
.add(MapEntry(formField['name']!, formField['value']!));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final response = await dio.Dio().post(
|
||||||
|
'https://api.apidash.dev/io/img',
|
||||||
|
data: data,
|
||||||
|
);
|
||||||
|
print(response.statusCode);
|
||||||
|
print(response.data);
|
||||||
|
} on dio.DioException catch (e, s) {
|
||||||
|
print(e.response?.statusCode);
|
||||||
|
print(e.response?.data);
|
||||||
|
print(s);
|
||||||
|
} catch (e, s) {
|
||||||
|
print(e);
|
||||||
|
print(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(CodegenLanguage.dartDio, requestModelPost7, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('POST 8', () {
|
||||||
|
const expectedCode = r"""import 'package:dio/dio.dart' as dio;
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
try {
|
||||||
|
final queryParams = {
|
||||||
|
'size': '2',
|
||||||
|
'len': '3',
|
||||||
|
};
|
||||||
|
final data = dio.FormData();
|
||||||
|
final List<Map<String, String>> formDataList = [
|
||||||
|
{"name": "text", "value": "API", "type": "text"},
|
||||||
|
{"name": "sep", "value": "|", "type": "text"},
|
||||||
|
{"name": "times", "value": "3", "type": "text"}
|
||||||
|
];
|
||||||
|
for (var formField in formDataList) {
|
||||||
|
if (formField['type'] == 'file') {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.files.add(MapEntry(
|
||||||
|
formField['name']!,
|
||||||
|
await dio.MultipartFile.fromFile(formField['value']!,
|
||||||
|
filename: formField['value']!),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.fields
|
||||||
|
.add(MapEntry(formField['name']!, formField['value']!));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final response = await dio.Dio().post(
|
||||||
|
'https://api.apidash.dev/io/form',
|
||||||
|
queryParameters: queryParams,
|
||||||
|
data: data,
|
||||||
|
);
|
||||||
|
print(response.statusCode);
|
||||||
|
print(response.data);
|
||||||
|
} on dio.DioException catch (e, s) {
|
||||||
|
print(e.response?.statusCode);
|
||||||
|
print(e.response?.data);
|
||||||
|
print(s);
|
||||||
|
} catch (e, s) {
|
||||||
|
print(e);
|
||||||
|
print(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(CodegenLanguage.dartDio, requestModelPost8, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('POST 9', () {
|
||||||
|
const expectedCode = r"""import 'package:dio/dio.dart' as dio;
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
try {
|
||||||
|
final queryParams = {
|
||||||
|
'size': '2',
|
||||||
|
'len': '3',
|
||||||
|
};
|
||||||
|
final headers = {
|
||||||
|
'User-Agent': 'Test Agent',
|
||||||
|
'Keep-Alive': 'true',
|
||||||
|
};
|
||||||
|
final data = dio.FormData();
|
||||||
|
final List<Map<String, String>> formDataList = [
|
||||||
|
{"name": "token", "value": "xyz", "type": "text"},
|
||||||
|
{
|
||||||
|
"name": "imfile",
|
||||||
|
"value": "/Documents/up/1.png",
|
||||||
|
"type": "file"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
for (var formField in formDataList) {
|
||||||
|
if (formField['type'] == 'file') {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.files.add(MapEntry(
|
||||||
|
formField['name']!,
|
||||||
|
await dio.MultipartFile.fromFile(formField['value']!,
|
||||||
|
filename: formField['value']!),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (formField['value'] != null) {
|
||||||
|
data.fields
|
||||||
|
.add(MapEntry(formField['name']!, formField['value']!));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final response = await dio.Dio().post(
|
||||||
|
'https://api.apidash.dev/io/img',
|
||||||
|
queryParameters: queryParams,
|
||||||
|
options: dio.Options(headers: headers),
|
||||||
|
data: data,
|
||||||
|
);
|
||||||
|
print(response.statusCode);
|
||||||
|
print(response.data);
|
||||||
|
} on dio.DioException catch (e, s) {
|
||||||
|
print(e.response?.statusCode);
|
||||||
|
print(e.response?.data);
|
||||||
|
print(s);
|
||||||
|
} catch (e, s) {
|
||||||
|
print(e);
|
||||||
|
print(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
expect(
|
||||||
|
codeGen.getCode(CodegenLanguage.dartDio, requestModelPost9, "https"),
|
||||||
|
expectedCode);
|
||||||
|
});
|
||||||
|
});
|
||||||
group('PUT Request', () {
|
group('PUT Request', () {
|
||||||
test('PUT 1', () {
|
test('PUT 1', () {
|
||||||
const expectedCode = r"""import 'package:dio/dio.dart' as dio;
|
const expectedCode = r"""import 'package:dio/dio.dart' as dio;
|
||||||
@ -499,13 +817,13 @@ void main() async {
|
|||||||
"name": "morpheus",
|
"name": "morpheus",
|
||||||
"job": "zion resident"
|
"job": "zion resident"
|
||||||
}''');
|
}''');
|
||||||
final response = await dio.Dio.put(
|
final response = await dio.Dio().put(
|
||||||
'https://reqres.in/api/users/2',
|
'https://reqres.in/api/users/2',
|
||||||
data: data,
|
data: data,
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -532,13 +850,13 @@ void main() async {
|
|||||||
"name": "marfeus",
|
"name": "marfeus",
|
||||||
"job": "accountant"
|
"job": "accountant"
|
||||||
}''');
|
}''');
|
||||||
final response = await dio.Dio.patch(
|
final response = await dio.Dio().patch(
|
||||||
'https://reqres.in/api/users/2',
|
'https://reqres.in/api/users/2',
|
||||||
data: data,
|
data: data,
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -560,10 +878,10 @@ void main() async {
|
|||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
try {
|
try {
|
||||||
final response = await dio.Dio.delete('https://reqres.in/api/users/2');
|
final response = await dio.Dio().delete('https://reqres.in/api/users/2');
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
@ -589,13 +907,13 @@ void main() async {
|
|||||||
"name": "marfeus",
|
"name": "marfeus",
|
||||||
"job": "accountant"
|
"job": "accountant"
|
||||||
}''');
|
}''');
|
||||||
final response = await dio.Dio.delete(
|
final response = await dio.Dio().delete(
|
||||||
'https://reqres.in/api/users/2',
|
'https://reqres.in/api/users/2',
|
||||||
data: data,
|
data: data,
|
||||||
);
|
);
|
||||||
print(response.statusCode);
|
print(response.statusCode);
|
||||||
print(response.data);
|
print(response.data);
|
||||||
} on DioException catch (e, s) {
|
} on dio.DioException catch (e, s) {
|
||||||
print(e.response?.statusCode);
|
print(e.response?.statusCode);
|
||||||
print(e.response?.data);
|
print(e.response?.data);
|
||||||
print(s);
|
print(s);
|
||||||
|
Reference in New Issue
Block a user