Add the functionality to import a curl file into the app.

The user can click on the button saying "+ Import" after which, they
will be allow to drop a curl file and import the curl command in the
app.
This commit is contained in:
Ketan Sonar
2024-07-04 23:35:59 +05:30
parent 063638b8ef
commit 7720611cd7
8 changed files with 180 additions and 3 deletions

View File

@@ -1,3 +1,8 @@
import 'dart:io';
import 'package:apidash/utils/convert_utils.dart';
import 'package:apidash/widgets/drag_and_drop_area.dart';
import 'package:curl_converter/curl_converter.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash/providers/providers.dart';
@@ -32,6 +37,65 @@ class CollectionPane extends ConsumerWidget {
onAddNew: () {
ref.read(collectionStateNotifierProvider.notifier).add();
},
onImport: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
contentPadding: const EdgeInsets.all(12),
content: DragAndDropArea(
onFileDropped: (file) {
final i = file.path.lastIndexOf('.') + 1;
final String ext = file.path.substring(i);
switch (ext) {
case 'curl':
file.readAsString().then((value) {
if (value.endsWith('\n')) {
value = value.substring(0, value.length - 1);
}
try {
final curl = Curl.parse(value);
ref
.read(
collectionStateNotifierProvider.notifier)
.add();
final selectedId =
ref.read(selectedIdStateProvider)!;
ref
.read(
collectionStateNotifierProvider.notifier)
.update(
selectedId,
method: httpVerbFromString(curl.method),
url: curl.uri.toString().substring(0,
curl.uri.toString().lastIndexOf('?')),
headers: curl.headers?.entries
.map((entry) => NameValueModel(
name: entry.key,
value: entry.value,
))
.toList(),
params: curl.uri.queryParameters.entries
.map((entry) => NameValueModel(
name: entry.key,
value: entry.value,
))
.toList(),
body: curl.data,
);
} catch (e) {
throw Error();
}
Navigator.of(context).pop();
});
break;
default:
throw Error();
}
},
),
),
);
},
),
kVSpacer10,
SidebarFilter(