generatePostmanJson static method

String generatePostmanJson(
  1. DebugModel model, {
  2. bool redact = false,
})

Implementation

static String generatePostmanJson(DebugModel model, {bool redact = false}) {
  final uri = model.uri;
  final collection = <String, dynamic>{
    'info': <String, dynamic>{
      'name': model.shortPath,
      'schema':
          'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
    },
    'item': <dynamic>[
      <String, dynamic>{
        'name': model.shortPath,
        'request': <String, dynamic>{
          'method': model.httpMethod,
          'header': model
              .requestHeadersView(redact: redact)
              .entries
              .map(
                (e) => <String, dynamic>{
                  'key': e.key,
                  'value': e.value,
                  'type': 'text',
                },
              )
              .toList(),
          'body': <String, dynamic>{
            'mode': 'raw',
            'raw': LogFormatter.pretty(
              redact
                  ? Redaction.redact(model.requestData)
                  : model.requestData,
            ),
            'options': <String, dynamic>{
              'raw': <String, dynamic>{'language': 'json'},
            },
          },
          'url': <String, dynamic>{
            'raw': redact ? Redaction.url(model.url) : model.url,
            'protocol': uri.scheme,
            'host': uri.host.split('.'),
            if (uri.hasPort) 'port': '${uri.port}',
            'path': uri.pathSegments,
            if (uri.hasQuery)
              'query': uri.queryParameters.entries
                  .map(
                    (e) => <String, dynamic>{
                      'key': e.key,
                      'value': redact && Redaction.isSensitive(e.key)
                          ? Redaction.mask
                          : e.value,
                    },
                  )
                  .toList(),
          },
        },
        'response': <dynamic>[],
      },
    ],
  };

  return LogFormatter.pretty(collection);
}