provokateurin / osm-nominatim

https://pub.dev/packages/osm_nominatim
GNU General Public License v3.0
10 stars 11 forks source link

[Feature Request] User-Agent identifying the application #12

Open wildsurfer opened 1 week ago

wildsurfer commented 1 week ago

According to Nominatim Usage Policy every client should provide a valid HTTP Referer or User-Agent identifying the application.

image

Currently, this lib doesn't provide a way to configure request headers.

MuhammadAhmadSharif commented 6 days ago

Please update the nominatim classs this class include the request headers

class Nominatim { static Future<List> searchByName({ String? query, String? street, String? city, String? county, String? state, String? country, String? postalCode, bool addressDetails = false, bool extraTags = false, bool nameDetails = false, String? language, List? countryCodes, List? excludePlaceIds, int limit = 10, ViewBox? viewBox, String host = 'nominatim.openstreetmap.org', Map<String, String>? headers, // Added headers parameter }) async { if (query == null) { assert( street != null || city != null || county != null || state != null || country != null || postalCode != null, 'Any search parameter is needed for a structured request', ); } else { assert( street == null && city == null && county == null && state == null && country == null && postalCode == null, 'Do not use query and any other search parameter together', ); } assert(limit > 0, 'Limit has to be greater than zero'); assert(limit <= 50, 'Limit has to be smaller or equals than 50'); final uri = Uri.https( host, '/search', { 'format': 'jsonv2', if (query != null) 'q': query, if (street != null) 'street': street, if (city != null) 'city': city, if (county != null) 'county': county, if (state != null) 'state': state, if (country != null) 'country': country, if (postalCode != null) 'postalcode': postalCode, if (addressDetails) 'addressdetails': '1', if (extraTags) 'extratags': '1', if (nameDetails) 'namedetails': '1', if (language != null) 'accept-language': language, if (countryCodes != null && countryCodes.isNotEmpty) 'countrycodes': countryCodes.join(','), if (excludePlaceIds != null && excludePlaceIds.isNotEmpty) 'exclude_place_ids': excludePlaceIds.join(','), if (limit != 10) 'limit': limit.toString(), if (viewBox != null) 'viewbox': '${viewBox.westLongitude},${viewBox.southLatitude},${viewBox.eastLongitude},${viewBox.northLatitude}', if (viewBox != null) 'bounded': '1', }, ); final response = await http.get(uri, headers: headers); final data = json.decode(response.body) as List; return data .map((p) => Place.fromJson(p as Map<String, dynamic>)) .toList(); }

static Future reverseSearch({ double? lat, double? lon, String? osmType, int? osmId, bool addressDetails = false, bool extraTags = false, bool nameDetails = false, String? language, int zoom = 18, String host = 'nominatim.openstreetmap.org', Map<String, String>? headers, // Added headers parameter }) async { final notNullParameters = [lat, lon, osmType, osmId].where((e) => e != null).length; assert( notNullParameters == 2, 'Either provide lat and lon or osmType and osmId', ); assert( (lat != null && lon != null && osmType == null && osmId == null) || (lat == null && lon == null && osmType != null && osmId != null), 'Do not mix coordinates and OSM object', ); assert( ['N', 'W', 'R', null].contains(osmType), 'osmType needs to be one of N, W, R', ); assert( zoom >= 0 && zoom <= 18, 'Zoom needs to be between 0 and 18', ); final uri = Uri.https( host, '/reverse', { 'format': 'jsonv2', 'zoom': zoom.toString(), if (lat != null) 'lat': lat.toString(), if (lon != null) 'lon': lon.toString(), if (osmType != null) 'osm_type': osmType, if (osmId != null) 'osm_id': osmId.toString(), if (addressDetails) 'addressdetails': '1', if (extraTags) 'extratags': '1', if (nameDetails) 'namedetails': '1', if (language != null) 'accept-language': language, }, ); final response = await http.get(uri, headers: headers); final data = json.decode(response.body) as Map<String, dynamic>; if (data['error'] != null) { throw Exception(data['error']); } return Place.fromJson(data); } }