Skip to content

Commit

Permalink
Refactor internal api_call() interface
Browse files Browse the repository at this point in the history
  • Loading branch information
aoles committed May 23, 2019
1 parent 5d8963e commit 1c7ddcb
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: openrouteservice
Title: Openrouteservice API Client
Version: 0.2.7
Version: 0.3.0
Authors@R: person("Andrzej", "Oleś", email = "[email protected]", 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.
Expand Down
12 changes: 11 additions & 1 deletion R/api_call.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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="/")

Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions R/directions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions R/elevation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions R/geocode.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 2 additions & 4 deletions R/isochrones.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions R/matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions R/pois.R
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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)
}

0 comments on commit 1c7ddcb

Please sign in to comment.