sendHttpRequest method

Future<Response> sendHttpRequest(
  1. RequestType type,
  2. String urlString,
  3. Map<String, String> headers,
  4. Object? data, {
  5. dynamic timeoutMs = TIMEOUT,
})

Implementation

Future<Response> sendHttpRequest(RequestType type, String urlString,
    Map<String, String> headers, Object? data,
    {timeoutMs = TIMEOUT}) async {
  var url = Uri.parse(urlString);
  switch (type) {
    case RequestType.Post:
      {
        Flagship.logger(Level.INFO, data.toString(), isJsonString: true);

        try {
          var response = await this
              .httpClient
              .post(url, body: data, headers: headers)
              .timeout(Duration(milliseconds: timeoutMs));
          return response;
        } on TimeoutException catch (e) {
          Flagship.logger(
              Level.INFO, REQUEST_TIMEOUT.replaceFirst("%s", urlString));
          return Response(e.toString(), 408);
        } on Error catch (e) {
          Flagship.logger(
              Level.INFO, REQUEST_ERROR.replaceFirst("%s", urlString));
          return Response(e.toString(), 400);
        } on SocketException catch (error) {
          Flagship.logger(
              Level.INFO, REQUEST_ERROR.replaceFirst("%s", error.toString()));
          return Response(error.toString(), 400);
        }
      }
    case RequestType.Get:
      try {
        var response = await this.httpClient.get(url, headers: headers);
        return response;
      } on Error catch (e) {
        return Response(e.toString(), 400);
      }
    default:
      return Response('Error', 400);
  }
}