getUniqueCountryCodes <- function(refresh=FALSE) {
  if (!refresh && exists("country_codes.spotify", envir=parent.frame()))
    return(country_codes.spotify)
  ## ELSE
  country_codes.spotify <- runQry("SELECT DISTINCT country FROM production.staging_raw_spotify")$country
  return(sort(country_codes.spotify))
}

getNewToken <- function(id, secret, url) {
  ## TODO
}

isExpired <- function(token, tol.seconds=5) {
  expires_at <- attr(token, "expires_at")
  if (is.null(expires_at)) {
    warning ("token does not have an expiration value.\nReturning FALSE, ie \"not expired\"")
    return(FALSE)
  }
  ## ELSE
  Sys.time() - expires_at > (-tol.seconds)
}

getNewSpotifyToken <- function() {
    ret.json <- getURL("https://ws.spotify.com/oauth/token?client_id=orchard&grant_type=client_credentials&client_secret=4BVPsfKMVu4T6HSQ")
    ret.ll <- rjson::fromJSON(ret.json)
    token <- ret.ll[["access_token"]]
    if ("expires_in" %in% names(ret.ll)) {
      setattr(token, "expires_at", Sys.time() + ret.ll[["expires_in"]] - 120) ## -2 Minutes margin of error
    }
    return(token)
}

fileExistsWithData <- function(file, size=0) {
  file.exists(file) & file.info(file)$size > size
}


fileDownloaded <- function(file, check.proccessed=TRUE, processed.folder=as.path(dirname(file), processed.subfolder), minSize=-1, processed.subfolder="processed") {
## Note that if the file is found in the initial spot, but it is not larger than minSize, FALSE is returned
##  In other words, we do not keep checking the processed folder for a similarly named larger file
##
## Processed checks three additional files:  
##        * same location, without extension
##        * processed subfolder, same name
##        * processed subfolder, without extension


  if (!length(file)) {
    warning("'file' argument sent to fileDLd() has no length")
    return(logical())
  }

  ## If file exists and greter than minSize, then file found, return TRUE 

  ret <- fileExistsWithData(file=file, size=minSize)

  if (all(ret))
    return(ret)

  ## If there is nowhere else to check, then return FALSE
  if (!check.proccessed)
    return(ret)

  f.no_ext <- gsub("(\\.tar)?\\.gz$", "", file)
  f.processed <- as.path(processed.folder, basename(file), expand=FALSE)
  f.proccessed_no_ext <- gsub("(\\.tar)?\\.gz$", "", f.processed)

  ## RETURN the 'OR' combination of all of these
  ret |
  fileExistsWithData(file=f.no_ext, size=minSize) | 
  fileExistsWithData(file=f.processed, size=minSize) | 
  fileExistsWithData(file=f.proccessed_no_ext, size=minSize)
}
