在API调用(R)中使用httpr::RETRY()函数实现调用重试

dgiusagp  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(140)

我用的是UN Comtrade data API with R

library(rjson)

get.Comtrade <- function(url="http://comtrade.un.org/api/get?"
                         ,maxrec=50000
                         ,type="C"
                         ,freq="A"
                         ,px="HS"
                         ,ps="now"
                         ,r
                         ,p
                         ,rg="all"
                         ,cc="TOTAL"
                         ,fmt="json"
)
{
  string<- paste(url
                 ,"max=",maxrec,"&" #maximum no. of records returned
                 ,"type=",type,"&" #type of trade (c=commodities)
                 ,"freq=",freq,"&" #frequency
                 ,"px=",px,"&" #classification
                 ,"ps=",ps,"&" #time period
                 ,"r=",r,"&" #reporting area
                 ,"p=",p,"&" #partner country
                 ,"rg=",rg,"&" #trade flow
                 ,"cc=",cc,"&" #classification code
                 ,"fmt=",fmt        #Format
                 ,sep = ""
  )
  
  if(fmt == "csv") {
    raw.data<- read.csv(string,header=TRUE)
    return(list(validation=NULL, data=raw.data))
  } else {
    if(fmt == "json" ) {
      raw.data<- fromJSON(file=string)
      data<- raw.data$dataset
      validation<- unlist(raw.data$validation, recursive=TRUE)
      ndata<- NULL
      if(length(data)> 0) {
        var.names<- names(data[[1]])
        data<- as.data.frame(t( sapply(data,rbind)))
        ndata<- NULL
        for(i in 1:ncol(data)){
          data[sapply(data[,i],is.null),i]<- NA
          ndata<- cbind(ndata, unlist(data[,i]))
        }
        ndata<- as.data.frame(ndata)
        colnames(ndata)<- var.names
      }
      return(list(validation=validation,data =ndata))
    }
  }
}

然而,有时它无法连接服务器,我需要运行代码几次才能开始工作。解决方案给出here,使用Retry()函数,重试请求,直到它成功,似乎很有吸引力。然而,我有一些困难,在上面给出的代码实现这个函数。有人用过它,知道如何重新编码吗?

bis0qfac

bis0qfac1#

使用httr::RETRY的API调用可能如下所示:

library(httr)
library(jsonlite)

res <- RETRY(
    verb = "GET",
    url = "http://comtrade.un.org/",
    path = "api/get",
    encode = "json",
    times = 3,
    query = list(
      max = 50000,
      type = "C",
      freq = "A",
      px = "HS",
      ps = "now",
      r = 842,
      p = "124,484",
      rg = "all",
      cc = "TOTAL",
      fmt = "json"
    )
  )

# alternativ: returns dataset as a `list`:
# parsed_content <- content(res, as = "parsed")    

# returns dataset as a `data.frame`:
json_content <- content(res, as = "text")
parsed_content <- parse_json(json_content, simplifyVector = TRUE)
parsed_content$validation
parsed_content$dataset

我建议使用httr重写get.Comtrade函数:

get.Comtrade <- function(verb = "GET",
                         url = "http://comtrade.un.org/",
                         path = "api/get",
                         encode = "json",
                         times = 3,
                         max = 50000,
                         type = "C",
                         freq = "A",
                         px = "HS",
                         ps = "now",
                         r,
                         p,
                         rg = "all",
                         cc = "TOTAL",
                         fmt = "json") {
  res <- httr::RETRY(
    verb = verb,
    url = url,
    path = path,
    encode = encode,
    times = times,
    query = list(
      max = max,
      type = type,
      freq = freq,
      px = px,
      ps = ps,
      r = r,
      p = p,
      rg = rg,
      cc = cc,
      fmt = fmt
    )
  )
  jsonlite::parse_json(content(res, as = "text"), simplifyVector = TRUE)
}

s1 <- get.Comtrade(r = "842", p = "124,484", times = 5)
print(s1)

有关library(httr)的详细信息,请参见thisthis

相关问题