httpAPICall method

dynamic httpAPICall (
  1. dynamic url,
  2. dynamic options,
  3. [GatewayCallbacks callbacks]
)

Implementation

static httpAPICall(url, options, [GatewayCallbacks callbacks]) {
  int timeout = 60;
  Future<http.Response> fetching;
  final jsonEncoder = JsonEncoder();

  Map<String, String> fetchOptions = {
    // 'headers': 'Accept': 'application/json, text/plain, */*',
    'cache': 'no-cache'
  };
  Janus.debug(options.toString());
  if (options['withCredentials']) {
    if ((options['withCredentials']).length > 0) {
      fetchOptions['credentials'] = 'include';
    } else {
      fetchOptions['credentials'] = 'omit';
    }
  }

  if (options['timeout'] != null) {
    timeout = options['timeout'];
  }
  if (options['verb'] == "GET" || options['verb'] == 'get') {
    fetching = http.get(url, headers: fetchOptions);
  }
  if (options['verb'] == "POST" || options['verb'] == 'post') {
    String body;
    if (options['body'] != null) {
      body = jsonEncoder.convert(options['body']);
    }

    // fetchOptions['headers']['Content-Type'] = 'application/json';
    fetching = http.post(url, headers: fetchOptions, body: body);
  }

  fetching
      .timeout(Duration(seconds: timeout),
          onTimeout: () =>
              Janus.error('Request timed out: ' + timeout.toString()))
      .then((response) {
    if (response.statusCode == 200) {
      if (callbacks.success is Function) {
        callbacks.success(jsonDecode(response.body));
      }
    } else {
      callbacks.error(
          'API call failed ', response.statusCode.toString() + response.body);
    }
  }).catchError((error, StackTrace stackTrace) {
    Janus.error('Internal Error', error);
    Janus.debug(options);
  });
}