Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add compatibility for api version 3 #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Imports:
Rcpp
License: GPL-3
LazyData: true
RoxygenNote: 6.1.0
RoxygenNote: 7.1.1
LinkingTo: Rcpp, rapidjsonr
Suggests: knitr,
testthat
18 changes: 12 additions & 6 deletions R/get_coords.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,24 @@ bmap_get_coords <- function(location, type = c("data.frame", "json"),
envir = bmap_env)

# If API key is invalid, throw error.
if (grepl("message", res) && !grepl('\"lng\"', res)) {
if (invalid_key(res)) {
stop(invalid_key_msg(res), call. = FALSE)
}

# If API response contains "APP 服务被禁用", try flipping the API version
# and making the call again. If that returns "APP 服务被禁用", throw
# error.
if (key_api_version_incompatible(res)) {
flip_api_version()
res <- baidu_coord_query(location[x])
if (key_api_version_incompatible(res)) {
stop(key_api_version_incompatible_msg(), call. = FALSE)
}
}

# Assign res to output vector.
out[x] <- res

# If API key is invalid, throw error.
if (grepl("message", res) && !grepl('\"lng\"', res)) {
stop(invalid_key_msg(res), call. = FALSE)
}

# If there was a connection error or http status code 302, do not cache
# the result to addr_hash_map.
if (grepl('con error:|"status\\":302', res)) {
Expand Down
14 changes: 13 additions & 1 deletion R/get_location.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,22 @@ bmap_get_location <- function(lat, lon, type = c("data.frame", "json"),
envir = bmap_env)

# If API key is invalid, throw error.
if (grepl("message", res) && !grepl('\"lng\"', res)) {
if (invalid_key(res)) {
stop(invalid_key_msg(res), call. = FALSE)
}

# If API response contains "APP 服务被禁用", try flipping the API version
# and making the call again. If that returns "APP 服务被禁用", throw
# error.
if (key_api_version_incompatible(res)) {
flip_api_version()
uri <- get_addr_query_uri(lon[x], lat[x])
res <- baidu_location_query(uri)
if (key_api_version_incompatible(res)) {
stop(key_api_version_incompatible_msg(), call. = FALSE)
}
}

# Assign res to output vector.
out[x] <- res

Expand Down
98 changes: 84 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,96 @@
#'
#' @noRd
get_coords_query_uri <- function(location) {
paste0(
"http://api.map.baidu.com/geocoder/v2/?address=",
location,
"&output=json&ak=",
"%s"
)
api_version <- get_api_version()
if (api_version == "v2") {
uri <- paste0(
"http://api.map.baidu.com/geocoder/v2/?address=",
location,
"&output=json&ak=",
"%s"
)
} else {
uri <- paste0(
"http://api.map.baidu.com/geocoding/v3/?address=",
location,
"&output=json&ak=",
"%s"
)
}
uri
}


#' Get URI for an address API query
#'
#' @noRd
get_addr_query_uri <- function(lon, lat) {
api_version <- get_api_version()
if (api_version == "v2") {
uri <- paste0(
"http://api.map.baidu.com/geocoder/v2/?ak=",
"%s",
"&location=",
lat,
",",
lon,
"&output=json&pois=0"
)
} else {
uri <- paste0(
"http://api.map.baidu.com/reverse_geocoding/v3/?ak=",
"%s",
"&location=",
lat,
",",
lon,
"&output=json&coordtype=wgs84ll"
)
}
uri
}

#' Get API version, either v2 or v3
#'
#' @noRd
get_api_version <- function() {
get("api_key_version", envir = bmap_env)
}

#' Flip API version, between v2 and v3
#'
#' @noRd
flip_api_version <- function() {
curr <- get_api_version()
if (curr == "v2") {
assign("api_key_version", "v3", envir = bmap_env)
} else {
assign("api_key_version", "v2", envir = bmap_env)
}
}

#' Check to see if a json string response indicates the API key is not valid
#'
#' @noRd
invalid_key <- function(res) {
grepl('\"message\":\"APP不存在,AK有误请检查再重试\"', res)
}

#' Check to see if a json string response indicates that an API key is not
#' compatible with the current API version, as indicated by get_api_version()
#'
#' @noRd
key_api_version_incompatible <- function(res) {
grepl("APP 服务被禁用", res) && grepl('\"status\":240', res)
}

#' Error message indiating an API key is incompatible with both v2 and v3
#' of the Baidu API
#'
#' @noRd
key_api_version_incompatible_msg <- function() {
paste0(
"http://api.map.baidu.com/geocoder/v2/?ak=",
"%s",
"&location=",
lat,
",",
lon,
"&output=json&pois=0"
)
"Current API key is incompatible with both v2 and v3 of the Baidu API.\n",
missing_key_msg()
)
}
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ assign("bmap_daily_rate_limit", 5950L, envir = bmap_env)
assign("queries_left_today", 5950L, envir = bmap_env)
assign("next_limit_reset", NULL, envir = bmap_env)
assign("time_of_last_query", Sys.time(), envir = bmap_env)
assign("api_key_version", "v2", envir = bmap_env)

# Initialize placeholders for package data within bmap_env.
assign("coord_hash_map", NULL, envir = bmap_env)
Expand Down
9 changes: 7 additions & 2 deletions man/bmap_get_coords.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions man/bmap_get_location.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/test-baidugeo.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ test_that("invalid key msg is correct", {
"len of str is 3 or fewer chars")
)
})

context("flip_api_versoin")

test_that("flip_api_version correctly flips the api version env var", {
assign("api_key_version", "v3", envir = bmap_env)
flip_api_version()
expect_equal(get_api_version(), "v2")
})