post method

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

Make a POST request with timeout and error handling

Implementation

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

  try {
    PaygateLogger.apiRequest('POST', url.toString(),
        body is Map ? body as Map<String, dynamic> : null);

    final response = await _client
        .post(
          url,
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'User-Agent': 'FlutterPayGateGlobal/0.1.5',
            ...?headers,
          },
          body: body,
        )
        .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,
    );
  }
}