APIRequest constructor

APIRequest({
  1. String? url,
  2. String? path,
  3. Map<String, String>? headers,
  4. Map<String, dynamic>? payload,
  5. String method = 'GET',
})

Implementation

APIRequest(
    {String? url,
    String? path,
    Map<String, String>? headers,
    Map<String, dynamic>? payload,
    String method = 'GET'}) {
  _headers = {..._defaultHeaders};
  if (headers != null) {
    _headers = {...headers};
  }
  if (url != null && path != null) {
    throw Exception('Only url or path is accepted.');
  }
  if (url == null && path == null) {
    throw Exception('one of url or path is required.');
  } else {
    if (base == null && path != null) {
      throw Exception(
          'Cannot proceed to request uri when base is not set. Please set base first in order to request using uri.');
    }
  }
  if (path != null) {
    _url = base! + ((path[0] == '/') ? path.substring(1) : path);
  }
  if (url != null) _url = url;
  if (!_acceptedMethods.containsKey(method)) {
    throw Exception("Unsupported method '$method'.");
  }
  _method = method;
  _payload = payload;
}