Commit 867ef757 authored by Jean-Marie Lepioufle's avatar Jean-Marie Lepioufle
Browse files

clean the code with the use of ecapi

parent 957605e2
Package: eipar
Type: Package
Version: 0.0.1
Version: 0.0.2
Authors@R: c(person("Jean-Marie", "Lepioufle", , "jml@nilu.no", role=c("aut","cre")))
Title: eipar
Description: Extracting air quality timeseries from the Irish Environmental Protection Agency.
......@@ -8,8 +8,8 @@ Description: Extracting air quality timeseries from the Irish Environmental Prot
Depends:
R (>= 3.2.0)
Imports:
httr,
dplyr
ecapi
Remotes: git::git@git.nilu.no:rextapi/ecapi.git
Suggests:
knitr
License: GPL-3
......
# Generated by roxygen2: do not edit by hand
export(eipar_base_url)
export(endpoints_eipar)
export(endpoints_metadata)
export(endpoints_qa)
export(endpoints_timeseries)
export(get_timeseries)
export(url_request)
S3method(print,eipar)
export(eipar)
#' eipar_base_url
#' eipar_base_url
#' @keywords eipar
#' @export
#' @examples
#' \dontrun{
#' eipar_base_url()
#' }
eipar_base_url <- function(){
res <- "https://air-tst.epa.ie"
return(res)
}
#' eipar
#'
#' eipar
#' @keywords eipar
#' @export
#' @examples
#' \dontrun{
#' eipar()
#' }
eipar <- function(){
eipar_object()
}
eipar_object <- function(){
client_api <- ecapi::ecapi()
client_api$url_auth("ask for the id key")
client_api$auth_type("basic")
client_api$ua("eipar-r-package")
client_api$base_url(host="https://air-tst.epa.ie",basepath="api/v1/")
client_api$add_endpoint(name = "measurements",
epoint = "aq_measurements",
question = "?where",
debut = "{",
end = "}",
syntaxe = "'measurementtype':<<'measurementtype'>>,'samplingpoint':<<'samplingpoint'>>,'measurementunit':<<'measurementunit'>>,'pollutantname':<<'pollutantname'>>,'raw_reading_measurement_time':<<'raw_reading_measurement_time'>>,'rawreadingid':<<rawreadingid>>"
)
object <- local({
measurements <- function(measurementtype="",measurementunit="",pollutantname="",raw_reading_measurement_time="",rawreadingid="",samplingpoint="",username,password,type){
client_api$verb_get(name="measurements",measurementtype=measurementtype,measurementunit=measurementunit,pollutantname=pollutantname,raw_reading_measurement_time=raw_reading_measurement_time,rawreadingid=rawreadingid,samplingpoint=samplingpoint,out='_items',username=username,password=password)
}
summary <- function(){
client_api$summary()
}
environment()
})
lockEnvironment(object, TRUE)
structure(object, class=c("eipar", class(object)))
}
#' endpoints_eipar
#' endpoints_eipar
#' @param api api endpoint
#' @keywords eipar
#' @export
#' @examples
#' \dontrun{
#' endpoints_eipar()
#' }
endpoints_eipar <- function(api=c("timeseries","metadata","qa")){
api <- match.arg(api,c("timeseries","metadata"))
res <- switch(api,
"timeseries" = endpoints_timeseries(),
"metadata" = endpoints_metadata(),
"qa" = endpoints_qa(),
stop("endpoints not recognized"))
class(res) <- "eipar_endpoints"
return(res)
}
#' endpoints_timeseries
#' endpoints_timeseries
#' @keywords eipar
#' @export
#' @examples
#' \dontrun{
#' endpoints_timeseries()
#' }
endpoints_timeseries <- function(){
res <- eipar_url("aq_measurements")
return(res)
}
#' endpoints_metadata
#' endpoints_metadata
#' @keywords eipar
#' @export
#' @examples
#' \dontrun{
#' endpoints_metadata()
#' }
endpoints_metadata <- function(){
res <- eipar_url("aq_meta")
return(res)
}
#' endpoints_qa
#' endpoints_qa
#' @keywords eipar
#' @export
#' @examples
#' \dontrun{
#' endpoints_qa()
#' }
endpoints_qa <- function(){
res <- eipar_url("aq_qa")
return(res)
}
eipar_url <- function(...){
url <- eipar_base_url()
param <- c(as.list(environment()), list(...))
indice <- which(unlist(lapply(param,is.null)))
if (length(indice)) {
param <- param[-indice]
}
res <- do.call("file.path",param)
return(res)
}
#' get_timeseries
#' get_timeseries
#' @param stationIDs stationIDs
#' @param componentName componentName
#' @param fromDateTime fromDateTime
#' @param toDateTime toDateTime
#' @param username username
#' @param password password
#' @keywords eipar
#' @export
#' @examples
#' \dontrun{
#' get_timeseries()
#' }
get_timeseries <- function(stationIDs=NULL,componentName=NULL,fromDateTime=NULL,toDateTime=NULL,username,password){
epoint <- endpoints_eipar(api="timeseries")
url <- url_request(epoint=epoint,stationIDs=stationIDs,componentName=componentName,fromDateTime=fromDateTime,toDateTime=toDateTime)
tmp <- httr::GET(url,httr::authenticate(user=username, password=password,type="basic"),httr::add_headers("user-agent"="eipar-r-package"))
status_code <- tmp$status_code
if (status_code == 200L) {
tmp <- httr::content(tmp)
res <- dplyr::bind_rows(lapply(tmp$'_items',function(x) data.frame(t(x))))
res <- apply(res,2,function(x) unlist(as.character(x)))
rm(tmp)
} else res <- NULL
res <- list(status_code = status_code,
resp = res)
class(res) <- c("eipar_get_timeseries",class(res))
return(res)
}
#' url_request
#' url_request
#' @param epoint epoints obkect
#' @param ... parameters
#' @keywords eipar
#' @export
#' @examples
#' \dontrun{
#' url_request()
#' }
url_request <- function(epoint,...){
if (inherits(epoint,"eipar_endpoints")){
param <- list(...)
res <-paste0(epoint,"?")
if (length(param)>0) {
param_name <- names(param)
for (i in 1:length(param_name)){
tmp <- paste0(param_name[i],"=",param[[i]])
res <- paste0(res,"&",tmp)
}
}
class(res) <- c("eipar_url",class(res))
} else stop("epoint must be an eipar_endpoints object")
return(res)
}
#' @export
print.eipar <- function(x,...){
get("summary",x)()
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/endpoints.R
\name{endpoints_qa}
\alias{endpoints_qa}
\title{endpoints_qa
endpoints_qa}
% Please edit documentation in R/eipar.R
\name{eipar}
\alias{eipar}
\title{eipar}
\usage{
endpoints_qa()
eipar()
}
\description{
endpoints_qa
endpoints_qa
eipar
}
\examples{
\dontrun{
endpoints_qa()
eipar()
}
}
\keyword{eipar}
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/base_url.R
\name{eipar_base_url}
\alias{eipar_base_url}
\title{eipar_base_url
eipar_base_url}
\usage{
eipar_base_url()
}
\description{
eipar_base_url
eipar_base_url
}
\examples{
\dontrun{
eipar_base_url()
}
}
\keyword{eipar}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/endpoints.R
\name{endpoints_eipar}
\alias{endpoints_eipar}
\title{endpoints_eipar
endpoints_eipar}
\usage{
endpoints_eipar(api = c("timeseries", "metadata", "qa"))
}
\arguments{
\item{api}{api endpoint}
}
\description{
endpoints_eipar
endpoints_eipar
}
\examples{
\dontrun{
endpoints_eipar()
}
}
\keyword{eipar}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/endpoints.R
\name{endpoints_metadata}
\alias{endpoints_metadata}
\title{endpoints_metadata
endpoints_metadata}
\usage{
endpoints_metadata()
}
\description{
endpoints_metadata
endpoints_metadata
}
\examples{
\dontrun{
endpoints_metadata()
}
}
\keyword{eipar}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/endpoints.R
\name{endpoints_timeseries}
\alias{endpoints_timeseries}
\title{endpoints_timeseries
endpoints_timeseries}
\usage{
endpoints_timeseries()
}
\description{
endpoints_timeseries
endpoints_timeseries
}
\examples{
\dontrun{
endpoints_timeseries()
}
}
\keyword{eipar}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_timeseries.R
\name{get_timeseries}
\alias{get_timeseries}
\title{get_timeseries
get_timeseries}
\usage{
get_timeseries(stationIDs = NULL, componentName = NULL,
fromDateTime = NULL, toDateTime = NULL, username, password)
}
\arguments{
\item{stationIDs}{stationIDs}
\item{componentName}{componentName}
\item{fromDateTime}{fromDateTime}
\item{toDateTime}{toDateTime}
\item{username}{username}
\item{password}{password}
}
\description{
get_timeseries
get_timeseries
}
\examples{
\dontrun{
get_timeseries()
}
}
\keyword{eipar}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/url_request.R
\name{url_request}
\alias{url_request}
\title{url_request
url_request}
\usage{
url_request(epoint, ...)
}
\arguments{
\item{epoint}{epoints obkect}
\item{...}{parameters}
}
\description{
url_request
url_request
}
\examples{
\dontrun{
url_request()
}
}
\keyword{eipar}
......@@ -14,5 +14,9 @@ knitr::opts_chunk$set(collapse = T, comment = "#>")
```r
test <- get_timeseries(username="*****",password="*****")
test <- eipar()
test <- test$measurements(measurementtype="",measurementunit="",pollutantname="SO2",raw_reading_measurement_time="Thu, 30 Aug 2012 22:00:00 GMT",rawreadingid="",samplingpoint="",username="******",password="******")
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment