mirror of
https://github.com/foss42/apidash.git
synced 2025-08-06 13:51:20 +08:00
refactor importer
This commit is contained in:
@ -11,6 +11,8 @@ class CurlFileImport {
|
|||||||
final headers = mapToRows(curl.headers);
|
final headers = mapToRows(curl.headers);
|
||||||
final params = mapToRows(curl.uri.queryParameters);
|
final params = mapToRows(curl.uri.queryParameters);
|
||||||
final body = curl.data;
|
final body = curl.data;
|
||||||
|
// TODO: formdata with file paths must be set to empty as
|
||||||
|
// there will be permission issue while trying to access the path
|
||||||
final formData = curl.formData;
|
final formData = curl.formData;
|
||||||
// Determine content type based on form data and headers
|
// Determine content type based on form data and headers
|
||||||
final ContentType contentType = curl.form
|
final ContentType contentType = curl.form
|
||||||
|
50
lib/importer/import_dialog.dart
Normal file
50
lib/importer/import_dialog.dart
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:apidash/providers/providers.dart';
|
||||||
|
import 'package:apidash/widgets/widgets.dart';
|
||||||
|
import 'importer.dart';
|
||||||
|
|
||||||
|
void importToCollectionPane(
|
||||||
|
BuildContext context,
|
||||||
|
WidgetRef ref,
|
||||||
|
ScaffoldMessengerState sm,
|
||||||
|
) {
|
||||||
|
// TODO: The dialog must have a feature to paste contents in a text field
|
||||||
|
// Also, a mechanism can be added where on importing a file it shows the
|
||||||
|
// contents in the text field and then the user presses ok to add it to collection
|
||||||
|
showImportDialog(
|
||||||
|
context: context,
|
||||||
|
importFormat: ref.watch(importFormatStateProvider),
|
||||||
|
onImportFormatChange: (format) {
|
||||||
|
if (format != null) {
|
||||||
|
ref.read(importFormatStateProvider.notifier).state = format;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onFileDropped: (file) {
|
||||||
|
final importFormatType = ref.read(importFormatStateProvider);
|
||||||
|
sm.hideCurrentSnackBar();
|
||||||
|
file.readAsString().then(
|
||||||
|
(content) {
|
||||||
|
kImporter
|
||||||
|
.getHttpRequestModel(importFormatType, content)
|
||||||
|
.then((importedRequestModel) {
|
||||||
|
if (importedRequestModel != null) {
|
||||||
|
ref
|
||||||
|
.read(collectionStateNotifierProvider.notifier)
|
||||||
|
.addRequestModel(importedRequestModel);
|
||||||
|
if (!context.mounted) return;
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
} else {
|
||||||
|
var err = "Unable to parse ${file.name}";
|
||||||
|
sm.showSnackBar(getSnackBar(err, small: false));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onError: (e) {
|
||||||
|
var err = "Unable to import ${file.name}";
|
||||||
|
sm.showSnackBar(getSnackBar(err, small: false));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
@ -13,3 +13,5 @@ class Importer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final kImporter = Importer();
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
import 'package:apidash_design_system/apidash_design_system.dart';
|
import 'package:apidash_design_system/apidash_design_system.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:apidash/importer/import_dialog.dart';
|
||||||
import 'package:apidash/providers/providers.dart';
|
import 'package:apidash/providers/providers.dart';
|
||||||
import 'package:apidash/importer/importer.dart';
|
|
||||||
import 'package:apidash/extensions/extensions.dart';
|
import 'package:apidash/extensions/extensions.dart';
|
||||||
import 'package:apidash/widgets/widgets.dart';
|
import 'package:apidash/widgets/widgets.dart';
|
||||||
import 'package:apidash/models/models.dart';
|
import 'package:apidash/models/models.dart';
|
||||||
import 'package:apidash/consts.dart';
|
import 'package:apidash/consts.dart';
|
||||||
import '../common_widgets/common_widgets.dart';
|
import '../common_widgets/common_widgets.dart';
|
||||||
|
|
||||||
final kImporter = Importer();
|
|
||||||
|
|
||||||
class CollectionPane extends ConsumerWidget {
|
class CollectionPane extends ConsumerWidget {
|
||||||
const CollectionPane({
|
const CollectionPane({
|
||||||
super.key,
|
super.key,
|
||||||
@ -38,40 +36,7 @@ class CollectionPane extends ConsumerWidget {
|
|||||||
ref.read(collectionStateNotifierProvider.notifier).add();
|
ref.read(collectionStateNotifierProvider.notifier).add();
|
||||||
},
|
},
|
||||||
onImport: () {
|
onImport: () {
|
||||||
showImportDialog(
|
importToCollectionPane(context, ref, sm);
|
||||||
context: context,
|
|
||||||
importFormat: ref.watch(importFormatStateProvider),
|
|
||||||
onImportFormatChange: (format) {
|
|
||||||
if (format != null) {
|
|
||||||
ref.read(importFormatStateProvider.notifier).state = format;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onFileDropped: (file) {
|
|
||||||
final importFormatType = ref.read(importFormatStateProvider);
|
|
||||||
sm.hideCurrentSnackBar();
|
|
||||||
file.readAsString().then(
|
|
||||||
(content) {
|
|
||||||
kImporter
|
|
||||||
.getHttpRequestModel(importFormatType, content)
|
|
||||||
.then((importedRequestModel) {
|
|
||||||
if (importedRequestModel != null) {
|
|
||||||
ref
|
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
|
||||||
.addRequestModel(importedRequestModel);
|
|
||||||
} else {
|
|
||||||
var err = "Unable to parse ${file.name}";
|
|
||||||
sm.showSnackBar(getSnackBar(err, small: false));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
onError: (e) {
|
|
||||||
var err = "Unable to import ${file.name}";
|
|
||||||
sm.showSnackBar(getSnackBar(err, small: false));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
Navigator.of(context).pop();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
kVSpacer10,
|
kVSpacer10,
|
||||||
|
Reference in New Issue
Block a user