mirror of
https://github.com/foss42/apidash.git
synced 2025-06-30 12:57:27 +08:00
Add graphql support in apicore_core
This commit is contained in:
@ -12,6 +12,7 @@ typedef HttpResponse = http.Response;
|
|||||||
|
|
||||||
Future<(HttpResponse?, Duration?, String?)> request(
|
Future<(HttpResponse?, Duration?, String?)> request(
|
||||||
String requestId,
|
String requestId,
|
||||||
|
APIType apiType,
|
||||||
HttpRequestModel requestModel, {
|
HttpRequestModel requestModel, {
|
||||||
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
|
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
|
||||||
bool noSSL = false,
|
bool noSSL = false,
|
||||||
@ -28,10 +29,11 @@ Future<(HttpResponse?, Duration?, String?)> request(
|
|||||||
if (uriRec.$1 != null) {
|
if (uriRec.$1 != null) {
|
||||||
Uri requestUrl = uriRec.$1!;
|
Uri requestUrl = uriRec.$1!;
|
||||||
Map<String, String> headers = requestModel.enabledHeadersMap;
|
Map<String, String> headers = requestModel.enabledHeadersMap;
|
||||||
HttpResponse response;
|
HttpResponse? response;
|
||||||
String? body;
|
String? body;
|
||||||
try {
|
try {
|
||||||
Stopwatch stopwatch = Stopwatch()..start();
|
Stopwatch stopwatch = Stopwatch()..start();
|
||||||
|
if (apiType == APIType.rest) {
|
||||||
var isMultiPartRequest =
|
var isMultiPartRequest =
|
||||||
requestModel.bodyContentType == ContentType.formdata;
|
requestModel.bodyContentType == ContentType.formdata;
|
||||||
|
|
||||||
@ -41,7 +43,8 @@ Future<(HttpResponse?, Duration?, String?)> request(
|
|||||||
var contentLength = utf8.encode(requestBody).length;
|
var contentLength = utf8.encode(requestBody).length;
|
||||||
if (contentLength > 0) {
|
if (contentLength > 0) {
|
||||||
body = requestBody;
|
body = requestBody;
|
||||||
headers[HttpHeaders.contentLengthHeader] = contentLength.toString();
|
headers[HttpHeaders.contentLengthHeader] =
|
||||||
|
contentLength.toString();
|
||||||
if (!requestModel.hasContentTypeHeader) {
|
if (!requestModel.hasContentTypeHeader) {
|
||||||
headers[HttpHeaders.contentTypeHeader] =
|
headers[HttpHeaders.contentTypeHeader] =
|
||||||
requestModel.bodyContentType.header;
|
requestModel.bodyContentType.header;
|
||||||
@ -86,7 +89,8 @@ Future<(HttpResponse?, Duration?, String?)> request(
|
|||||||
await client.post(requestUrl, headers: headers, body: body);
|
await client.post(requestUrl, headers: headers, body: body);
|
||||||
break;
|
break;
|
||||||
case HTTPVerb.put:
|
case HTTPVerb.put:
|
||||||
response = await client.put(requestUrl, headers: headers, body: body);
|
response =
|
||||||
|
await client.put(requestUrl, headers: headers, body: body);
|
||||||
break;
|
break;
|
||||||
case HTTPVerb.patch:
|
case HTTPVerb.patch:
|
||||||
response =
|
response =
|
||||||
@ -97,6 +101,25 @@ Future<(HttpResponse?, Duration?, String?)> request(
|
|||||||
await client.delete(requestUrl, headers: headers, body: body);
|
await client.delete(requestUrl, headers: headers, body: body);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (apiType == APIType.graphql) {
|
||||||
|
var requestBody = getGraphQLBody(requestModel);
|
||||||
|
if (requestBody != null) {
|
||||||
|
var contentLength = utf8.encode(requestBody).length;
|
||||||
|
if (contentLength > 0) {
|
||||||
|
body = requestBody;
|
||||||
|
headers[HttpHeaders.contentLengthHeader] = contentLength.toString();
|
||||||
|
if (!requestModel.hasContentTypeHeader) {
|
||||||
|
headers[HttpHeaders.contentTypeHeader] = ContentType.json.header;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
response = await client.post(
|
||||||
|
requestUrl,
|
||||||
|
headers: headers,
|
||||||
|
body: body,
|
||||||
|
);
|
||||||
|
}
|
||||||
stopwatch.stop();
|
stopwatch.stop();
|
||||||
return (response, stopwatch.elapsed, null);
|
return (response, stopwatch.elapsed, null);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
11
packages/apidash_core/lib/utils/graphql_utils.dart
Normal file
11
packages/apidash_core/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;
|
||||||
|
}
|
@ -1,5 +1,8 @@
|
|||||||
|
import 'package:apidash_core/consts.dart';
|
||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
import 'package:seed/seed.dart';
|
import 'package:seed/seed.dart';
|
||||||
|
import '../models/models.dart';
|
||||||
|
import 'graphql_utils.dart';
|
||||||
|
|
||||||
Map<String, String>? rowsToMap(
|
Map<String, String>? rowsToMap(
|
||||||
List<NameValueModel>? kvRows, {
|
List<NameValueModel>? kvRows, {
|
||||||
@ -88,3 +91,13 @@ List<NameValueModel>? getEnabledRows(
|
|||||||
rows.where((element) => isRowEnabledList[rows.indexOf(element)]).toList();
|
rows.where((element) => isRowEnabledList[rows.indexOf(element)]).toList();
|
||||||
return finalRows == [] ? null : finalRows;
|
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),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
export 'content_type_utils.dart';
|
export 'content_type_utils.dart';
|
||||||
|
export 'graphql_utils.dart';
|
||||||
export 'http_request_utils.dart';
|
export 'http_request_utils.dart';
|
||||||
export 'http_response_utils.dart';
|
export 'http_response_utils.dart';
|
||||||
export 'string_utils.dart';
|
export 'string_utils.dart';
|
||||||
|
Reference in New Issue
Block a user