Refactor code

This commit is contained in:
Ashita Prasad
2024-07-15 00:08:30 +05:30
parent 71924091ee
commit ad4d2c847c
7 changed files with 103 additions and 107 deletions

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View 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;
}
}
}

View 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;
}
}
}

View File

@ -1,5 +1,4 @@
import 'package:apidash/fileimport/fileimport.dart';
import 'package:apidash/widgets/drag_and_drop_area.dart';
import 'package:apidash/importer/importer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash/providers/providers.dart';
@ -9,7 +8,7 @@ import 'package:apidash/models/models.dart';
import 'package:apidash/consts.dart';
import '../common_widgets/common_widgets.dart';
final fileImport = FileImport();
final kImporter = Importer();
class CollectionPane extends ConsumerWidget {
const CollectionPane({
@ -37,42 +36,25 @@ class CollectionPane extends ConsumerWidget {
ref.read(collectionStateNotifierProvider.notifier).add();
},
onImport: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
contentPadding: const EdgeInsets.all(12),
content: DragAndDropArea(
onFileDropped: (file) {
ref.read(collectionStateNotifierProvider.notifier).add();
final newId = ref.watch(selectedIdStateProvider)!;
final i = file.path.lastIndexOf('.') + 1;
final String ext = file.path.substring(i);
final fileType = ImportFormat.values.byName(ext);
file.readAsString().then((contents) {
fileImport
.getRequestModel(fileType, contents, newId)
showImportDialog(
context,
(file) {
final importFormatType = ref.read(importFormatStateProvider);
file.readAsString().then((content) {
kImporter
.getHttpRequestModel(importFormatType, content)
.then((importedRequestModel) {
if (importedRequestModel == null) {
// could not parse the file
return;
}
final rM = importedRequestModel.httpRequestModel!;
if (importedRequestModel != null) {
ref
.read(collectionStateNotifierProvider.notifier)
.update(
newId,
method: rM.method,
url: rM.url,
headers: rM.headers,
params: rM.params,
body: rM.body,
);
.addRequestModel(importedRequestModel);
} else {
// TODO: Throw an error, unable to parse
}
});
});
Navigator.of(context).pop();
},
),
),
);
},
),

View 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,
),
);
},
);
}

View File

@ -11,7 +11,9 @@ export 'checkbox.dart';
export 'code_previewer.dart';
export 'codegen_previewer.dart';
export 'dialog_about.dart';
export 'dialog_import.dart';
export 'dialog_rename.dart';
export 'drag_and_drop_area.dart';
export 'dropdown_codegen.dart';
export 'dropdown_content_type.dart';
export 'dropdown_formdata.dart';