R/autocomplete.R

Defines functions weatherstack_Lookup

Documented in weatherstack_Lookup

#' lookup specific locations and their identifying response objects
#'
#' @param api_key Your API access key, which can be found in your acccount dashboard.
#' @param location pass a single location
#' @return datframe contain current all the possible location associated with the input location
#' @export
#' @import jsonlite
#' @import httr
#' @examples
#' weatherstack_Lookup("ba435151893f4b833c9b27ca6f28044f","London")

weatherstack_Lookup <- function(api_key,location) {
  # setting up the url to access the api
  domain<- "http://api.weatherstack.com/"
  endpoint <- "autocomplete"
  params<-list(access_key=api_key,
               query=location)
  url <- modify_url(paste(domain,endpoint,sep =""),
                    query = params)
  #post request api
  resp<- POST(url)
  # try for proper API key and access
  if(resp$status_code==101){
    print("User supplied an invalid access key.")
    return (NULL)}
  else if(resp$status_code==404){
    print("User requested a resource which does not exist.")
    return (NULL)}
  #convert the data to json format
  resp_json <- fromJSON(content(resp, "text"),
                        flatten = TRUE)
  #convert to dataframe
  df <- as.data.frame(resp_json)
  return(df)
}
Yuening-Li/weatherstack documentation built on Feb. 16, 2020, 5:42 a.m.