Search in sources :

Example 1 with ApiRequestWriter

use of de.westnordost.osmapi.ApiRequestWriter in project StreetComplete by westnordost.

the class OverpassMapDataDao method get.

/**
 * Feeds map data to the given MapDataWithGeometryHandler.
 *
 * @param query Query string. Either Overpass QL or Overpass XML query string
 * @param handler map data handler that is fed the map data and geometry
 *
 * @throws OsmTooManyRequestsException if the user is over his request quota. See getStatus, killMyQueries
 * @throws OsmBadUserInputException if there is an error if the query
 */
public synchronized void get(final String query, MapDataWithGeometryHandler handler) {
    OverpassMapDataParser parser = parserProvider.get();
    parser.setHandler(handler);
    try {
        ApiRequestWriter writer = new ApiRequestWriter() {

            @Override
            public String getContentType() {
                return "application/x-www-form-urlencoded";
            }

            @Override
            public void write(OutputStream out) throws IOException {
                String request = "data=" + urlEncode(query);
                out.write(request.getBytes("UTF-8"));
            }
        };
        osm.makeRequest("interpreter", "POST", false, writer, parser);
    } catch (OsmApiException e) {
        if (e.getErrorCode() == 429)
            throw new OsmTooManyRequestsException(e.getErrorCode(), e.getErrorTitle(), e.getDescription());
        else
            throw e;
    }
}
Also used : ApiRequestWriter(de.westnordost.osmapi.ApiRequestWriter) OsmApiException(de.westnordost.osmapi.common.errors.OsmApiException) OutputStream(java.io.OutputStream)

Aggregations

ApiRequestWriter (de.westnordost.osmapi.ApiRequestWriter)1 OsmApiException (de.westnordost.osmapi.common.errors.OsmApiException)1 OutputStream (java.io.OutputStream)1