call method

Future<Result> call({
  1. dynamic body,
  2. Map<String, dynamic>? parameters,
  3. Map<String, String>? headers,
  4. String network = 'main',
})

main points to default Network object.

Example:

await Route.get('location.search').call(
parameters: {'location': 'hamburg','language': 'en','country': 'de',},
headers: {
OdinNetworkHeader.acceptEncoding: OdinNetworkHeader.applicationGzip,
OdinNetworkHeader.xRapidApiKey:'<API-KEY>',
});

Implementation

//OdinNetworkHeader.xRapidApiHost: 'foreca-weather.p.rapidapi.com'
///});
///```
Future<Result> call({
  dynamic body,
  Map<String, dynamic>? parameters,
  Map<String, String>? headers,
  String network = 'main',
}) async {
  try {
    if (simulated != null) {
      return await simulated!.handle(Request(
          route: this, parameters: parameters, body: body, headers: headers));
    }

    Network currentNetwork = Network.networks[network]!;

    String formatedPath = path;

    if (parameters != null) {
      for (String parameterKey in parameters.keys) {
        formatedPath = formatedPath.replaceAll(
            '{$parameterKey}', parameters[parameterKey]);
      }
    }

    final Uri uri = Uri.parse('${currentNetwork.getRoot()}/$formatedPath');

    headers ??= {};

    String cookies = Service.get<CookieService>().convertCookiesToString();

    Map<String, dynamic> networkHeaders = currentNetwork.getHeaders(this);

    networkHeaders.forEach((key, value) {
      headers![key] = value;
    });

    if (cookies.isNotEmpty) {
      headers[OdinNetworkHeader.cookie] = cookies;
    }

    if (body != null) {
      if (body is String) {
      } else if (body is FormData) {
        headers[OdinNetworkHeader.contentType] =
            OdinNetworkHeader.applicationJson;
      } else {
        headers[OdinNetworkHeader.contentType] =
            OdinNetworkHeader.applicationJson;

        body = json.encode(body);
      }
    }

    BaseOptions baseOptions = BaseOptions(
      connectTimeout: 10000,
      receiveTimeout: 10000,
      sendTimeout: 10000,
      followRedirects: true,
      validateStatus: (status) => true,
      receiveDataWhenStatusError: true,
      headers: headers,
    );

    if (dio == null) {
      if (kIsWeb) {
        dio = Dio(baseOptions);
      } else {
        dio = Dio(baseOptions);
      }
    }

    Response? response;

    Options options = Options(headers: headers);

    if (method == Method.post) {
      response = await dio!.postUri(uri, data: body, options: options);
    } else if (method == Method.get) {
      response = await dio!.getUri(uri, options: options);
    } else if (method == Method.patch) {
      response = await dio!.patchUri(uri, options: options, data: body);
    } else if (method == Method.put) {
      response = await dio!.putUri(uri, options: options, data: body);
    } else if (method == Method.delete) {
      response = await dio!.deleteUri(uri, options: options, data: body);
    } else if (method == Method.head) {
      response = await dio!.headUri(uri, options: options, data: body);
    }

    if (response == null) {
      return Result(statusCode: 404);
    }

    await Service.get<CookieService>().updateCookies(response.headers.map);

    Result result = Result(
      statusCode: response.statusCode ?? 200,
      data: response.data,
    );

    return result;
  } catch (e) {
    print(e);
  }

  return Result(statusCode: 404);
}