from static method

DataSourceOperationHttp? from(
  1. Object? config
)

Implementation

static DataSourceOperationHttp? from(Object? config) {
  if (config == null) return null;

  if (config is String) {
    config = parseJSON(config);
  }

  if (config is Map) {
    config.removeWhere((key, value) => value == null);

    var operation = getDataSourceOperation(parseString(
        parseString(findKeyValue(config, ['operation', 'op'], true))));

    var baseURL = parseString(
        parseString(findKeyValue(config, ['baseurl', 'url'], true)));
    if (baseURL != null) {
      baseURL = resolveDataSourceBaseURL(baseURL);
    }

    var baseURLProxy =
        parseString(findKeyValue(config, ['baseurlproxy', 'urlproxy'], true));

    if (baseURLProxy != null) {
      baseURLProxy = resolveDataSourceBaseURL(baseURLProxy);
    }

    var method =
        getHttpMethod(parseString(findKeyValue(config, ['method'], true)));
    var path = parseString(findKeyValue(config, ['path'], true));
    var fullPath = parseBool(findKeyValue(config, ['fullPath'], true));
    var parameters =
        findKeyValue(config, ['parameters', 'args', 'properties'], true)
            as Map;
    var body = findKeyValue(config, ['body', 'content', 'payload'], true);

    var jsonTransformer = findKeyValue(
        config,
        ['jsonTransformer', 'transformResponse', 'transform', 'transformer'],
        true);

    var samples = findKeyValue(config, ['samples'], true);

    Map<String, dynamic>? samplesMap;
    if (samples is Map) {
      samplesMap = Map<String, dynamic>.fromEntries(
          samples.entries.map((e) => MapEntry('${e.key}', e.value)));
    }

    return DataSourceOperationHttp(operation,
        baseURL: baseURL,
        baseURLProxy: baseURLProxy,
        method: method ?? HttpMethod.GET,
        path: path ?? '',
        fullPath: fullPath ?? false,
        parameters: parameters,
        body: body,
        jsonTransformer: jsonTransformer,
        samples: samplesMap);
  }

  return null;
}