mirror of
https://github.com/foss42/apidash.git
synced 2025-05-29 21:06:01 +08:00
Refactor code
This commit is contained in:
@ -1,52 +0,0 @@
|
|||||||
import 'package:apidash/consts.dart';
|
|
||||||
import 'package:apidash/models/http_request_model.dart';
|
|
||||||
import 'package:apidash/models/name_value_model.dart';
|
|
||||||
import 'package:apidash/models/request_model.dart';
|
|
||||||
import 'package:curl_converter/curl_converter.dart';
|
|
||||||
|
|
||||||
class CurlFileImport {
|
|
||||||
RequestModel? getRequestModel(String contents, String newId) {
|
|
||||||
if (contents.endsWith('\n')) {
|
|
||||||
contents = contents.substring(0, contents.length - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
final curl = Curl.parse(contents);
|
|
||||||
|
|
||||||
final method = HTTPVerb.values.byName(curl.method.toLowerCase());
|
|
||||||
|
|
||||||
final uri = curl.uri.toString();
|
|
||||||
final url = uri.substring(0, uri.lastIndexOf('?'));
|
|
||||||
|
|
||||||
final headers = curl.headers?.entries
|
|
||||||
.map((entry) => NameValueModel(
|
|
||||||
name: entry.key,
|
|
||||||
value: entry.value,
|
|
||||||
))
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
final params = curl.uri.queryParameters.entries
|
|
||||||
.map((entry) => NameValueModel(
|
|
||||||
name: entry.key,
|
|
||||||
value: entry.value,
|
|
||||||
))
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
final body = curl.data;
|
|
||||||
|
|
||||||
return RequestModel(
|
|
||||||
id: newId,
|
|
||||||
httpRequestModel: HttpRequestModel(
|
|
||||||
method: method,
|
|
||||||
url: url,
|
|
||||||
headers: headers,
|
|
||||||
params: params,
|
|
||||||
body: body,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
} catch (e) {
|
|
||||||
print(e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
import 'package:apidash/consts.dart';
|
|
||||||
import 'package:apidash/fileimport/curl/curl.dart';
|
|
||||||
import 'package:apidash/models/request_model.dart';
|
|
||||||
|
|
||||||
class FileImport {
|
|
||||||
Future<RequestModel?> getRequestModel(
|
|
||||||
ImportFormat fileType, String contents, String newId) async {
|
|
||||||
switch (fileType) {
|
|
||||||
case ImportFormat.curl:
|
|
||||||
return CurlFileImport().getRequestModel(contents, newId);
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
42
lib/importer/curl/curl.dart
Normal file
42
lib/importer/curl/curl.dart
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import 'package:apidash/consts.dart';
|
||||||
|
import 'package:apidash/models/models.dart';
|
||||||
|
import 'package:apidash/utils/utils.dart';
|
||||||
|
import 'package:curl_converter/curl_converter.dart';
|
||||||
|
|
||||||
|
class CurlFileImport {
|
||||||
|
HttpRequestModel? getHttpRequestModel(String content) {
|
||||||
|
content = content.trim();
|
||||||
|
try {
|
||||||
|
final curl = Curl.parse(content);
|
||||||
|
final url = stripUriParams(curl.uri);
|
||||||
|
final method = HTTPVerb.values.byName(curl.method.toLowerCase());
|
||||||
|
|
||||||
|
final headers = curl.headers?.entries
|
||||||
|
.map((entry) => NameValueModel(
|
||||||
|
name: entry.key,
|
||||||
|
value: entry.value,
|
||||||
|
))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
final params = curl.uri.queryParameters.entries
|
||||||
|
.map((entry) => NameValueModel(
|
||||||
|
name: entry.key,
|
||||||
|
value: entry.value,
|
||||||
|
))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
// TODO: parse curl data to determine the type of body
|
||||||
|
final body = curl.data;
|
||||||
|
|
||||||
|
return HttpRequestModel(
|
||||||
|
method: method,
|
||||||
|
url: url,
|
||||||
|
headers: headers,
|
||||||
|
params: params,
|
||||||
|
body: body,
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
lib/importer/importer.dart
Normal file
17
lib/importer/importer.dart
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import 'package:apidash/consts.dart';
|
||||||
|
import 'package:apidash/models/models.dart';
|
||||||
|
import 'curl/curl.dart';
|
||||||
|
|
||||||
|
class Importer {
|
||||||
|
Future<HttpRequestModel?> getHttpRequestModel(
|
||||||
|
ImportFormat fileType,
|
||||||
|
String content,
|
||||||
|
) async {
|
||||||
|
switch (fileType) {
|
||||||
|
case ImportFormat.curl:
|
||||||
|
return CurlFileImport().getHttpRequestModel(content);
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
import 'package:apidash/fileimport/fileimport.dart';
|
import 'package:apidash/importer/importer.dart';
|
||||||
import 'package:apidash/widgets/drag_and_drop_area.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/providers/providers.dart';
|
import 'package:apidash/providers/providers.dart';
|
||||||
@ -9,7 +8,7 @@ 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 fileImport = FileImport();
|
final kImporter = Importer();
|
||||||
|
|
||||||
class CollectionPane extends ConsumerWidget {
|
class CollectionPane extends ConsumerWidget {
|
||||||
const CollectionPane({
|
const CollectionPane({
|
||||||
@ -37,48 +36,31 @@ class CollectionPane extends ConsumerWidget {
|
|||||||
ref.read(collectionStateNotifierProvider.notifier).add();
|
ref.read(collectionStateNotifierProvider.notifier).add();
|
||||||
},
|
},
|
||||||
onImport: () {
|
onImport: () {
|
||||||
showDialog(
|
showImportDialog(
|
||||||
context: context,
|
context,
|
||||||
builder: (context) => AlertDialog(
|
(file) {
|
||||||
contentPadding: const EdgeInsets.all(12),
|
final importFormatType = ref.read(importFormatStateProvider);
|
||||||
content: DragAndDropArea(
|
file.readAsString().then((content) {
|
||||||
onFileDropped: (file) {
|
kImporter
|
||||||
ref.read(collectionStateNotifierProvider.notifier).add();
|
.getHttpRequestModel(importFormatType, content)
|
||||||
final newId = ref.watch(selectedIdStateProvider)!;
|
.then((importedRequestModel) {
|
||||||
final i = file.path.lastIndexOf('.') + 1;
|
if (importedRequestModel != null) {
|
||||||
final String ext = file.path.substring(i);
|
ref
|
||||||
final fileType = ImportFormat.values.byName(ext);
|
.read(collectionStateNotifierProvider.notifier)
|
||||||
file.readAsString().then((contents) {
|
.addRequestModel(importedRequestModel);
|
||||||
fileImport
|
} else {
|
||||||
.getRequestModel(fileType, contents, newId)
|
// TODO: Throw an error, unable to parse
|
||||||
.then((importedRequestModel) {
|
}
|
||||||
if (importedRequestModel == null) {
|
});
|
||||||
// could not parse the file
|
});
|
||||||
return;
|
Navigator.of(context).pop();
|
||||||
}
|
},
|
||||||
final rM = importedRequestModel.httpRequestModel!;
|
|
||||||
ref
|
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
|
||||||
.update(
|
|
||||||
newId,
|
|
||||||
method: rM.method,
|
|
||||||
url: rM.url,
|
|
||||||
headers: rM.headers,
|
|
||||||
params: rM.params,
|
|
||||||
body: rM.body,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
Navigator.of(context).pop();
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
kVSpacer10,
|
kVSpacer10,
|
||||||
SidebarFilter(
|
SidebarFilter(
|
||||||
filterHintText: "Filter by name or url",
|
filterHintText: "Filter by name or url",
|
||||||
onFilterFieldChanged: (value) {
|
onFilterFieldChanged: (value) {
|
||||||
ref.read(collectionSearchQueryProvider.notifier).state =
|
ref.read(collectionSearchQueryProvider.notifier).state =
|
||||||
value.toLowerCase();
|
value.toLowerCase();
|
||||||
|
20
lib/widgets/dialog_import.dart
Normal file
20
lib/widgets/dialog_import.dart
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:file_selector/file_selector.dart';
|
||||||
|
import 'drag_and_drop_area.dart';
|
||||||
|
|
||||||
|
showImportDialog(
|
||||||
|
BuildContext context,
|
||||||
|
Function(XFile) onFileDropped,
|
||||||
|
) {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) {
|
||||||
|
return AlertDialog(
|
||||||
|
contentPadding: const EdgeInsets.all(12),
|
||||||
|
content: DragAndDropArea(
|
||||||
|
onFileDropped: onFileDropped,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
@ -11,7 +11,9 @@ export 'checkbox.dart';
|
|||||||
export 'code_previewer.dart';
|
export 'code_previewer.dart';
|
||||||
export 'codegen_previewer.dart';
|
export 'codegen_previewer.dart';
|
||||||
export 'dialog_about.dart';
|
export 'dialog_about.dart';
|
||||||
|
export 'dialog_import.dart';
|
||||||
export 'dialog_rename.dart';
|
export 'dialog_rename.dart';
|
||||||
|
export 'drag_and_drop_area.dart';
|
||||||
export 'dropdown_codegen.dart';
|
export 'dropdown_codegen.dart';
|
||||||
export 'dropdown_content_type.dart';
|
export 'dropdown_content_type.dart';
|
||||||
export 'dropdown_formdata.dart';
|
export 'dropdown_formdata.dart';
|
||||||
|
Reference in New Issue
Block a user