npgDefaultHttpGet function

Future<NpgHttpResponse> npgDefaultHttpGet(
  1. Uri url,
  2. Map<String, String> headers
)

Default NpgHttpGet: a plain dart:io GET, so the package stays dependency-free.

Exported so you can wrap it — for instance to log the call — instead of reimplementing it:

NexiNpgPayment(
  hostname: hostname,
  apiKey: apiKey,
  httpGet: (url, headers) async {
    final response = await npgDefaultHttpGet(url, headers);
    debugPrint('NPG order lookup: ${response.statusCode}');
    return response;
  },
);

Implementation

Future<NpgHttpResponse> npgDefaultHttpGet(
  Uri url,
  Map<String, String> headers,
) async {
  final client = HttpClient()..connectionTimeout = const Duration(seconds: 10);
  try {
    return await _get(client, url, headers).timeout(
      npgHttpTimeout,
      onTimeout: () {
        // Drop the socket instead of leaving the request in flight: the
        // caller has already given up on it.
        client.close(force: true);
        throw TimeoutException('NPG orders API did not answer', npgHttpTimeout);
      },
    );
  } finally {
    client.close(force: true);
  }
}