mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 02:39:19 +08:00
apidash_core: contents moved into better_networking package
This commit is contained in:
54
packages/better_networking/lib/utils/content_type_utils.dart
Normal file
54
packages/better_networking/lib/utils/content_type_utils.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'package:http_parser/http_parser.dart';
|
||||
import '../consts.dart';
|
||||
import '../extensions/extensions.dart';
|
||||
|
||||
ContentType? getContentTypeFromHeadersMap(
|
||||
Map<String, String>? kvMap,
|
||||
) {
|
||||
if (kvMap != null && kvMap.hasKeyContentType()) {
|
||||
var val = getMediaTypeFromHeaders(kvMap);
|
||||
return getContentTypeFromMediaType(val);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
MediaType? getMediaTypeFromHeaders(Map? headers) {
|
||||
var contentType = headers?.getValueContentType();
|
||||
MediaType? mediaType = getMediaTypeFromContentType(contentType);
|
||||
return mediaType;
|
||||
}
|
||||
|
||||
MediaType? getMediaTypeFromContentType(String? contentType) {
|
||||
if (contentType != null) {
|
||||
try {
|
||||
MediaType mediaType = MediaType.parse(contentType);
|
||||
return mediaType;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
ContentType? getContentTypeFromMediaType(MediaType? mediaType) {
|
||||
if (mediaType != null) {
|
||||
if (mediaType.subtype.contains(kSubTypeJson)) {
|
||||
return ContentType.json;
|
||||
} else if (mediaType.type == kTypeMultipart &&
|
||||
mediaType.subtype == kSubTypeFormData) {
|
||||
return ContentType.formdata;
|
||||
}
|
||||
return ContentType.text;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
ContentType? getContentTypeFromContentTypeStr(
|
||||
String? contentType,
|
||||
) {
|
||||
if (contentType != null) {
|
||||
var val = getMediaTypeFromContentType(contentType);
|
||||
return getContentTypeFromMediaType(val);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
11
packages/better_networking/lib/utils/graphql_utils.dart
Normal file
11
packages/better_networking/lib/utils/graphql_utils.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
import '../consts.dart';
|
||||
import '../models/models.dart';
|
||||
|
||||
String? getGraphQLBody(HttpRequestModel httpRequestModel) {
|
||||
if (httpRequestModel.hasQuery) {
|
||||
return kJsonEncoder.convert({
|
||||
"query": httpRequestModel.query,
|
||||
});
|
||||
}
|
||||
return null;
|
||||
}
|
||||
111
packages/better_networking/lib/utils/http_request_utils.dart
Normal file
111
packages/better_networking/lib/utils/http_request_utils.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
import 'package:better_networking/consts.dart';
|
||||
import 'package:seed/seed.dart';
|
||||
import '../models/models.dart';
|
||||
import 'graphql_utils.dart';
|
||||
import 'package:json5/json5.dart' as json5;
|
||||
|
||||
Map<String, String>? rowsToMap(
|
||||
List<NameValueModel>? kvRows, {
|
||||
bool isHeader = false,
|
||||
}) {
|
||||
if (kvRows == null) {
|
||||
return null;
|
||||
}
|
||||
Map<String, String> finalMap = {};
|
||||
for (var row in kvRows) {
|
||||
if (row.name.trim() != "") {
|
||||
String key = row.name;
|
||||
if (isHeader) {
|
||||
key = key.toLowerCase();
|
||||
}
|
||||
finalMap[key] = row.value.toString();
|
||||
}
|
||||
}
|
||||
return finalMap;
|
||||
}
|
||||
|
||||
List<NameValueModel>? mapToRows(Map<String, String>? kvMap) {
|
||||
if (kvMap == null) {
|
||||
return null;
|
||||
}
|
||||
List<NameValueModel> finalRows = [];
|
||||
for (var k in kvMap.keys) {
|
||||
finalRows.add(NameValueModel(name: k, value: kvMap[k]));
|
||||
}
|
||||
return finalRows;
|
||||
}
|
||||
|
||||
List<Map<String, String>>? rowsToFormDataMapList(List<FormDataModel>? kvRows) {
|
||||
if (kvRows == null) {
|
||||
return null;
|
||||
}
|
||||
List<Map<String, String>> finalMap = kvRows
|
||||
.map(
|
||||
(FormDataModel formData) =>
|
||||
(formData.name.trim().isEmpty && formData.value.trim().isEmpty)
|
||||
? null
|
||||
: {
|
||||
"name": formData.name,
|
||||
"value": formData.value,
|
||||
"type": formData.type.name,
|
||||
},
|
||||
)
|
||||
.nonNulls
|
||||
.toList();
|
||||
return finalMap;
|
||||
}
|
||||
|
||||
List<FormDataModel>? mapListToFormDataModelRows(List<Map>? kvMap) {
|
||||
if (kvMap == null) {
|
||||
return null;
|
||||
}
|
||||
List<FormDataModel> finalRows = kvMap.map((formData) {
|
||||
return FormDataModel(
|
||||
name: formData["name"],
|
||||
value: formData["value"],
|
||||
type: getFormDataType(formData["type"]),
|
||||
);
|
||||
}).toList();
|
||||
return finalRows;
|
||||
}
|
||||
|
||||
FormDataType getFormDataType(String? type) {
|
||||
return FormDataType.values.firstWhere(
|
||||
(element) => element.name == type,
|
||||
orElse: () => FormDataType.text,
|
||||
);
|
||||
}
|
||||
|
||||
List<NameValueModel>? getEnabledRows(
|
||||
List<NameValueModel>? rows,
|
||||
List<bool>? isRowEnabledList,
|
||||
) {
|
||||
if (rows == null || isRowEnabledList == null) {
|
||||
return rows;
|
||||
}
|
||||
List<NameValueModel> finalRows = rows
|
||||
.where((element) => isRowEnabledList[rows.indexOf(element)])
|
||||
.toList();
|
||||
return finalRows == [] ? null : finalRows;
|
||||
}
|
||||
|
||||
String? getRequestBody(APIType type, HttpRequestModel httpRequestModel) {
|
||||
return switch (type) {
|
||||
APIType.rest =>
|
||||
(httpRequestModel.hasJsonData || httpRequestModel.hasTextData)
|
||||
? httpRequestModel.body
|
||||
: null,
|
||||
APIType.graphql => getGraphQLBody(httpRequestModel),
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: Expose this function to remove JSON comments
|
||||
String? removeJsonComments(String? json) {
|
||||
try {
|
||||
if (json == null) return null;
|
||||
var parsed = json5.json5Decode(json);
|
||||
return kJsonEncoder.convert(parsed);
|
||||
} catch (e) {
|
||||
return json;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http_parser/http_parser.dart';
|
||||
import 'package:xml/xml.dart';
|
||||
import '../consts.dart';
|
||||
|
||||
String? formatBody(String? body, MediaType? mediaType) {
|
||||
if (mediaType != null && body != null) {
|
||||
var subtype = mediaType.subtype;
|
||||
try {
|
||||
if (subtype.contains(kSubTypeJson)) {
|
||||
final tmp = jsonDecode(body);
|
||||
String result = kJsonEncoder.convert(tmp);
|
||||
return result;
|
||||
}
|
||||
if (subtype.contains(kSubTypeXml)) {
|
||||
final document = XmlDocument.parse(body);
|
||||
String result = document.toXmlString(pretty: true, indent: ' ');
|
||||
return result;
|
||||
}
|
||||
if (subtype == kSubTypeHtml) {
|
||||
var len = body.length;
|
||||
var lines = kSplitter.convert(body);
|
||||
var numOfLines = lines.length;
|
||||
if (numOfLines != 0 && len / numOfLines <= kCodeCharsPerLineLimit) {
|
||||
return body;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<http.Response> convertStreamedResponse(
|
||||
http.StreamedResponse streamedResponse,
|
||||
) async {
|
||||
Uint8List bodyBytes = await streamedResponse.stream.toBytes();
|
||||
|
||||
http.Response response = http.Response.bytes(
|
||||
bodyBytes,
|
||||
streamedResponse.statusCode,
|
||||
headers: streamedResponse.headers,
|
||||
persistentConnection: streamedResponse.persistentConnection,
|
||||
reasonPhrase: streamedResponse.reasonPhrase,
|
||||
request: streamedResponse.request,
|
||||
);
|
||||
|
||||
return response;
|
||||
}
|
||||
19
packages/better_networking/lib/utils/string_utils.dart
Normal file
19
packages/better_networking/lib/utils/string_utils.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'dart:math';
|
||||
|
||||
class RandomStringGenerator {
|
||||
static const _chars =
|
||||
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
|
||||
static Random rnd = Random();
|
||||
|
||||
static String getRandomString(int length) =>
|
||||
String.fromCharCodes(Iterable.generate(
|
||||
length, (_) => _chars.codeUnitAt(rnd.nextInt(_chars.length))));
|
||||
|
||||
static String getRandomStringLines(int lines, int length) {
|
||||
List<String> result = [];
|
||||
for (var i = 0; i < lines; i++) {
|
||||
result.add(getRandomString(length));
|
||||
}
|
||||
return result.join('\n');
|
||||
}
|
||||
}
|
||||
65
packages/better_networking/lib/utils/uri_utils.dart
Normal file
65
packages/better_networking/lib/utils/uri_utils.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'package:collection/collection.dart' show mergeMaps;
|
||||
import 'package:seed/seed.dart';
|
||||
import '../consts.dart';
|
||||
import 'http_request_utils.dart';
|
||||
|
||||
(String?, bool) getUriScheme(Uri uri) {
|
||||
if (uri.hasScheme) {
|
||||
if (kSupportedUriSchemes.contains(uri.scheme.toLowerCase())) {
|
||||
return (uri.scheme, true);
|
||||
}
|
||||
return (uri.scheme, false);
|
||||
}
|
||||
return (null, false);
|
||||
}
|
||||
|
||||
String stripUriParams(Uri uri) {
|
||||
return "${uri.scheme}://${uri.authority}${uri.path}";
|
||||
}
|
||||
|
||||
String stripUrlParams(String url) {
|
||||
var idx = url.indexOf("?");
|
||||
return idx > 0 ? url.substring(0, idx) : url;
|
||||
}
|
||||
|
||||
(Uri?, String?) getValidRequestUri(
|
||||
String? url, List<NameValueModel>? requestParams,
|
||||
{SupportedUriSchemes defaultUriScheme = kDefaultUriScheme}) {
|
||||
url = url?.trim();
|
||||
if (url == null || url == "") {
|
||||
return (null, "URL is missing!");
|
||||
}
|
||||
|
||||
if (kLocalhostRegex.hasMatch(url) || kIPHostRegex.hasMatch(url)) {
|
||||
url = '${SupportedUriSchemes.http.name}://$url';
|
||||
}
|
||||
|
||||
Uri? uri = Uri.tryParse(url);
|
||||
if (uri == null) {
|
||||
return (null, "Check URL (malformed)");
|
||||
}
|
||||
(String?, bool) urlScheme = getUriScheme(uri);
|
||||
|
||||
if (urlScheme.$1 != null) {
|
||||
if (!urlScheme.$2) {
|
||||
return (null, "Unsupported URL Scheme (${urlScheme.$1})");
|
||||
}
|
||||
} else {
|
||||
url = "${defaultUriScheme.name}://$url";
|
||||
}
|
||||
|
||||
uri = Uri.parse(url);
|
||||
if (uri.hasFragment) {
|
||||
uri = uri.removeFragment();
|
||||
}
|
||||
|
||||
Map<String, String>? queryParams = rowsToMap(requestParams);
|
||||
if (queryParams != null && queryParams.isNotEmpty) {
|
||||
if (uri.hasQuery) {
|
||||
Map<String, String> urlQueryParams = uri.queryParameters;
|
||||
queryParams = mergeMaps(urlQueryParams, queryParams);
|
||||
}
|
||||
uri = uri.replace(queryParameters: queryParams);
|
||||
}
|
||||
return (uri, null);
|
||||
}
|
||||
6
packages/better_networking/lib/utils/utils.dart
Normal file
6
packages/better_networking/lib/utils/utils.dart
Normal file
@@ -0,0 +1,6 @@
|
||||
export 'content_type_utils.dart';
|
||||
export 'graphql_utils.dart';
|
||||
export 'http_request_utils.dart';
|
||||
export 'http_response_utils.dart';
|
||||
export 'string_utils.dart';
|
||||
export 'uri_utils.dart';
|
||||
Reference in New Issue
Block a user