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

Update get_eurostat_json.R #262

Merged
merged 7 commits into from
Jun 20, 2023
Merged
Changes from 4 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
62 changes: 43 additions & 19 deletions R/get_eurostat_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ get_eurostat_json <- function(id,
message("You have no access to ec.europe.eu.
Please check your connection and/or review your proxy settings")
} else {

msg <- ". Some datasets are not accessible via the eurostat
interface. You can try to search the data manually from the comext
database at http://epp.eurostat.ec.europa.eu/newxtweb/ or bulk
download facility at
http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing
or annual Excel files
http://ec.europa.eu/eurostat/web/prodcom/data/excel-files-nace-rev.2"
ake123 marked this conversation as resolved.
Show resolved Hide resolved
# get response
# url <- try(eurostat_json_url(id = id, filters = filters, lang = lang))
# if (class(url) == "try-error") { stop(paste("The requested data set cannot be found with the following specifications to get_eurostat_json function: ", "id: ", id, "/ filters: ", filters, "/ lang: ", lang)) }
Expand All @@ -115,40 +121,58 @@ get_eurostat_json <- function(id,
# resp <- try(httr::GET(url, ...))
# if (class(resp) == "try-error") { stop(paste("The requested url cannot be found within the get_eurostat_json function:", url)) }
resp <- httr::RETRY("GET", url, terminate_on = c(404))

if (httr::http_error(resp)) {
stop(paste("The requested url cannot be found within the get_eurostat_json function:", url))
# stop(paste("The requested url cannot be found within the get_eurostat_json function:
# Client Error - 100 No results found
# Description - The requested resource is not available.", url))
#
stop(paste("Status : ",httr::content(resp)$error[[1]]$status,
", error id : ",httr::content(resp)$error[[1]]$id,"No results found",
", label : ", httr::content(resp)$error[[1]]$label),"\n",
msg)
}
}

status <- httr::status_code(resp)

# check status and get json

msg <- ". Some datasets are not accessible via the eurostat
interface. You can try to search the data manually from the comext
database at http://epp.eurostat.ec.europa.eu/newxtweb/ or bulk
download facility at
http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing
or annual Excel files
http://ec.europa.eu/eurostat/web/prodcom/data/excel-files-nace-rev.2"
# msg <- ". Some datasets are not accessible via the eurostat
# interface. You can try to search the data manually from the comext
# database at http://epp.eurostat.ec.europa.eu/newxtweb/ or bulk
# download facility at
# http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing
# or annual Excel files
# http://ec.europa.eu/eurostat/web/prodcom/data/excel-files-nace-rev.2"
ake123 marked this conversation as resolved.
Show resolved Hide resolved

if (status == 200) {
jdat <- jsonlite::fromJSON(url)
} else if (status == 400) {
stop(
"Failure to get data. Probably invalid dataset id. Status code: ",
status, msg
)

stop(paste(paste("Status : ",httr::content(resp)$error[[1]]$status,"Bad request",
if(httr::content(resp)$error[[1]]$id ==100){paste(", error id : ",httr::content(resp)$error[[1]]$id , "No result found")}
else if(httr::content(resp)$error[[1]]$id ==140){paste(", error id : ",httr::content(resp)$error[[1]]$id , "Syntax error")}
else if(httr::content(resp)$error[[1]]$id ==150){paste(", error id : ",httr::content(resp)$error[[1]]$id , "Semantic error")},
#", error id : ",httr::content(resp)$error[[1]]$id,
", label : ", httr::content(resp)$error[[1]]$label,"\n",
msg)))
pitkant marked this conversation as resolved.
Show resolved Hide resolved
} else if (status == 500) {
stop("Failure to get data. Probably filters did not return any data
or data exceeded query size limitation. Status code: ", status, msg)

stop(paste("Status : ",httr::content(resp)$error[[1]]$status,"Internal Server error",
", error id : ",httr::content(resp)$error[[1]]$id," Internal Server error",
", label : ", httr::content(resp)$error[[1]]$label),"\n",
msg)
} else if (status == 416) {
stop(
"Too many categories have been requested. Maximum is 50.",
status, msg
status,
msg
)
} else {
stop("Failure to get data. Status code: ", status, msg)
stop(paste("Failure to get data. Status code: ",msg))
}


# get json data
# dims <- jdat[[1]]$dimension # Was called like this in API v1.1
Expand Down Expand Up @@ -188,7 +212,7 @@ get_eurostat_json <- function(id,

tibble::as_tibble(dat)
}
}




Expand Down