Skip to content

Commit 1c7ddcb

Browse files
committed
Refactor internal api_call() interface
1 parent 5d8963e commit 1c7ddcb

File tree

8 files changed

+29
-24
lines changed

8 files changed

+29
-24
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: openrouteservice
22
Title: Openrouteservice API Client
3-
Version: 0.2.7
3+
Version: 0.3.0
44
Authors@R: person("Andrzej", "Oleś", email = "andrzej@openrouteservice.org", comment = c(ORCID = "0000-0003-0285-2787"), role = c("aut", "cre"))
55
Description: The package streamlines access to the services provided by openrouteservice.org.
66
It allows you to painlessly query for directions, geocoding, isochrones, time-distance matrices, and POIs.

R/api_call.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ ors_path <- function(endpoint) {
9595
}
9696

9797
#' @importFrom httr modify_url parse_url verbose
98-
api_call <- function(method, path, query, ...,
98+
api_call <- function(path, api_key, query = NULL, body = NULL, ...,
9999
response_format = c("json", "geojson", "gpx"),
100100
output, simplifyMatrix = TRUE) {
101101

@@ -104,6 +104,10 @@ api_call <- function(method, path, query, ...,
104104
endpoint <- basename(path[1L])
105105
path[1L] <- ors_path(endpoint)
106106

107+
## process query params
108+
if (!is.null(query))
109+
query <- api_query(api_key, query, collapse = ",")
110+
107111
## extract base path from url in order to retain it in modify_url(), see #46
108112
path <- paste0(parse_url(ors_url())$path, path, collapse="/")
109113

@@ -117,6 +121,12 @@ api_call <- function(method, path, query, ...,
117121
...,
118122
if (isTRUE(getOption('openrouteservice.verbose'))) verbose())
119123

124+
method = if (is.null(body)) "GET" else "POST"
125+
126+
## add body and authorization header
127+
if (method=="POST")
128+
args = c(args, list(body = body, add_headers(Authorization = api_key)))
129+
120130
res <- call_api(method, args)
121131

122132
process_response(res, endpoint, output, simplifyMatrix)

R/directions.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ ors_directions <- function(coordinates,
5151

5252
body <- list(coordinates = coordinates, ...)
5353

54-
api_call(method = "POST",
55-
path = c("v2/directions", profile, format),
56-
query = NULL,
57-
add_headers(Authorization = api_key),
54+
api_call(path = c("v2/directions", profile, format),
55+
api_key = api_key,
5856
body = body,
5957
encode = "json",
6058
response_format = format,

R/elevation.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ ors_elevation <- function(format_in = c("geojson", "point", "polyline", "encoded
7979
format_out = format_out,
8080
...)
8181

82-
api_call(method = "POST",
83-
path = c("elevation", endpoint),
84-
query = NULL,
85-
add_headers(Authorization = api_key),
82+
api_call(path = c("elevation", endpoint),
83+
api_key = api_key,
8684
body = body,
8785
encode = "json",
8886
output = output)

R/geocode.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ ors_geocode <- function(query,
6363
}
6464
}
6565

66-
query <- api_query(api_key, params, collapse = ",")
67-
6866
output <- match.arg(output)
6967

70-
api_call("GET", c("geocode", endpoint), query, output = output)
68+
api_call(c("geocode", endpoint),
69+
api_key = api_key,
70+
query = params,
71+
output = output)
7172
}

R/isochrones.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@ ors_isochrones <- function(locations,
5454

5555
body <- list(locations = locations, range = range, ...)
5656

57-
api_call(method = "POST",
58-
path = c("v2/isochrones", profile),
59-
query = NULL,
60-
add_headers(Authorization = api_key),
57+
api_call(path = c("v2/isochrones", profile),
58+
api_key = api_key,
6159
body = body,
6260
encode = "json",
6361
response_format = "geojson",

R/matrix.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ ors_matrix <- function(locations,
5151
## request parameters
5252
body = list(locations = locations, profile = profile, ...)
5353

54-
api_call(method = "POST",
55-
path = c("v2/matrix", profile),
56-
query = NULL,
57-
add_headers(Authorization = api_key),
54+
api_call(path = c("v2/matrix", profile),
55+
api_key = api_key,
5856
body = body,
5957
encode = "json",
6058
output = output)

R/pois.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ ors_pois <- function(request = c('pois', 'stats', 'list'),
6666
if (request!="pois" && output=="sf")
6767
stop('"sf" output available only for request type "pois"')
6868

69-
query = api_query(api_key)
70-
7169
body = list(request = request, ...)
7270

7371
if ( request!="list") {
@@ -77,5 +75,9 @@ ors_pois <- function(request = c('pois', 'stats', 'list'),
7775
body$geometry = geometry
7876
}
7977

80-
api_call("POST", "pois", query, body = body, encode = "json", output = output)
78+
api_call("pois",
79+
api_key = api_key,
80+
body = body,
81+
encode = "json",
82+
output = output)
8183
}

0 commit comments

Comments
 (0)