diff --git a/DESCRIPTION b/DESCRIPTION index aa0cf2d..7611ba7 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: openrouteservice Title: Openrouteservice API Client -Version: 0.2.7 +Version: 0.3.0 Authors@R: person("Andrzej", "OleÅ›", email = "andrzej@openrouteservice.org", comment = c(ORCID = "0000-0003-0285-2787"), role = c("aut", "cre")) Description: The package streamlines access to the services provided by openrouteservice.org. It allows you to painlessly query for directions, geocoding, isochrones, time-distance matrices, and POIs. diff --git a/R/api_call.R b/R/api_call.R index 3ae0a14..e1eb8e4 100755 --- a/R/api_call.R +++ b/R/api_call.R @@ -95,7 +95,7 @@ ors_path <- function(endpoint) { } #' @importFrom httr modify_url parse_url verbose -api_call <- function(method, path, query, ..., +api_call <- function(path, api_key, query = NULL, body = NULL, ..., response_format = c("json", "geojson", "gpx"), output, simplifyMatrix = TRUE) { @@ -104,6 +104,10 @@ api_call <- function(method, path, query, ..., endpoint <- basename(path[1L]) path[1L] <- ors_path(endpoint) + ## process query params + if (!is.null(query)) + query <- api_query(api_key, query, collapse = ",") + ## extract base path from url in order to retain it in modify_url(), see #46 path <- paste0(parse_url(ors_url())$path, path, collapse="/") @@ -117,6 +121,12 @@ api_call <- function(method, path, query, ..., ..., if (isTRUE(getOption('openrouteservice.verbose'))) verbose()) + method = if (is.null(body)) "GET" else "POST" + + ## add body and authorization header + if (method=="POST") + args = c(args, list(body = body, add_headers(Authorization = api_key))) + res <- call_api(method, args) process_response(res, endpoint, output, simplifyMatrix) diff --git a/R/directions.R b/R/directions.R index 4eeff57..c3d243c 100755 --- a/R/directions.R +++ b/R/directions.R @@ -51,10 +51,8 @@ ors_directions <- function(coordinates, body <- list(coordinates = coordinates, ...) - api_call(method = "POST", - path = c("v2/directions", profile, format), - query = NULL, - add_headers(Authorization = api_key), + api_call(path = c("v2/directions", profile, format), + api_key = api_key, body = body, encode = "json", response_format = format, diff --git a/R/elevation.R b/R/elevation.R index afb438e..6f6d1f0 100644 --- a/R/elevation.R +++ b/R/elevation.R @@ -79,10 +79,8 @@ ors_elevation <- function(format_in = c("geojson", "point", "polyline", "encoded format_out = format_out, ...) - api_call(method = "POST", - path = c("elevation", endpoint), - query = NULL, - add_headers(Authorization = api_key), + api_call(path = c("elevation", endpoint), + api_key = api_key, body = body, encode = "json", output = output) diff --git a/R/geocode.R b/R/geocode.R index 37c6118..9466b59 100755 --- a/R/geocode.R +++ b/R/geocode.R @@ -63,9 +63,10 @@ ors_geocode <- function(query, } } - query <- api_query(api_key, params, collapse = ",") - output <- match.arg(output) - api_call("GET", c("geocode", endpoint), query, output = output) + api_call(c("geocode", endpoint), + api_key = api_key, + query = params, + output = output) } diff --git a/R/isochrones.R b/R/isochrones.R index 40e2fe0..38d68e5 100755 --- a/R/isochrones.R +++ b/R/isochrones.R @@ -54,10 +54,8 @@ ors_isochrones <- function(locations, body <- list(locations = locations, range = range, ...) - api_call(method = "POST", - path = c("v2/isochrones", profile), - query = NULL, - add_headers(Authorization = api_key), + api_call(path = c("v2/isochrones", profile), + api_key = api_key, body = body, encode = "json", response_format = "geojson", diff --git a/R/matrix.R b/R/matrix.R index 9dc7cbb..863fe0e 100755 --- a/R/matrix.R +++ b/R/matrix.R @@ -51,10 +51,8 @@ ors_matrix <- function(locations, ## request parameters body = list(locations = locations, profile = profile, ...) - api_call(method = "POST", - path = c("v2/matrix", profile), - query = NULL, - add_headers(Authorization = api_key), + api_call(path = c("v2/matrix", profile), + api_key = api_key, body = body, encode = "json", output = output) diff --git a/R/pois.R b/R/pois.R index c0d4424..bd8d904 100755 --- a/R/pois.R +++ b/R/pois.R @@ -66,8 +66,6 @@ ors_pois <- function(request = c('pois', 'stats', 'list'), if (request!="pois" && output=="sf") stop('"sf" output available only for request type "pois"') - query = api_query(api_key) - body = list(request = request, ...) if ( request!="list") { @@ -77,5 +75,9 @@ ors_pois <- function(request = c('pois', 'stats', 'list'), body$geometry = geometry } - api_call("POST", "pois", query, body = body, encode = "json", output = output) + api_call("pois", + api_key = api_key, + body = body, + encode = "json", + output = output) }