mirror of
https://github.com/foss42/apidash.git
synced 2025-12-08 05:59:15 +08:00
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:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user