get method

Future<IpGeoLocation> get(
  1. IpAddress ip, {
  2. String? language,
})

Returns the IpGeoLocation associated with the provided IpAddress.

Implementation

Future<IpGeoLocation> get(IpAddress ip, {String? language}) async {
  final Dio dio = await _dio;

  final String endpoint;
  if (Config.geoEndpoint.startsWith('/')) {
    endpoint = '${Config.origin}${Config.geoEndpoint}';
  } else {
    endpoint = Config.geoEndpoint;
  }

  final Response response = await dio.get(
    '$endpoint/$ip${language == null ? '' : '?lang=$language'}',
  );

  if (response.statusCode != 200 ||
      response.data is! Map<String, dynamic> ||
      response.data?['country'] == null ||
      response.data?['countryCode'] == null ||
      response.data?['city'] == null) {
    throw Exception(
      'Failed to fetch geo information. Status code: ${response.statusCode}. Data: ${response.data}',
    );
  }

  return IpGeoLocation.fromJson(response.data);
}