Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROPOSAL: Improve experience when retrying MultiPart request with Files #598

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ You should create a new class to encapsulate the response.
mapperVal = '''
(json)=> json is List<dynamic>
? json
.map<$genericTypeString>((i) =>
.map<$genericTypeString>((i) =>
i as $genericTypeString
)
.toList()
Expand Down Expand Up @@ -1258,13 +1258,13 @@ You should create a new class to encapsulate the response.
if (baseUrl == null || baseUrl.trim().isEmpty) {
return dioBaseUrl;
}

final url = Uri.parse(baseUrl);

if (url.isAbsolute) {
return url.toString();
}

return Uri.parse(dioBaseUrl).resolveUri(url).toString();
''');
});
Expand Down Expand Up @@ -1732,21 +1732,29 @@ ${bodyName.displayName} == null
final contentType = r.peek('contentType')?.stringValue;

if (isFileField) {
final keepFilePath = r.peek('keepFilePath')?.boolValue ?? false;
final fileNameValue = r.peek('fileName')?.stringValue;
final fileName = fileNameValue != null
? literalString(fileNameValue)
: refer(p.displayName)
.property('path.split(Platform.pathSeparator).last');
final filePath = refer(p.displayName).property('path');
final headers = keepFilePath
? literalMap({
'original_file_path': literalList([filePath])
})
: literalMap({});

final uploadFileInfo = refer('$MultipartFile.fromFileSync').call([
refer(p.displayName).property('path')
filePath
], {
'filename': fileName,
if (contentType != null)
'contentType':
refer('MediaType', 'package:http_parser/http_parser.dart')
.property('parse')
.call([literal(contentType)])
.call([literal(contentType)]),
'headers': headers,
});

final optionalFile = m.parameters
Expand Down Expand Up @@ -1789,7 +1797,7 @@ ${bodyName.displayName} == null
MultipartFile.fromBytes(${p.displayName},

filename:${literal(fileName)},
$conType
$conType,
))
''')
]).statement;
Expand Down Expand Up @@ -1823,7 +1831,7 @@ ${bodyName.displayName} == null
'$fieldName',
MultipartFile.fromBytes(i,
filename:${literal(fileName)},
$conType
$conType,
)))
''')
]).statement,
Expand Down Expand Up @@ -1851,6 +1859,13 @@ ${bodyName.displayName} == null
final conType = contentType == null
? ''
: 'contentType: MediaType.parse(${literal(contentType)}),';
final keepFilePath = r.peek('keepFilePath')?.boolValue ?? false;
final filePath = refer(p.displayName).property('path');
final headers = keepFilePath
? literalMap({
'original_file_path': [filePath]
})
: '{}';
if (p.type.isNullable) {
blocks.add(Code('if (${p.displayName} != null) {'));
}
Expand All @@ -1861,7 +1876,8 @@ ${bodyName.displayName} == null
'$fieldName',
MultipartFile.fromFileSync(i.path,
filename: i.path.split(Platform.pathSeparator).last,
$conType
$conType,
headers: $headers,
)))
''')
]).statement,
Expand Down
3 changes: 2 additions & 1 deletion generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ dependencies:
code_builder: ^4.4.0
dart_style: ^2.3.0
dio: ^5.0.0
retrofit: ^4.0.1
retrofit: ## update this dependency once a new version is released with keepOriginalPath change
path: ../retrofit
source_gen: ^1.3.0
tuple: ^2.0.1

Expand Down
17 changes: 14 additions & 3 deletions generator/test/src/generator_test_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ enum ImageType { icon, large }
MultipartFile.fromFileSync(
image.path,
filename: image.path.split(Platform.pathSeparator).last,
headers: {
'original_file_path': [image.path]
},
),
));
''',
Expand All @@ -271,7 +274,7 @@ enum ImageType { icon, large }
@RestApi(baseUrl: 'https://httpbin.org/')
abstract class FilePartTest {
@POST('/profile')
Future<String> setProfile(@Part() File image);
Future<String> setProfile(@Part(keepFilePath: true) File image);
}

@ShouldGenerate(
Expand All @@ -282,6 +285,7 @@ abstract class FilePartTest {
MultipartFile.fromFileSync(
image.path,
filename: 'my_profile_image.jpg',
headers: {},
),
));
''',
Expand Down Expand Up @@ -320,7 +324,8 @@ abstract class FilePartWithMultipartListTest {
@RestApi(baseUrl: 'https://httpbin.org/')
abstract class FilePartWithNullableMultipartListTest {
@POST('/profile')
Future<String> setProfile(@Part() List<MultipartFile>? images);
Future<String> setProfile(
@Part(keepFilePath: true) List<MultipartFile>? images);
}

@ShouldGenerate(
Expand All @@ -331,6 +336,9 @@ abstract class FilePartWithNullableMultipartListTest {
MultipartFile.fromFileSync(
image.path,
filename: image.path.split(Platform.pathSeparator).last,
headers: {
'original_file_path': [image.path]
},
),
));
''',
Expand All @@ -339,7 +347,7 @@ abstract class FilePartWithNullableMultipartListTest {
@RestApi(baseUrl: 'https://httpbin.org/')
abstract class UploadFileInfoPartTest {
@POST('/profile')
Future<String> setProfile(@Part() File image);
Future<String> setProfile(@Part(keepFilePath: true) File image);
}

@ShouldGenerate(
Expand Down Expand Up @@ -940,6 +948,7 @@ abstract class TestHttpResponseArray {
MultipartFile.fromFileSync(
i.path,
filename: i.path.split(Platform.pathSeparator).last,
headers: {},
))));
''',
contains: true,
Expand All @@ -953,6 +962,7 @@ abstract class TestHttpResponseArray {
MultipartFile.fromFileSync(
i.path,
filename: i.path.split(Platform.pathSeparator).last,
headers: {},
))));
}
''',
Expand All @@ -966,6 +976,7 @@ abstract class TestHttpResponseArray {
MultipartFile.fromFileSync(
file.path,
filename: file.path.split(Platform.pathSeparator).last,
headers: {},
),
));
}
Expand Down
4 changes: 4 additions & 0 deletions retrofit/lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ class Part {
this.name,
this.fileName,
this.contentType,
this.keepFilePath = false,
});

@Deprecated('future release')
Expand All @@ -313,6 +314,9 @@ class Part {

// To identify the content type of a file
final String? contentType;

/// If this field is a file, optionally specify whether to keep the file path for retrying requests
final bool? keepFilePath;
}

@immutable
Expand Down