Commit 7e40b99d authored by Jean-Marie Lepioufle's avatar Jean-Marie Lepioufle
Browse files

dependencies to rapiclient

parent 82eb0047
......@@ -3,16 +3,14 @@ Type: Package
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. The irish EPA API provides free access to Irish air quality
timeseries. (generated by Swagger Codegen https://github.com/swagger-api/
swagger-codegen).
Description: Extracting data from the Irish EPA API.
The Irish EPA API provides free access to air quality timeseries.
Depends:
R (>= 3.2.0)
R (>= 3.2.3)
Imports:
jsonlite,
httr,
R6
utils,
rapiclient
Remotes: git::git@git.nilu.no:rextapi/rapiclient.git
Suggests:
knitr
License: GPL-3
......
# Generated by roxygen2: do not edit by hand
export(ApiClient)
export(AqMeasurement)
export(AqMeasurementApi)
export(Element)
export(Response)
importFrom(R6,R6Class)
importFrom(jsonlite,fromJSON)
importFrom(jsonlite,toJSON)
S3method(print,api)
export(api)
# Air Quality Open Data API
#
# Air Quality Open Data API
#
# OpenAPI spec version: 1.0
#
# Generated by: https://github.com/swagger-api/swagger-codegen.git
#' ApiClient Class
#'
#' Generic API client for Swagger client library builds.
#' Swagger generic API client. This client handles the client-
#' server communication, and is invariant across implementations. Specifics of
#' the methods and models for each application are generated from the Swagger
#' templates.
#'
#' NOTE: This class is auto generated by the swagger code generator program.
#' Ref: https://github.com/swagger-api/swagger-codegen
#' Do not edit the class manually.
#'
#' @export
ApiClient <- R6::R6Class(
'ApiClient',
public = list(
basePath = "http://air-tst.epa.ie/api/v1",
configuration = NULL,
userAgent = NULL,
defaultHeaders = NULL,
initialize = function(basePath, configuration, defaultHeaders){
if (!missing(basePath)) {
self$basePath <- basePath
}
if (!missing(configuration)) {
self$configuration <- configuration
}
if (!missing(defaultHeaders)) {
self$defaultHeaders <- defaultHeaders
}
self$`userAgent` <- 'Swagger-Codegen/1.0.0/r'
},
callApi = function(url, method, queryParams, headerParams, body, ...){
headers <- httr::add_headers(headerParams)
if (method == "GET") {
httr::GET(url, queryParams, headers, ...)
}
else if (method == "POST") {
httr::POST(url, queryParams, headers, body = body, ...)
}
else if (method == "PUT") {
httr::PUT(url, queryParams, headers, body = body, ...)
}
else if (method == "PATCH") {
httr::PATCH(url, queryParams, headers, body = body, ...)
}
else if (method == "HEAD") {
httr::HEAD(url, queryParams, headers, ...)
}
else if (method == "DELETE") {
httr::DELETE(url, queryParams, headers, ...)
}
else {
stop("http method must be `GET`, `HEAD`, `OPTIONS`, `POST`, `PATCH`, `PUT` or `DELETE`.")
}
}
)
)
\ No newline at end of file
# Air Quality Open Data API
#
# Air Quality Open Data API
#
# OpenAPI spec version: 1.0
#
# Generated by: https://github.com/swagger-api/swagger-codegen.git
#' AqMeasurement Class
#'
#' @field measurementtype
#' @field samplingpoint
#' @field pollutantname
#' @field measurementunit
#' @field raw_reading_measurement_time
#' @field _id
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
AqMeasurement <- R6::R6Class(
'AqMeasurement',
public = list(
`measurementtype` = NULL,
`samplingpoint` = NULL,
`pollutantname` = NULL,
`measurementunit` = NULL,
`raw_reading_measurement_time` = NULL,
`_id` = NULL,
initialize = function(`measurementtype`, `samplingpoint`, `pollutantname`, `measurementunit`, `raw_reading_measurement_time`, `_id`){
if (!missing(`measurementtype`)) {
stopifnot(is.character(`measurementtype`), length(`measurementtype`) == 1)
self$`measurementtype` <- `measurementtype`
}
if (!missing(`samplingpoint`)) {
stopifnot(is.character(`samplingpoint`), length(`samplingpoint`) == 1)
self$`samplingpoint` <- `samplingpoint`
}
if (!missing(`pollutantname`)) {
stopifnot(is.character(`pollutantname`), length(`pollutantname`) == 1)
self$`pollutantname` <- `pollutantname`
}
if (!missing(`measurementunit`)) {
stopifnot(is.character(`measurementunit`), length(`measurementunit`) == 1)
self$`measurementunit` <- `measurementunit`
}
if (!missing(`raw_reading_measurement_time`)) {
stopifnot(is.character(`raw_reading_measurement_time`), length(`raw_reading_measurement_time`) == 1)
self$`raw_reading_measurement_time` <- `raw_reading_measurement_time`
}
if (!missing(`_id`)) {
stopifnot(is.character(`_id`), length(`_id`) == 1)
self$`_id` <- `_id`
}
},
toJSON = function() {
AqMeasurementObject <- list()
if (!is.null(self$`measurementtype`)) {
AqMeasurementObject[['measurementtype']] <- self$`measurementtype`
}
if (!is.null(self$`samplingpoint`)) {
AqMeasurementObject[['samplingpoint']] <- self$`samplingpoint`
}
if (!is.null(self$`pollutantname`)) {
AqMeasurementObject[['pollutantname']] <- self$`pollutantname`
}
if (!is.null(self$`measurementunit`)) {
AqMeasurementObject[['measurementunit']] <- self$`measurementunit`
}
if (!is.null(self$`raw_reading_measurement_time`)) {
AqMeasurementObject[['raw_reading_measurement_time']] <- self$`raw_reading_measurement_time`
}
if (!is.null(self$`_id`)) {
AqMeasurementObject[['_id']] <- self$`_id`
}
AqMeasurementObject
},
fromJSON = function(AqMeasurementJson) {
AqMeasurementObject <- jsonlite::fromJSON(AqMeasurementJson)
if (!is.null(AqMeasurementObject$`measurementtype`)) {
self$`measurementtype` <- AqMeasurementObject$`measurementtype`
}
if (!is.null(AqMeasurementObject$`samplingpoint`)) {
self$`samplingpoint` <- AqMeasurementObject$`samplingpoint`
}
if (!is.null(AqMeasurementObject$`pollutantname`)) {
self$`pollutantname` <- AqMeasurementObject$`pollutantname`
}
if (!is.null(AqMeasurementObject$`measurementunit`)) {
self$`measurementunit` <- AqMeasurementObject$`measurementunit`
}
if (!is.null(AqMeasurementObject$`raw_reading_measurement_time`)) {
self$`raw_reading_measurement_time` <- AqMeasurementObject$`raw_reading_measurement_time`
}
if (!is.null(AqMeasurementObject$`_id`)) {
self$`_id` <- AqMeasurementObject$`_id`
}
},
toJSONString = function() {
sprintf(
'{
"measurementtype": %s,
"samplingpoint": %s,
"pollutantname": %s,
"measurementunit": %s,
"raw_reading_measurement_time": %s,
"_id": %s
}',
self$`measurementtype`,
self$`samplingpoint`,
self$`pollutantname`,
self$`measurementunit`,
self$`raw_reading_measurement_time`,
self$`_id`
)
},
fromJSONString = function(AqMeasurementJson) {
AqMeasurementObject <- jsonlite::fromJSON(AqMeasurementJson)
self$`measurementtype` <- AqMeasurementObject$`measurementtype`
self$`samplingpoint` <- AqMeasurementObject$`samplingpoint`
self$`pollutantname` <- AqMeasurementObject$`pollutantname`
self$`measurementunit` <- AqMeasurementObject$`measurementunit`
self$`raw_reading_measurement_time` <- AqMeasurementObject$`raw_reading_measurement_time`
self$`_id` <- AqMeasurementObject$`_id`
}
)
)
# Air Quality Open Data API
#
# Air Quality Open Data API
#
# OpenAPI spec version: 1.0
#
# Generated by: https://github.com/swagger-api/swagger-codegen.git
#' @title AqMeasurement operations
#' @description eipar .AqMeasurement
#'
#' @field path Stores url path of the request.
#' @field apiClient Handles the client-server communication.
#' @field userAgent Set the user agent of the request.
#'
#' @importFrom R6 R6Class
#'
#' @section Methods:
#' \describe{
#'
#' aq_measurements_aq_measurement_id_get Retrieves a Aq_measurement document
#'
#'
#' aq_measurements_get Retrieves one or more aq_measurements
#'
#' }
#'
#' @export
AqMeasurementApi <- R6::R6Class(
'AqMeasurementApi',
public = list(
userAgent = "Swagger-Codegen/1.0.0/r",
apiClient = NULL,
initialize = function(apiClient){
if (!missing(apiClient)) {
self$apiClient <- apiClient
}
else {
self$apiClient <- ApiClient$new()
}
},
aq_measurements_aq_measurement_id_get = function(aq_measurement_id, ...){
args <- list(...)
queryParams <- list()
headerParams <- character()
urlPath <- "/aq_measurements/{aq_measurementId}"
if (!missing(`aq_measurement_id`)) {
urlPath <- gsub(paste0("\\{", "aq_measurementId", "\\}"), `aq_measurement_id`, urlPath)
}
resp <- self$apiClient$callApi(url = paste0(self$apiClient$basePath, urlPath),
method = "GET",
queryParams = queryParams,
headerParams = headerParams,
body = body,
...)
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
returnObject <- AqMeasurement$new()
result <- returnObject$fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
Response$new(returnObject, resp)
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
Response$new("API client error", resp)
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
Response$new("API server error", resp)
}
},
aq_measurements_get = function(...){
args <- list(...)
queryParams <- list()
headerParams <- character()
urlPath <- "/aq_measurements"
resp <- self$apiClient$callApi(url = paste0(self$apiClient$basePath, urlPath),
method = "GET",
queryParams = queryParams,
headerParams = headerParams,
body = body,
...)
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
returnObject <- AqMeasurement$new()
result <- returnObject$fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
Response$new(returnObject, resp)
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
Response$new("API client error", resp)
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
Response$new("API server error", resp)
}
}
)
)
#' Element Class
#'
#' Element Class
#' @export
Element <- R6::R6Class(
'Element',
public = list(
id = NULL,
name = NULL,
initialize = function(id,name){
if (!missing(id)) {
stopifnot(is.numeric(id), length(id) == 1)
self$id <- id
}
if (!missing(name)) {
stopifnot(is.character(name), length(name) == 1)
self$name <- name
}
},
toJSON = function() {
sprintf('{"id":%d,"name":"%s"}', self$id, self$name)
}
)
)
\ No newline at end of file
#' Response Class
#'
#' Response Class
#' @export
Response <- R6::R6Class(
'Response',
public = list(
content = NULL,
response = NULL,
initialize = function(content, response){
self$content <- content
self$response <- response
}
)
)
\ No newline at end of file
#' api
#'
#' api
#' @param ... authentication parameters
#' @keywords api
#' @export
#' @examples
#' \dontrun{
#' api()
#' }
api <- function(...){
api_object(...)
}
api_object <- function(...){
name_api <- "eipa"
register_api <- "http://www.epa.ie"
client_api <- rapiclient::get_api(url = "https://air-tst.epa.ie/api-docs",type="swagger")
operations <- rapiclient::get_operations(client_api,...)
object <- local({
register <- function(openURL=TRUE){
if(openURL){
utils::browseURL(register_api)
} else return(register_api)
}
help <- function(name=NULL){
if (!is.null(name)) {
utils::str(operations[name])
} else names(operations)
}
Retrieves_one_or_more_aq_measurements <- function(...) {
res <- operations$Retrieves_one_or_more_aq_measurements(...)
return(res)
}
Retrieves_a_Aq_measurement_document<- function(...) {
res <- operations$Retrieves_a_Aq_measurement_document(...)
return(res)
}
summary <- function(){
print(paste0(name_api," client api"))
}
environment()
})
lockEnvironment(object, TRUE)
structure(object, class=c("api", class(object)))
}
#' @export
print.eipar <- function(x,...){
print.api <- function(x,...){
get("summary",x)()
}
......@@ -25,4 +25,20 @@ remotes::install_git("git@git.nilu.no:rextapi/eipar.git")
## Usage
Have a look at the vignettes
```R
eipar <- eipar::api(httpauth = 1,userpwd=paste0(user,":",pswd))
#get an id/ ask EPA's IT
eipar$register()
# get some help
eipar$help()
# observations
eipar$help("Retrieves_one_or_more_aq_measurements")
res <- eipar$Retrieves_one_or_more_aq_measurements()
# code table
eipar$help("Retrieves_a_Aq_measurement_document")
res <- eipar$Retrieves_a_Aq_measurement_document(aq_measurementId=2)
```
#' 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)))
}
---
title: "first"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{first}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
library(eipar)
knitr::opts_chunk$set(collapse = T, comment = "#>")
```
```r
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="******")
```
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ApiClient.r
\docType{data}
\name{ApiClient}
\alias{ApiClient}
\title{ApiClient Class}
\format{An object of class \code{R6ClassGenerator} of length 24.}
\usage{
ApiClient
}
\description{
Generic API client for Swagger client library builds.
Swagger generic API client. This client handles the client-
server communication, and is invariant across implementations. Specifics of
the methods and models for each application are generated from the Swagger
templates.
}
\details{
NOTE: This class is auto generated by the swagger code generator program.
Ref: https://github.com/swagger-api/swagger-codegen
Do not edit the class manually.
}
\keyword{datasets}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/AqMeasurement.r
\docType{data}
\name{AqMeasurement}
\alias{AqMeasurement}
\title{AqMeasurement Class}
\format{An object of class \code{R6ClassGenerator} of length 24.}
\usage{
AqMeasurement
}
\description{
AqMeasurement Class
}
\section{Fields}{
\describe{
\item{\code{measurementtype}}{}
\item{\code{samplingpoint}}{}
\item{\code{pollutantname}}{}
\item{\code{measurementunit}}{}
\item{\code{raw_reading_measurement_time}}{}
\item{\code{_id}}{}
}}
\keyword{datasets}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/AqMeasurementApi.r
\docType{data}
\name{AqMeasurementApi}
\alias{AqMeasurementApi}
\title{AqMeasurement operations}
\format{An object of class \code{R6ClassGenerator} of length 24.}
\usage{
AqMeasurementApi
}
\description{
eipar .AqMeasurement
}
\section{Fields}{
\describe{
\item{\code{path}}{Stores url path of the request.}
\item{\code{apiClient}}{Handles the client-server communication.}
\item{\code{userAgent}}{Set the user agent of the request.}
}}
\section{Methods}{
\describe{
aq_measurements_aq_measurement_id_get Retrieves a Aq_measurement document