getPrayerTimesByGps method

Future<SolatV2> getPrayerTimesByGps(
  1. double latitude,
  2. double longitude, {
  3. int? year,
  4. int? month,
})

Fetches prayer times based on GPS coordinates, optionally for a given year and month.

Corresponds to the /v2/solat/gps/{lat}/{long} endpoint.

Parameters:

  • latitude: The latitude coordinate.
  • longitude: The longitude coordinate.
  • year: Optional. The year for which to fetch prayer times (e.g., 2024). If omitted, the API defaults to the current year.
  • month: Optional. The month for which to fetch prayer times (1-12). If omitted, the API defaults to the current month.

Returns a Future<SolatV2> containing the prayer times and metadata upon success. Throws WaktuSolatApiException on failure (e.g., invalid coordinates, network error).

Implementation

Future<SolatV2> getPrayerTimesByGps(double latitude, double longitude,
    {int? year, int? month}) async {
  final Map<String, String> queryParameters = {};
  if (year != null) queryParameters['year'] = year.toString();
  if (month != null) queryParameters['month'] = month.toString();

  final endpoint = '/v2/solat/$latitude/$longitude';

  try {
    final jsonResponse = await _getRequest(endpoint,
        queryParameters: queryParameters.isEmpty ? null : queryParameters);
    // Use the same SolatV2 model, as the response structure is expected to be identical
    return SolatV2.fromJson(jsonResponse as Map<String, dynamic>);
  } on FormatException catch (e) {
    // Catch FormatExceptions specifically from SolatV2.fromJson
    throw WaktuSolatApiException('Failed to parse SolatV2 response: $e');
  } catch (e) {
    // Rethrow WaktuSolatApiException from _getRequest or other unexpected errors
    rethrow;
  }
}