executeGenericRequest<T extends ISerializable<T>> method

Future<T?> executeGenericRequest<T extends ISerializable<T>>({
  1. required T typeResolver,
  2. required HttpMethod method,
  3. required Uri uri,
  4. dynamic body,
})

Executes a generic request with authentication to the specified Uri with specified HttpMethod and with the specified body (if any)

returns response data as T type which is specified as a generic type parameter to the function.

Implementation

Future<T?> executeGenericRequest<T extends ISerializable<T>>({required T typeResolver, required HttpMethod method, required Uri uri, dynamic body}) async {
  if (!_isInitialized) {
    callback.invokeErrorCallback('Client is not initialized yet. Try calling init()');
    return null;
  }

  try {
    Response<dynamic> response;

    if (body == null) {
      response = await _client.requestUri(
        uri,
        options: Options(
          contentType: ContentType.json.value,
          responseType: ResponseType.json,
          method: method.humanized.toUpperCase(),
        ),
      );
    } else {
      response = await _client.requestUri(
        uri,
        data: body,
        options: Options(
          contentType: ContentType.json.value,
          responseType: ResponseType.json,
          method: method.humanized.toUpperCase(),
        ),
      );
    }

    if (response.statusCode != 200) {
      return null;
    }

    return typeResolver.fromJson(response.data is String ? jsonDecode(response.data) : response.data);
  } on DioError catch (e) {
    callback.invokeRequestErrorCallback(e);
    return null;
  }
}