get method

Future<Response> get(
  1. Uri url, {
  2. Map<String, String>? headers,
  3. Duration? timeout,
})

Make a GET request with timeout and error handling

Implementation

Future<http.Response> get(
  Uri url, {
  Map<String, String>? headers,
  Duration? timeout,
}) async {
  final effectiveTimeout = timeout ?? _defaultTimeout;

  try {
    PaygateLogger.apiRequest('GET', url.toString(), null);

    final response = await _client.get(
      url,
      headers: {
        'User-Agent': 'FlutterPayGateGlobal/0.1.5',
        ...?headers,
      },
    ).timeout(effectiveTimeout);

    PaygateLogger.apiResponse(url.toString(), response.statusCode,
        response.body.length > 1000 ? 'Large response body' : response.body);

    return response;
  } on SocketException catch (e) {
    PaygateLogger.error('Network error: ${e.message}', e);
    throw PaygateNetworkException(
      'Network connection failed: ${e.message}',
      originalError: e,
    );
  } on HttpException catch (e) {
    PaygateLogger.error('HTTP error: ${e.message}', e);
    throw PaygateNetworkException(
      'HTTP request failed: ${e.message}',
      originalError: e,
    );
  } on TimeoutException catch (e) {
    PaygateLogger.error('Request timeout: ${e.message}', e);
    throw PaygateNetworkException(
      'Request timed out after ${effectiveTimeout.inSeconds} seconds',
      originalError: e,
    );
  } catch (e) {
    PaygateLogger.error('Unexpected error during HTTP request', e);
    throw PaygateNetworkException(
      'Unexpected network error: $e',
      originalError: e,
    );
  }
}