From 4345213990c24c6105b40c1ead8bb4cd1d84bc2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leszek=20Siemi=C5=84ski?= <32634801+Leszek-Sieminski@users.noreply.github.com> Date: Tue, 16 Jul 2019 12:46:11 +0200 Subject: [PATCH 1/9] improve docs & add .Rproj --- NAMESPACE | 5 +++ R/auth.R | 32 +++++++-------- R/condition.R | 30 +++++--------- R/downloader.R | 53 +++++++++---------------- R/rah_ahrefs_rank.R | 12 +++--- R/rah_anchors.R | 12 +++--- R/rah_anchors_refdomains.R | 19 ++++----- R/rah_backlinks.R | 2 - R/rah_backlinks_new_lost.R | 19 ++++----- R/rah_backlinks_new_lost_counters.R | 20 +++++----- R/rah_backlinks_one_per_domain.R | 21 +++++----- R/rah_broken_backlinks.R | 31 ++++++++------- R/rah_broken_links.R | 19 ++++----- R/rah_domain_rating.R | 21 +++++----- R/rah_linked_anchors.R | 21 +++++----- R/rah_linked_domains.R | 31 ++++++++------- R/rah_linked_domains_by_type.R | 21 +++++----- R/rah_metrics.R | 33 ++++++++------- R/rah_metrics_extended.R | 21 +++++----- R/rah_pages.R | 33 ++++++++------- R/rah_pages_extended.R | 33 ++++++++------- R/rah_pages_info.R | 33 ++++++++------- R/rah_refdomains.R | 31 ++++++++------- R/rah_refdomains_by_type.R | 31 +++++++-------- R/rah_refdomains_new_lost.R | 33 ++++++++------- R/rah_refdomains_new_lost_counters.R | 31 +++++++-------- R/rah_refips.R | 33 ++++++++------- R/rah_subscription_info.R | 33 ++++++++------- RAhrefs.Rproj | 21 ++++++++++ man/rah_backlinks.Rd | 2 - man/rah_backlinks_new_lost_counters.Rd | 2 - man/rah_backlinks_one_per_domain.Rd | 2 - man/rah_domain_rating.Rd | 2 - man/rah_linked_anchors.Rd | 2 - man/rah_linked_domains_by_type.Rd | 2 - man/rah_metrics.Rd | 2 - man/rah_metrics_extended.Rd | 2 - man/rah_pages.Rd | 2 - man/rah_pages_extended.Rd | 2 - man/rah_pages_info.Rd | 2 - man/rah_refdomains_by_type.Rd | 2 - man/rah_refdomains_new_lost.Rd | 2 - man/rah_refdomains_new_lost_counters.Rd | 2 - man/rah_refips.Rd | 2 - man/rah_subscription_info.Rd | 2 - 45 files changed, 361 insertions(+), 406 deletions(-) create mode 100644 RAhrefs.Rproj 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..c0d0685 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,10 @@ #' @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 +#' #' @examples #' \dontrun{ #' rah_auth("ABCDEFGHIJKLMNOPQRST") @@ -15,25 +18,18 @@ rah_auth <- function( api_key, verbose = TRUE ){ - assertthat::noNA(api_key) - assertthat::not_empty(api_key) - assertthat::is.string(api_key) - - assertthat::noNA(verbose) - assertthat::not_empty(verbose) - assertthat::assert_that(is.logical(verbose)) + # safety net ---------------------------------------------------------------- + assert_that( + noNA(api_key), not_empty(api_key), is.string(api_key), noNA(verbose), + not_empty(verbose), assert_that(is.logical(verbose))) - x <- httr::GET(url = 'https://apiv2.ahrefs.com/', - httr::add_headers(token = api_key)) + # connecting to auth endpoint ----------------------------------------------- + x <- GET(url = 'https://apiv2.ahrefs.com/', + add_headers(token = api_key)) + # saving enviromental variable ---------------------------------------------- if (x$status_code == 200){ 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 message(paste0("Authorization error: HTTP status code ", x$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..16c5ee2 100644 --- a/R/rah_anchors.R +++ b/R/rah_anchors.R @@ -114,12 +114,12 @@ rah_anchors <- function(target, 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/RAhrefs.Rproj b/RAhrefs.Rproj new file mode 100644 index 0000000..270314b --- /dev/null +++ b/RAhrefs.Rproj @@ -0,0 +1,21 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace 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{ From 6fbfc87d5114653ef16ef7984da144c0c68e316c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leszek=20Siemi=C5=84ski?= <32634801+Leszek-Sieminski@users.noreply.github.com> Date: Wed, 17 Jul 2019 11:02:36 +0200 Subject: [PATCH 2/9] add CI configs --- .Rbuildignore | 4 ++++ .gitignore | 1 + .travis.yml | 11 +++++++++++ 3 files changed, 16 insertions(+) create mode 100644 .travis.yml diff --git a/.Rbuildignore b/.Rbuildignore index 25f18da..f79d4e6 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -3,3 +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/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d1dae89 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +language: r +cache: packages +r_packages: + - covr + +script: + - R CMD build . + - R CMD check *tar.gz + +after_success: + - Rscript -e 'library(covr); codecov()' From b49aade52e4132c1efa155e67bac7f19f2cfbce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leszek=20Siemi=C5=84ski?= <32634801+Leszek-Sieminski@users.noreply.github.com> Date: Wed, 17 Jul 2019 11:04:00 +0200 Subject: [PATCH 3/9] add tests --- DESCRIPTION | 3 ++- tests/testthat.R | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 tests/testthat.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/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") From ebe38a1b2b8460abec88176ee2c71a175fad6904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leszek=20Siemi=C5=84ski?= <32634801+Leszek-Sieminski@users.noreply.github.com> Date: Wed, 17 Jul 2019 11:04:17 +0200 Subject: [PATCH 4/9] add appveyour config --- appveyor.yml | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..16d563e --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,41 @@ +# before_build: +# ps: | +# cp ..\travis-tool.sh R-package\travis-tool.sh +# cp travis-tool.sh.cmd R-package\travis-tools.sh.cmd +# cd R-package +# bash -c "echo '^travis-tool\.sh\.cmd$' >> .Rbuildignore" +# build_script: +# - R CMD build . +# - R CMD check *tar.gz + +# DO NOT CHANGE the "init" and "install" sections below + +# Download script file from GitHub +init: + ps: | + $ErrorActionPreference = "Stop" + Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1" + Import-Module '..\appveyor-tool.ps1' + install: + ps: Bootstrap + + # Adapt as necessary starting from here + + #environment: + # global: + # WARNINGS_ARE_ERRORS: 1 + # _R_CHECK_SYSTEM_CLOCK_: FALSE + # + # matrix: + # - R_VERSION: 3.4.4 + # R_ARCH: x64 + # GCC_PATH: mingw_64 + + matrix: + fast_finish: true + + build_script: + - travis-tool.sh install_deps + + test_script: + - travis-tool.sh run_tests From 164e82d7515589011eb8026d6b1848e91a4d3ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leszek=20Siemi=C5=84ski?= <32634801+Leszek-Sieminski@users.noreply.github.com> Date: Wed, 17 Jul 2019 11:05:24 +0200 Subject: [PATCH 5/9] add auth tests --- R/auth.R | 38 ++++++++++++++++++++++----- R/rah_anchors.R | 19 +++++++------- tests/testthat/test_auth.R | 54 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 tests/testthat/test_auth.R diff --git a/R/auth.R b/R/auth.R index c0d0685..7a60815 100644 --- a/R/auth.R +++ b/R/auth.R @@ -9,6 +9,8 @@ #' @import assertthat #' @importFrom httr GET #' @importFrom httr add_headers +#' @importFrom httr content +#' @importFrom jsonlite fromJSON #' #' @examples #' \dontrun{ @@ -20,16 +22,40 @@ rah_auth <- function( ){ # safety net ---------------------------------------------------------------- assert_that( - noNA(api_key), not_empty(api_key), is.string(api_key), noNA(verbose), - not_empty(verbose), assert_that(is.logical(verbose))) + !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)) + # 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)} + )) + + stop_for_status(response) + content <- content(response, type = "text", encoding = "UTF-8") + result <- fromJSON(content, simplifyVector = FALSE) + + # 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) # saving enviromental variable ---------------------------------------------- - if (x$status_code == 200){ + 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.")) + } else stop(paste0("Authorization error: HTTP status code ", response$status_code,". Check your api key.")) } diff --git a/R/rah_anchors.R b/R/rah_anchors.R index 16c5ee2..d0a701d 100644 --- a/R/rah_anchors.R +++ b/R/rah_anchors.R @@ -104,15 +104,16 @@ #' 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", diff --git a/tests/testthat/test_auth.R b/tests/testthat/test_auth.R new file mode 100644 index 0000000..64e6c98 --- /dev/null +++ b/tests/testthat/test_auth.R @@ -0,0 +1,54 @@ +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)) +}) + +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)) +}) + +# # api_key = NA +# # +# x <- httr::GET(url = 'https://apiv2.ahrefs.com/', +# httr::add_headers(token = Sys.getenv("AHREFS_AUTH_TOKEN"))) #"bullshit")) +# x$status_code +# y <- jsonlite::fromJSON(httr::content(x, as = "text")) +# "error" %in% names(jsonlite::fromJSON(httr::content(x, as = "text"))) +# +# response <- httr::GET(url = paste0( +# "https://apiv2.ahrefs.com/", +# "?token=", Sys.getenv("AHREFS_AUTH_TOKEN"), +# "&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)} +# )) +# +# stop_for_status(response) +# content <- httr::content(response, type = "text", encoding = "UTF-8") +# result <- jsonlite::fromJSON(content, simplifyVector = FALSE) +# +# 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) From 312a44f2ff89f42a2d6fbe3688cef54f714e4203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leszek=20Siemi=C5=84ski?= <32634801+Leszek-Sieminski@users.noreply.github.com> Date: Wed, 17 Jul 2019 11:07:12 +0200 Subject: [PATCH 6/9] fix appveyor --- appveyor.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 16d563e..096a343 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,9 +13,9 @@ # Download script file from GitHub init: ps: | - $ErrorActionPreference = "Stop" - Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1" - Import-Module '..\appveyor-tool.ps1' + $ErrorActionPreference = "Stop" + Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1" + Import-Module '..\appveyor-tool.ps1' install: ps: Bootstrap From c8114622124d1bd71ccdf24a7fd96c862daab3ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leszek=20Siemi=C5=84ski?= <32634801+Leszek-Sieminski@users.noreply.github.com> Date: Wed, 17 Jul 2019 11:27:20 +0200 Subject: [PATCH 7/9] cancel positive auth tests --- tests/testthat/test_auth.R | 40 ++++++-------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/tests/testthat/test_auth.R b/tests/testthat/test_auth.R index 64e6c98..3c63b67 100644 --- a/tests/testthat/test_auth.R +++ b/tests/testthat/test_auth.R @@ -18,37 +18,9 @@ test_that("api_key param doesn't accept wrong values", { expect_error(rah_auth(api_key = FALSE, verbose = FALSE)) }) -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)) -}) - -# # api_key = NA -# # -# x <- httr::GET(url = 'https://apiv2.ahrefs.com/', -# httr::add_headers(token = Sys.getenv("AHREFS_AUTH_TOKEN"))) #"bullshit")) -# x$status_code -# y <- jsonlite::fromJSON(httr::content(x, as = "text")) -# "error" %in% names(jsonlite::fromJSON(httr::content(x, as = "text"))) -# -# response <- httr::GET(url = paste0( -# "https://apiv2.ahrefs.com/", -# "?token=", Sys.getenv("AHREFS_AUTH_TOKEN"), -# "&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)} -# )) -# -# stop_for_status(response) -# content <- httr::content(response, type = "text", encoding = "UTF-8") -# result <- jsonlite::fromJSON(content, simplifyVector = FALSE) -# -# 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) +# 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)) +# }) From f9a17fc1cc1049881c53853839927dc022461858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leszek=20Siemi=C5=84ski?= <32634801+Leszek-Sieminski@users.noreply.github.com> Date: Wed, 17 Jul 2019 12:00:13 +0200 Subject: [PATCH 8/9] delete r project file --- RAhrefs.Rproj | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 RAhrefs.Rproj diff --git a/RAhrefs.Rproj b/RAhrefs.Rproj deleted file mode 100644 index 270314b..0000000 --- a/RAhrefs.Rproj +++ /dev/null @@ -1,21 +0,0 @@ -Version: 1.0 - -RestoreWorkspace: Default -SaveWorkspace: Default -AlwaysSaveHistory: Default - -EnableCodeIndexing: Yes -UseSpacesForTab: Yes -NumSpacesForTab: 2 -Encoding: UTF-8 - -RnwWeave: Sweave -LaTeX: pdfLaTeX - -AutoAppendNewline: Yes -StripTrailingWhitespace: Yes - -BuildType: Package -PackageUseDevtools: Yes -PackageInstallArgs: --no-multiarch --with-keep.source -PackageRoxygenize: rd,collate,namespace From 5a73717c3f0d3b010f8393192d42f6edfacb2894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leszek=20Siemi=C5=84ski?= <32634801+Leszek-Sieminski@users.noreply.github.com> Date: Wed, 17 Jul 2019 12:02:04 +0200 Subject: [PATCH 9/9] delete CI configs --- .travis.yml | 11 ----------- appveyor.yml | 41 ----------------------------------------- 2 files changed, 52 deletions(-) delete mode 100644 .travis.yml delete mode 100644 appveyor.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index d1dae89..0000000 --- a/.travis.yml +++ /dev/null @@ -1,11 +0,0 @@ -language: r -cache: packages -r_packages: - - covr - -script: - - R CMD build . - - R CMD check *tar.gz - -after_success: - - Rscript -e 'library(covr); codecov()' diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 096a343..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,41 +0,0 @@ -# before_build: -# ps: | -# cp ..\travis-tool.sh R-package\travis-tool.sh -# cp travis-tool.sh.cmd R-package\travis-tools.sh.cmd -# cd R-package -# bash -c "echo '^travis-tool\.sh\.cmd$' >> .Rbuildignore" -# build_script: -# - R CMD build . -# - R CMD check *tar.gz - -# DO NOT CHANGE the "init" and "install" sections below - -# Download script file from GitHub -init: - ps: | - $ErrorActionPreference = "Stop" - Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1" - Import-Module '..\appveyor-tool.ps1' - install: - ps: Bootstrap - - # Adapt as necessary starting from here - - #environment: - # global: - # WARNINGS_ARE_ERRORS: 1 - # _R_CHECK_SYSTEM_CLOCK_: FALSE - # - # matrix: - # - R_VERSION: 3.4.4 - # R_ARCH: x64 - # GCC_PATH: mingw_64 - - matrix: - fast_finish: true - - build_script: - - travis-tool.sh install_deps - - test_script: - - travis-tool.sh run_tests