Skip to content

Commit 3403f65

Browse files
committed
fix: use http package to handle zip file upload
- Add `http` package dependency, and use it instead of manually making the upload post request.
1 parent df7c665 commit 3403f65

File tree

3 files changed

+62
-51
lines changed

3 files changed

+62
-51
lines changed

bin/commands/upload_so_files.dart

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ class UploadSoFilesCommand {
9696

9797
// Validate architecture
9898
if (!validArchs.contains(options.arch)) {
99-
print('[Instabug-CLI] Error: Invalid architecture: ${options.arch}. Valid options: ${validArchs.join(', ')}');
99+
print(
100+
'[Instabug-CLI] Error: Invalid architecture: ${options.arch}. Valid options: ${validArchs.join(', ')}');
100101
throw Exception(
101102
'Invalid architecture: ${options.arch}. Valid options: ${validArchs.join(', ')}');
102103
}
@@ -106,31 +107,44 @@ class UploadSoFilesCommand {
106107
print('File: ${options.file}');
107108
print('App Version: ${options.name}');
108109

109-
// TODO: Implement the actual upload logic here
110-
// This would typically involve:
111-
// 1. Reading the zip file
112-
// 2. Making an HTTP request to the upload endpoint
113-
// 3. Handling the response
114-
115-
// Make an HTTP request to the upload endpoint
116-
final body = {
117-
'arch': options.arch,
118-
'api_key': options.apiKey,
119-
'application_token': options.token,
120-
'so_file': options.file,
121-
'app_version': options.name,
122-
};
123-
124110
const endPoint = 'https://api.instabug.com/api/web/public/so_files';
125111

126-
final response = await makeHttpPostRequest(
127-
url: endPoint,
128-
body: body,
112+
// Create multipart request
113+
final request = http.MultipartRequest('POST', Uri.parse(endPoint));
114+
115+
// Add form fields
116+
request.fields['arch'] = options.arch;
117+
request.fields['api_key'] = options.apiKey;
118+
request.fields['application_token'] = options.token;
119+
request.fields['app_version'] = options.name;
120+
121+
// Add the zip file
122+
final fileStream = http.ByteStream(file.openRead());
123+
final fileLength = await file.length();
124+
final multipartFile = http.MultipartFile(
125+
'so_file',
126+
fileStream,
127+
fileLength,
128+
filename: file.path.split('/').last,
129129
);
130+
request.files.add(multipartFile);
131+
132+
final response = await request.send();
133+
134+
if (response.statusCode < 200 || response.statusCode >= 300) {
135+
final responseBody = await response.stream.bytesToString();
136+
print('[Instabug-CLI] Error: Failed to upload .so files');
137+
print('Status Code: ${response.statusCode}');
138+
print('Response: $responseBody');
139+
exit(1);
140+
}
130141

131-
print('Successfully uploaded .so files for version: ${options.name} with arch ${options.arch}');
142+
print(
143+
'Successfully uploaded .so files for version: ${options.name} with arch ${options.arch}');
144+
exit(0);
132145
} catch (e) {
133-
print('[Instabug-CLI] Error: Error uploading .so files: $e');
146+
print('[Instabug-CLI] Error uploading .so files, $e');
147+
print('[Instabug-CLI] Error Stack Trace: ${StackTrace.current}');
134148
exit(1);
135149
}
136150
}

bin/instabug.dart

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/usr/bin/env dart
22

3-
import 'dart:convert';
43
import 'dart:io';
54

65
import 'package:args/args.dart';
6+
import 'package:http/http.dart' as http;
77

88
part 'commands/upload_so_files.dart';
99

1010
// Command registry for easy management
11+
// ignore: avoid_classes_with_only_static_members
1112
class CommandRegistry {
1213
static final Map<String, CommandHandler> _commands = {
1314
'upload-so-files': CommandHandler(
@@ -27,35 +28,30 @@ class CommandHandler {
2728
CommandHandler({required this.parser, required this.execute});
2829
}
2930

30-
Future<bool> makeHttpPostRequest({
31-
required String url,
32-
required Map<String, String> body,
33-
Map<String, String>? headers,
34-
}) async {
35-
try {
36-
final client = HttpClient();
37-
38-
final request = await client.postUrl(Uri.parse(url));
39-
40-
request.headers.contentType = ContentType.json;
41-
42-
request.write(jsonEncode(body));
43-
44-
final response = await request.close();
45-
46-
if (response.statusCode >= 200 && response.statusCode < 300) {
47-
final responseBody = await response.transform(utf8.decoder).join();
48-
return true;
49-
} else {
50-
print('Error: ${response.statusCode}');
51-
return false;
52-
}
53-
54-
} catch (e) {
55-
print('[Instabug-CLI] Error while making HTTP POST request: $e');
56-
exit(1);
57-
}
58-
}
31+
// Future<bool> makeHttpPostRequest({
32+
// required String url,
33+
// required Map<String, String> body,
34+
// Map<String, String>? headers,
35+
// }) async {
36+
// try {
37+
// final client = HttpClient();
38+
// final request = await client.postUrl(Uri.parse(url));
39+
// request.headers.contentType = ContentType.json;
40+
// request.write(jsonEncode(body));
41+
// final response = await request.close();
42+
// if (response.statusCode >= 200 && response.statusCode < 300) {
43+
// final responseBody = await response.transform(utf8.decoder).join();
44+
// return true;
45+
// } else {
46+
// print('Error Code: ${response.statusCode}');
47+
// print('Error Body: ${await response.transform(utf8.decoder).join()}');
48+
// return false;
49+
// }
50+
// } catch (e) {
51+
// print('[Instabug-CLI] Error while making HTTP POST request: $e');
52+
// exit(1);
53+
// }
54+
// }
5955

6056
void main(List<String> args) async {
6157
final parser = ArgParser()..addFlag('help', abbr: 'h');

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dependencies:
1111
args: ^2.3.0
1212
flutter:
1313
sdk: flutter
14+
http: ^0.13.3
1415
meta: ^1.3.0
1516
stack_trace: ^1.10.0
1617

0 commit comments

Comments
 (0)