diff --git a/.Rbuildignore b/.Rbuildignore index c44b155..f79d4e6 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -3,5 +3,7 @@ 1.R testy.R tabular_fun.R +RAhrefs.Rproj +helper_authorization\.R .travis.yml appveyor.yml diff --git a/.gitignore b/.gitignore index 50199ad..2e266ac 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .RData .Ruserdata *.csv +helper_authorization\.R diff --git a/DESCRIPTION b/DESCRIPTION index c1c51f6..ce3ba88 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,7 +6,8 @@ Depends: R (>= 3.5.0) Imports: assertthat, httr, - jsonlite + jsonlite, + testthat Author: Leszek Siemiński [aut, cre] Maintainer: Leszek Siemiński Description: Interface for interaction with the Ahrefs.com API through R. diff --git a/NAMESPACE b/NAMESPACE index b73ba29..4442add 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -28,3 +28,8 @@ export(rah_refdomains_new_lost_counters) export(rah_refips) export(rah_subscription_info) import(assertthat) +importFrom(httr,GET) +importFrom(httr,add_headers) +importFrom(httr,content) +importFrom(httr,stop_for_status) +importFrom(jsonlite,fromJSON) diff --git a/R/auth.R b/R/auth.R index 2e8ac26..7a60815 100644 --- a/R/auth.R +++ b/R/auth.R @@ -1,4 +1,3 @@ -# auth ------------------------------------------------------------------------ #' Authorize your Ahrefs API connection with a API Key (Token) #' #' @param api_key character string. Valid API key obtained at: https://ahrefs.com/api/profile @@ -7,6 +6,12 @@ #' @return invisibly returns API token into environment variable AHREFS_AUTH_TOKEN and prints the status #' @export #' +#' @import assertthat +#' @importFrom httr GET +#' @importFrom httr add_headers +#' @importFrom httr content +#' @importFrom jsonlite fromJSON +#' #' @examples #' \dontrun{ #' rah_auth("ABCDEFGHIJKLMNOPQRST") @@ -15,25 +20,42 @@ rah_auth <- function( api_key, verbose = TRUE ){ - assertthat::noNA(api_key) - assertthat::not_empty(api_key) - assertthat::is.string(api_key) + # safety net ---------------------------------------------------------------- + assert_that( + !is.na(api_key), noNA(api_key), not_empty(api_key), is.string(api_key), + nchar(api_key) > 0, + noNA(verbose), not_empty(verbose), assert_that(is.logical(verbose))) + + # connecting to auth endpoint ----------------------------------------------- + # x <- GET(url = 'https://apiv2.ahrefs.com/', + # add_headers(token = api_key)) + + response <- GET(url = paste0( + "https://apiv2.ahrefs.com/", + "?token=", api_key, + "&from=", "anchors", + "&target=", "ahrefs.com", + "&mode=", "domain", + # if (!is.null(metrics)) {paste0("&select=", paste(metrics, collapse = ","))}, + "&limit=", 2, + "&output=json"#, + # if (!is.null(where)) {paste0("&where=", where)}, + # if (!is.null(having)) {paste0("&having=", having)}, + # if (!is.null(order_by)){paste0("&order_by=", order_by)} + )) - assertthat::noNA(verbose) - assertthat::not_empty(verbose) - assertthat::assert_that(is.logical(verbose)) + stop_for_status(response) + content <- content(response, type = "text", encoding = "UTF-8") + result <- fromJSON(content, simplifyVector = FALSE) - x <- httr::GET(url = 'https://apiv2.ahrefs.com/', - httr::add_headers(token = api_key)) + # api_key sanity check ------------------------------------------------------ + http_status_200 <- response$status_code == 200 + no_hidden_error <- !("error" %in% names(jsonlite::fromJSON(httr::content(response, as = "text")))) + # is_df <- is.data.frame(result) - if (x$status_code == 200){ + # saving enviromental variable ---------------------------------------------- + if (http_status_200 & no_hidden_error){ Sys.setenv("AHREFS_AUTH_TOKEN" = api_key) - if (verbose) { - message("API authorized.") - } - } else { - message(paste0("Authorization error: HTTP status code ", - x$status_code, - ". Check your api key.")) - } + if (verbose) message("API authorized.") + } else stop(paste0("Authorization error: HTTP status code ", response$status_code,". Check your api key.")) } diff --git a/R/condition.R b/R/condition.R index cee0ecb..dd64459 100644 --- a/R/condition.R +++ b/R/condition.R @@ -49,26 +49,16 @@ #' value = "10") #' } rah_condition <- function(column_name, operator, value, is_date = FALSE){ - assert_that(is.logical(is_date), # date - not_empty(is_date), - !is.na(is_date), - !is.null(is_date), - not_empty(column_name), # column_name - !is.null(column_name), - !is.na(column_name), - is.string(column_name), - not_empty(value), # value - !is.null(value), - !is.na(value), - not_empty(operator), # operator - !is.null(operator), - !is.na(operator), - operator %in% c("EQUALS", "UNEQUALS", "LESS_THAN", - "LESS_OR_EQUAL", "GREATER_THAN", - "GREATER_OR_EQUAL", "SUBDOMAIN", - "SUBSTRING", "WORD"), - length(operator) == 1 - ) + # safety net ---------------------------------------------------------------- + assert_that( + is.logical(is_date), not_empty(is_date), !is.na(is_date), !is.null(is_date), + not_empty(column_name), !is.null(column_name), !is.na(column_name), + is.string(column_name), not_empty(value), !is.null(value), !is.na(value), + not_empty(operator), !is.null(operator), !is.na(operator), + operator %in% c("EQUALS", "UNEQUALS", "LESS_THAN", "LESS_OR_EQUAL", + "GREATER_THAN", "GREATER_OR_EQUAL", "SUBDOMAIN", "SUBSTRING", + "WORD"), length(operator) == 1 + ) if(!is_date){ x <- switch( diff --git a/R/downloader.R b/R/downloader.R index 60a8ddf..611e145 100644 --- a/R/downloader.R +++ b/R/downloader.R @@ -65,6 +65,10 @@ #' @return list or nested list object #' #' @import assertthat +#' @importFrom httr GET +#' @importFrom httr stop_for_status +#' @importFrom httr content +#' @importFrom jsonlite fromJSON #' #' @examples #' # do not use this function - instead use its wrappers (rah_()) @@ -109,45 +113,26 @@ rah_downloader <- function(target, # modes list ---------------------------------------------------------------- mode_vector <- c("exact", "domain", "subdomains", "prefix") - # safety net -------------------------------------------------------- - assert_that(!is.null(target), # target - !is.na(target), - is.string(target), - grepl("\\.", target), - !is.null(report), # report - !is.na(report), - is.string(report), - report %in% report_vector, - !is.null(token), # token - !is.na(token), - is.string(token), - nchar(token) > 30, - !is.null(mode), # mode - !is.na(mode), - is.string(mode), - mode %in% mode_vector, - is.null(metrics) | is.vector(metrics, mode = "character"), # metrics - # if (!is.null(metrics)) {is.vector(metrics, mode = "character")}, # metrics - limit >= 1, # limit - limit %% 1 == 0, - is.number(limit) - # !is.na(having), # having - # is.string(having), - # !is.na(where), # where - # is.string(where) - ) + # safety net ---------------------------------------------------------------- + assert_that( + !is.null(target), !is.na(target), is.string(target), grepl("\\.", target), + !is.null(report), !is.na(report), is.string(report), + report %in% report_vector, !is.null(token), !is.na(token), + is.string(token), nchar(token) > 30, !is.null(mode), !is.na(mode), + is.string(mode), mode %in% mode_vector, + is.null(metrics) | is.vector(metrics, mode = "character"), limit >= 1, + limit %% 1 == 0, is.number(limit) + ) # order_by preparation ------------------------------------------------------ if (!is.null(order_by)) { if (grepl("\\:", order_by)) { - order_by <- gsub(pattern = "\\:", - replacement = "%3A", - order_by) + order_by <- gsub("\\:", "%3A", order_by) } } # downloading --------------------------------------------------------------- - response <- httr::GET(paste0( + response <- GET(paste0( "https://apiv2.ahrefs.com/", "?token=", token, "&from=", report, @@ -161,8 +146,8 @@ rah_downloader <- function(target, if (!is.null(order_by)){paste0("&order_by=", order_by)} )) - httr::stop_for_status(response) - content <- httr::content(response, type = "text", encoding = "UTF-8") - result <- jsonlite::fromJSON(content, simplifyVector = FALSE) + stop_for_status(response) + content <- content(response, type = "text", encoding = "UTF-8") + result <- fromJSON(content, simplifyVector = FALSE) return(result) } diff --git a/R/rah_ahrefs_rank.R b/R/rah_ahrefs_rank.R index 4d0ab30..c51380a 100644 --- a/R/rah_ahrefs_rank.R +++ b/R/rah_ahrefs_rank.R @@ -114,12 +114,12 @@ rah_ahrefs_rank <- function(target, having = NULL ){ data_list <- rah_downloader( - target = target, - report = "ahrefs_rank", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "ahrefs_rank", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_anchors.R b/R/rah_anchors.R index 2944f24..d0a701d 100644 --- a/R/rah_anchors.R +++ b/R/rah_anchors.R @@ -104,22 +104,23 @@ #' where = cond_where, #' order_by = "refpages:desc") #' } -rah_anchors <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_anchors <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "anchors", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "anchors", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_anchors_refdomains.R b/R/rah_anchors_refdomains.R index fc01dd4..baf8816 100644 --- a/R/rah_anchors_refdomains.R +++ b/R/rah_anchors_refdomains.R @@ -93,15 +93,16 @@ #' where = cond_where, #' order_by = "anchors:desc") #' } -rah_anchors_refdomains <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_anchors_refdomains <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( target = target, report = "anchors_refdomains", diff --git a/R/rah_backlinks.R b/R/rah_backlinks.R index 328bd5a..9738948 100644 --- a/R/rah_backlinks.R +++ b/R/rah_backlinks.R @@ -89,8 +89,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' diff --git a/R/rah_backlinks_new_lost.R b/R/rah_backlinks_new_lost.R index 634d525..a26f4b7 100644 --- a/R/rah_backlinks_new_lost.R +++ b/R/rah_backlinks_new_lost.R @@ -119,15 +119,16 @@ #' where = cond_where, #' order_by = "domain_rating:desc") #' } -rah_backlinks_new_lost <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_backlinks_new_lost <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( target = target, report = "backlinks_new_lost", diff --git a/R/rah_backlinks_new_lost_counters.R b/R/rah_backlinks_new_lost_counters.R index c03e2de..b6849a1 100644 --- a/R/rah_backlinks_new_lost_counters.R +++ b/R/rah_backlinks_new_lost_counters.R @@ -91,7 +91,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} #' #' @return data frame #' @export @@ -122,15 +121,16 @@ #' where = cond_where, #' order_by = "links_external:desc") #' } -rah_backlinks_new_lost_counters <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_backlinks_new_lost_counters <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( target = target, report = "backlinks_new_lost_counters", diff --git a/R/rah_backlinks_one_per_domain.R b/R/rah_backlinks_one_per_domain.R index e86c9bb..05c6006 100644 --- a/R/rah_backlinks_one_per_domain.R +++ b/R/rah_backlinks_one_per_domain.R @@ -90,8 +90,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -121,15 +119,16 @@ #' where = cond_where, #' order_by = "ahrefs_rank:desc") #' } -rah_backlinks_one_per_domain <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_backlinks_one_per_domain <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( target = target, report = "backlinks_one_per_domain", diff --git a/R/rah_broken_backlinks.R b/R/rah_broken_backlinks.R index e1fc922..eff0a50 100644 --- a/R/rah_broken_backlinks.R +++ b/R/rah_broken_backlinks.R @@ -120,22 +120,23 @@ #' where = cond_where, #' order_by = "refpages:desc") #' } -rah_broken_backlinks <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_broken_backlinks <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "broken_backlinks", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "broken_backlinks", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_broken_links.R b/R/rah_broken_links.R index 58d55e2..3f03680 100644 --- a/R/rah_broken_links.R +++ b/R/rah_broken_links.R @@ -122,15 +122,16 @@ #' where = cond_where, #' order_by = "domain_rating:desc") #' } -rah_broken_links <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_broken_links <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( target = target, report = "broken_links", diff --git a/R/rah_domain_rating.R b/R/rah_domain_rating.R index f5d6b93..ddbddf5 100644 --- a/R/rah_domain_rating.R +++ b/R/rah_domain_rating.R @@ -68,8 +68,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -98,15 +96,16 @@ #' having = cond_having, #' order_by = "ahrefs_rank:desc") #' } -rah_domain_rating <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_domain_rating <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( target = target, report = "domain_rating", diff --git a/R/rah_linked_anchors.R b/R/rah_linked_anchors.R index 1893628..30612ab 100644 --- a/R/rah_linked_anchors.R +++ b/R/rah_linked_anchors.R @@ -85,8 +85,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -115,15 +113,16 @@ #' where = cond_where, #' order_by = "ahrefs_rank:desc") #' } -rah_linked_anchors <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_linked_anchors <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( target = target, report = "linked_anchors", diff --git a/R/rah_linked_domains.R b/R/rah_linked_domains.R index 40e9f79..e9d21dc 100644 --- a/R/rah_linked_domains.R +++ b/R/rah_linked_domains.R @@ -100,22 +100,23 @@ #' having = cond_having, #' order_by = "ahrefs_rank:desc") #' } -rah_linked_domains <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_linked_domains <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "linked_domains", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "linked_domains", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_linked_domains_by_type.R b/R/rah_linked_domains_by_type.R index 446aba5..103d2d3 100644 --- a/R/rah_linked_domains_by_type.R +++ b/R/rah_linked_domains_by_type.R @@ -82,8 +82,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -112,15 +110,16 @@ #' having = cond_having, #' order_by = "ahrefs_rank:desc") #' } -rah_linked_domains_by_type <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_linked_domains_by_type <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( target = target, report = "linked_domains_by_type", diff --git a/R/rah_metrics.R b/R/rah_metrics.R index 91ae1cf..7814051 100644 --- a/R/rah_metrics.R +++ b/R/rah_metrics.R @@ -78,8 +78,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -93,22 +91,23 @@ #' limit = 2,, #' order_by = "backlinks:desc") #' } -rah_metrics <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_metrics <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "metrics", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "metrics", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_metrics_extended.R b/R/rah_metrics_extended.R index 70b8a76..de07c75 100644 --- a/R/rah_metrics_extended.R +++ b/R/rah_metrics_extended.R @@ -85,8 +85,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -100,15 +98,16 @@ #' limit = 2,, #' order_by = "backlinks:desc") #' } -rah_metrics_extended <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_metrics_extended <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( target = target, report = "metrics_extended", diff --git a/R/rah_pages.R b/R/rah_pages.R index db26e82..bb682c0 100644 --- a/R/rah_pages.R +++ b/R/rah_pages.R @@ -78,8 +78,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -108,22 +106,23 @@ #' where = cond_where, #' order_by = "ahrefs_rank:desc") #' } -rah_pages <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_pages <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "pages", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "pages", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_pages_extended.R b/R/rah_pages_extended.R index a2fe1ae..af5ba45 100644 --- a/R/rah_pages_extended.R +++ b/R/rah_pages_extended.R @@ -83,8 +83,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -113,22 +111,23 @@ #' where = cond_where, #' order_by = "ahrefs_rank:desc") #' } -rah_pages_extended <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_pages_extended <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "pages_extended", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "pages_extended", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_pages_info.R b/R/rah_pages_info.R index e3d205e..5230db5 100644 --- a/R/rah_pages_info.R +++ b/R/rah_pages_info.R @@ -86,8 +86,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return nested list - the structure can be too complicated to convert into simple data frame #' @export #' @@ -116,22 +114,23 @@ #' where = cond_where, #' order_by = "ahrefs_rank:desc") #' } -rah_pages_info <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_pages_info <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "pages_info", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "pages_info", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_refdomains.R b/R/rah_refdomains.R index 0d8aec7..a1f8b58 100644 --- a/R/rah_refdomains.R +++ b/R/rah_refdomains.R @@ -102,22 +102,23 @@ #' where = cond_where, #' order_by = "ahrefs_rank:desc") #' } -rah_refdomains <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_refdomains <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "refdomains", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "refdomains", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_refdomains_by_type.R b/R/rah_refdomains_by_type.R index 4088953..39d9043 100644 --- a/R/rah_refdomains_by_type.R +++ b/R/rah_refdomains_by_type.R @@ -82,8 +82,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -112,22 +110,23 @@ #' where = cond_where, #' order_by = "ahrefs_rank:desc") #' } -rah_refdomains_by_type <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL +rah_refdomains_by_type <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL ){ data_list <- rah_downloader( - target = target, - report = "refdomains_by_type", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "refdomains_by_type", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_refdomains_new_lost.R b/R/rah_refdomains_new_lost.R index e8fe6f9..a2d632b 100644 --- a/R/rah_refdomains_new_lost.R +++ b/R/rah_refdomains_new_lost.R @@ -68,8 +68,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -83,22 +81,23 @@ #' limit = 2, #' order_by = "domain_rating:desc") #' } -rah_refdomains_new_lost <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_refdomains_new_lost <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "refdomains_new_lost", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "refdomains_new_lost", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_refdomains_new_lost_counters.R b/R/rah_refdomains_new_lost_counters.R index 0d8d61d..fc23cc0 100644 --- a/R/rah_refdomains_new_lost_counters.R +++ b/R/rah_refdomains_new_lost_counters.R @@ -71,8 +71,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -86,22 +84,23 @@ #' limit = 2, #' order_by = "new_total:desc") #' } -rah_refdomains_new_lost_counters <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL +rah_refdomains_new_lost_counters <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL ){ data_list <- rah_downloader( - target = target, - report = "refdomains_new_lost_counters", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "refdomains_new_lost_counters", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_refips.R b/R/rah_refips.R index b1e155a..219f426 100644 --- a/R/rah_refips.R +++ b/R/rah_refips.R @@ -67,8 +67,6 @@ #' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -82,22 +80,23 @@ #' limit = 2, #' order_by = "backlinks:desc") #' } -rah_refips <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_refips <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "refips", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "refips", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/R/rah_subscription_info.R b/R/rah_subscription_info.R index ad217cf..82f9933 100644 --- a/R/rah_subscription_info.R +++ b/R/rah_subscription_info.R @@ -66,8 +66,6 @@ #' \code{RAhrefs::rah_subscription_info()} #' } #' -#' @source \url{https://ahrefs.com/api/documentation} -#' #' @return data frame #' @export #' @@ -78,22 +76,23 @@ #' # downloading #' b <- RAhrefs::rah_subscription_info() #' } -rah_subscription_info <- function(target, - token = Sys.getenv("AHREFS_AUTH_TOKEN"), - mode = "domain", - metrics = NULL, - limit = 1000, - order_by = NULL, - where = NULL, - having = NULL -){ +rah_subscription_info <- function( + target, + token = Sys.getenv("AHREFS_AUTH_TOKEN"), + mode = "domain", + metrics = NULL, + limit = 1000, + order_by = NULL, + where = NULL, + having = NULL) +{ data_list <- rah_downloader( - target = target, - report = "subscription_info", - token = token, - mode = mode, - metrics = metrics, - limit = limit, + target = target, + report = "subscription_info", + token = token, + mode = mode, + metrics = metrics, + limit = limit, order_by = order_by, where = where, having = having) diff --git a/man/rah_backlinks.Rd b/man/rah_backlinks.Rd index 134cfb3..e394b56 100644 --- a/man/rah_backlinks.Rd +++ b/man/rah_backlinks.Rd @@ -4,8 +4,6 @@ \alias{rah_backlinks} \title{Export the backlinks and details of the referring pages, such as anchor and page title.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_backlinks_new_lost_counters.Rd b/man/rah_backlinks_new_lost_counters.Rd index d4ed9b4..aad3b1f 100644 --- a/man/rah_backlinks_new_lost_counters.Rd +++ b/man/rah_backlinks_new_lost_counters.Rd @@ -4,8 +4,6 @@ \alias{rah_backlinks_new_lost_counters} \title{Export new and lost backlink totals.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_backlinks_one_per_domain.Rd b/man/rah_backlinks_one_per_domain.Rd index e42fcb5..00aaaf1 100644 --- a/man/rah_backlinks_one_per_domain.Rd +++ b/man/rah_backlinks_one_per_domain.Rd @@ -4,8 +4,6 @@ \alias{rah_backlinks_one_per_domain} \title{Export the backlinks and details of the referring pages, such as anchor and page title.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_domain_rating.Rd b/man/rah_domain_rating.Rd index 499912c..27a84c5 100644 --- a/man/rah_domain_rating.Rd +++ b/man/rah_domain_rating.Rd @@ -4,8 +4,6 @@ \alias{rah_domain_rating} \title{Export the Domain Rating.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_linked_anchors.Rd b/man/rah_linked_anchors.Rd index 41816d1..5d27504 100644 --- a/man/rah_linked_anchors.Rd +++ b/man/rah_linked_anchors.Rd @@ -4,8 +4,6 @@ \alias{rah_linked_anchors} \title{Export the anchor text and the number of outgoing links that have it.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_linked_domains_by_type.Rd b/man/rah_linked_domains_by_type.Rd index f1e748e..3721b10 100644 --- a/man/rah_linked_domains_by_type.Rd +++ b/man/rah_linked_domains_by_type.Rd @@ -4,8 +4,6 @@ \alias{rah_linked_domains_by_type} \title{Export the external domains that the target has links to.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_metrics.Rd b/man/rah_metrics.Rd index 8abf20c..6d43065 100644 --- a/man/rah_metrics.Rd +++ b/man/rah_metrics.Rd @@ -4,8 +4,6 @@ \alias{rah_metrics} \title{Export metrics about the target, such as total number of backlinks, referring pages, etc., that are similar to the Site Explorer Overview page with the addition of stats for total number of HTML pages, internal and external links.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_metrics_extended.Rd b/man/rah_metrics_extended.Rd index 424da40..617c35a 100644 --- a/man/rah_metrics_extended.Rd +++ b/man/rah_metrics_extended.Rd @@ -4,8 +4,6 @@ \alias{rah_metrics_extended} \title{Export additional metrics about the target, such as total number of referring domains, referring class C networks and referring IP addresses.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_pages.Rd b/man/rah_pages.Rd index 6caca28..e8f79d9 100644 --- a/man/rah_pages.Rd +++ b/man/rah_pages.Rd @@ -4,8 +4,6 @@ \alias{rah_pages} \title{Export the crawled pages.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_pages_extended.Rd b/man/rah_pages_extended.Rd index d7a1a09..6ad653f 100644 --- a/man/rah_pages_extended.Rd +++ b/man/rah_pages_extended.Rd @@ -4,8 +4,6 @@ \alias{rah_pages_extended} \title{Export additional metrics about the target, such as total number of referring domains, referring class C networks and referring IP addresses.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_pages_info.Rd b/man/rah_pages_info.Rd index da1a63a..acbacc5 100644 --- a/man/rah_pages_info.Rd +++ b/man/rah_pages_info.Rd @@ -4,8 +4,6 @@ \alias{rah_pages_info} \title{Export additional info about the target, such as IP address, canonical URL, social meta tags and social metrics.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_refdomains_by_type.Rd b/man/rah_refdomains_by_type.Rd index c4c6369..c2ddf53 100644 --- a/man/rah_refdomains_by_type.Rd +++ b/man/rah_refdomains_by_type.Rd @@ -4,8 +4,6 @@ \alias{rah_refdomains_by_type} \title{Export the referring domains that contain backlinks to the target.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_refdomains_new_lost.Rd b/man/rah_refdomains_new_lost.Rd index 58f5a5e..9f91139 100644 --- a/man/rah_refdomains_new_lost.Rd +++ b/man/rah_refdomains_new_lost.Rd @@ -4,8 +4,6 @@ \alias{rah_refdomains_new_lost} \title{Export the new or lost referring domains and their details.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_refdomains_new_lost_counters.Rd b/man/rah_refdomains_new_lost_counters.Rd index 30ff11c..8d9703f 100644 --- a/man/rah_refdomains_new_lost_counters.Rd +++ b/man/rah_refdomains_new_lost_counters.Rd @@ -4,8 +4,6 @@ \alias{rah_refdomains_new_lost_counters} \title{Export new and lost domains totals.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_refips.Rd b/man/rah_refips.Rd index 4bc3c29..58681e4 100644 --- a/man/rah_refips.Rd +++ b/man/rah_refips.Rd @@ -4,8 +4,6 @@ \alias{rah_refips} \title{Export the referring IP addresses that have at least one link to the target.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/man/rah_subscription_info.Rd b/man/rah_subscription_info.Rd index 797de71..2e00fda 100644 --- a/man/rah_subscription_info.Rd +++ b/man/rah_subscription_info.Rd @@ -4,8 +4,6 @@ \alias{rah_subscription_info} \title{Export user subscription information.} \source{ -\url{https://ahrefs.com/api/documentation} - \url{https://ahrefs.com/api/documentation} } \usage{ diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..8b88709 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,3 @@ +library(testthat) +library(RAhrefs) +test_check("RAhrefs") diff --git a/tests/testthat/test_auth.R b/tests/testthat/test_auth.R new file mode 100644 index 0000000..3c63b67 --- /dev/null +++ b/tests/testthat/test_auth.R @@ -0,0 +1,26 @@ +context("Authorization") +library(RAhrefs) +library(testthat) + +# defensive ---------------------------------------------------------------------- +test_that("api_key param doesn't accept wrong values", { + expect_error(rah_auth(api_key = "", verbose = TRUE)) + expect_error(rah_auth(api_key = NA, verbose = TRUE)) + expect_error(rah_auth(api_key = NULL, verbose = TRUE)) + expect_error(rah_auth(api_key = "stupid things", verbose = TRUE)) + expect_error(rah_auth(api_key = TRUE, verbose = TRUE)) + expect_error(rah_auth(api_key = FALSE, verbose = TRUE)) + expect_error(rah_auth(api_key = "", verbose = FALSE)) + expect_error(rah_auth(api_key = NA, verbose = FALSE)) + expect_error(rah_auth(api_key = NULL, verbose = FALSE)) + expect_error(rah_auth(api_key = "stupid things", verbose = FALSE)) + expect_error(rah_auth(api_key = TRUE, verbose = FALSE)) + expect_error(rah_auth(api_key = FALSE, verbose = FALSE)) +}) + +# cancelled ------------------------------------------------------------------- +# can't provide the commercial API key in such a place, must resign from testing this +# test_that("proper api_key returns messages properly", { +# expect_message(rah_auth(api_key = Sys.getenv("AHREFS_AUTH_TOKEN"), verbose = TRUE), regexp = "API authorized.") +# expect_silent(rah_auth(api_key = Sys.getenv("AHREFS_AUTH_TOKEN"), verbose = FALSE)) +# })