sendHttpRequestV1 function
Future<(HttpResponse?, Duration?, String?)>
sendHttpRequestV1(
- String requestId,
- APIType apiType,
- HttpRequestModel requestModel, {
- SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
- bool noSSL = false,
Implementation
Future<(HttpResponse?, Duration?, String?)> sendHttpRequestV1(
String requestId,
APIType apiType,
HttpRequestModel requestModel, {
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
bool noSSL = false,
}) async {
final authData = requestModel.authModel;
if (httpClientManager.wasRequestCancelled(requestId)) {
httpClientManager.removeCancelledRequest(requestId);
}
final client = httpClientManager.createClient(requestId, noSSL: noSSL);
HttpRequestModel authenticatedRequestModel = requestModel.copyWith();
try {
if (authData != null && authData.type != APIAuthType.none) {
authenticatedRequestModel = await handleAuth(requestModel, authData);
}
} catch (e) {
return (null, null, e.toString());
}
(Uri?, String?) uriRec = getValidRequestUri(
authenticatedRequestModel.url,
authenticatedRequestModel.enabledParams,
defaultUriScheme: defaultUriScheme,
);
if (uriRec.$1 != null) {
Uri requestUrl = uriRec.$1!;
Map<String, String> headers = authenticatedRequestModel.enabledHeadersMap;
bool overrideContentType = false;
HttpResponse? response;
String? body;
try {
Stopwatch stopwatch = Stopwatch()..start();
if (apiType == APIType.rest) {
var isMultiPartRequest =
requestModel.bodyContentType == ContentType.formdata;
if (kMethodsWithBody.contains(authenticatedRequestModel.method)) {
var requestBody = authenticatedRequestModel.body;
if (requestBody != null &&
!isMultiPartRequest &&
requestBody.isNotEmpty) {
body = requestBody;
if (authenticatedRequestModel.hasContentTypeHeader) {
overrideContentType = true;
} else {
headers[HttpHeaders.contentTypeHeader] =
authenticatedRequestModel.bodyContentType.header;
}
}
if (isMultiPartRequest) {
var multiPartRequest = http.MultipartRequest(
authenticatedRequestModel.method.name.toUpperCase(),
requestUrl,
);
multiPartRequest.headers.addAll(headers);
for (var formData in authenticatedRequestModel.formDataList) {
if (formData.type == FormDataType.text) {
multiPartRequest.fields.addAll({formData.name: formData.value});
} else {
multiPartRequest.files.add(
await http.MultipartFile.fromPath(
formData.name,
formData.value,
),
);
}
}
http.StreamedResponse multiPartResponse = await client.send(
multiPartRequest,
);
stopwatch.stop();
http.Response convertedMultiPartResponse =
await convertStreamedResponse(multiPartResponse);
return (convertedMultiPartResponse, stopwatch.elapsed, null);
}
}
switch (authenticatedRequestModel.method) {
case HTTPVerb.get:
response = await client.get(requestUrl, headers: headers);
break;
case HTTPVerb.head:
response = await client.head(requestUrl, headers: headers);
break;
case HTTPVerb.post:
case HTTPVerb.put:
case HTTPVerb.patch:
case HTTPVerb.delete:
case HTTPVerb.options:
final request = prepareHttpRequest(
url: requestUrl,
method: authenticatedRequestModel.method.name.toUpperCase(),
headers: headers,
body: body,
overrideContentType: overrideContentType,
);
final streamed = await client.send(request);
response = await http.Response.fromStream(streamed);
break;
}
}
if (apiType == APIType.graphql) {
var requestBody = getGraphQLBody(authenticatedRequestModel);
if (requestBody != null) {
var contentLength = utf8.encode(requestBody).length;
if (contentLength > 0) {
body = requestBody;
headers[HttpHeaders.contentLengthHeader] = contentLength.toString();
if (!authenticatedRequestModel.hasContentTypeHeader) {
headers[HttpHeaders.contentTypeHeader] = ContentType.json.header;
}
}
}
response = await client.post(requestUrl, headers: headers, body: body);
}
stopwatch.stop();
return (response, stopwatch.elapsed, null);
} catch (e) {
if (httpClientManager.wasRequestCancelled(requestId)) {
return (null, null, kMsgRequestCancelled);
}
return (null, null, e.toString());
} finally {
httpClientManager.closeClient(requestId);
}
} else {
return (null, null, uriRec.$2);
}
}