R: Decorate methods of a reference class

Viewed 13

I'm writing an API client in R and was wondering if there is really no way to decorate class methods in a neat way. Take the following example. In each of the request methods, I need to do a type check on id and handle the response after the httr call. In python, I would simply use s.th. like @id_check and @handle on top of the methods.

handle <- function(resp) {
  # do somethind
  return(resp)
}

API <-  methods::setRefClass(
  Class = "API",
  methods = list(
    request1 = function(id){
      # ...httr::POST
      stopifnot(is(id, "character"))
      resp <- NULL
      resp <- handle(resp)
      return(resp)
    },
    request2 = function(id){
      # ...httr::POST
      stopifnot(is(id, "character"))
      resp <- NULL
      resp <- handle(resp)
      return(resp)
    }
  )
)

I know that something similiar could be achieved with the tinsel library, however, as I want to build a clean package, tinsel is no option because of its modified source function. Furthermore, I want the use to call the methods directly, i.e. API()::request1(), and not s.th. like API()::run(request1, id=...).

Do you have any tips?

0 Answers
Related