Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
selesnow authored Dec 5, 2018
1 parent 33a3a75 commit 394e4f2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 37 deletions.
12 changes: 6 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Package: ryandexdirect
Type: Package
Title: Load data from Yandex Direct
Version: 3.0.9.0
Date: 2018-10-25
Version: 3.0.10.0
Date: 2018-12-05
Author: Alexey Seleznev
Maintainer: Alexey Seleznev <[email protected]>
Description: Load data from yandex direct api into R
License: not license it free
Depends: R (>= 3.4.1)
Description: Load data from yandex direct api v5 into R
License: GPL-2
Depends: R (>= 3.5.1)
Encoding: CP1251
Imports: utils, httr, RCurl, bitops, jsonlite, xml2, data.table, readr, magrittr
Imports: utils, httr, RCurl, bitops, jsonlite, xml2, data.table, readr, magrittr, dplyr
110 changes: 79 additions & 31 deletions R/yadirGetClientList.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,82 @@ yadirGetClientList <-
function(AgencyAccount = NULL,
Token = NULL,
TokenPath = getwd()){

#Àâòîðèçàöèÿ
Token <- tech_auth(login = Logins[l], token = Token, AgencyAccount = AgencyAccount, TokenPath = TokenPath)
#Create POST request
answer <- POST("https://api.direct.yandex.ru/v4/json/", body = paste0("{\"method\": \"GetClientsList\", \"locale\": \"ru\", \"token\": \"",Token,"\"}"))
#Send POST request
stop_for_status(answer)
dataRaw <- content(answer, "parsed", "application/json")
#Create result data frame
data <- data.frame()
for (i in 1:length(dataRaw$data)){
#Replace NULL to NA
dataRaw$data[[i]][c('FIO','Login','DateCreate','Phone','Email','Role','StatusArch')][sapply(dataRaw$data[[i]][c('FIO','Login','DateCreate','Phone','Email','Role','StatusArch')], is.null)] <- NA
dataTemp <- data.frame(t(as.data.frame(unlist(dataRaw$data[[i]][c('FIO','Login','DateCreate','Phone','Email','Role','StatusArch')],recursive = T))),row.names = NULL)
data <- rbind(data, dataTemp)
}
dataTotal <- data.frame(
Login = as.character(data$Login),
FIO = as.character(data$FIO),
StatusArch = as.factor(data$StatusArch),
DateCreate = as.POSIXct(data$DateCreate),
Role = as.factor(data$Role),
Email = as.character(data$Email),
Phone = as.character(data$Phone)
);
dataTotal$Login = as.character(dataTotal$Login)
dataTotal$FIO = as.character(dataTotal$FIO)
dataTotal$Email = as.character(dataTotal$Email)
dataTotal$Phone = as.character(dataTotal$Phone)
return(dataTotal)
}

# authorize
Token <- tech_auth(login = Logins[l], token = Token, AgencyAccount = AgencyAccount,
TokenPath = TokenPath)

# prepare query body
q <- list(method = "get",
params = list(SelectionCriteria = NULL,
FieldNames = c( "AccountQuality",
"Archived",
"ClientId",
"ClientInfo",
"CountryId",
"CreatedAt",
"Currency",
"Grants",
"Login",
"Notification",
"OverdraftSumAvailable",
"Phone",
"Representatives",
"Restrictions",
"Settings",
"Type",
"VatRate")))
# convert body to json format
q_json <- toJSON(q, auto_unbox = T)

# send HTTP query
answer <- POST("https://api.direct.yandex.com/json/v5/agencyclients",
body = q_json,
add_headers(Authorization = paste0("Bearer ",Token),
'Accept-Language' = "ru"))

# check query for error
stop_for_status(answer)
dataRaw <- content(answer, "parsed", "application/json")

# check answer for error
if ( !is.null(dataRaw$error ) ) {
stop(dataRaw$error$error_detail,
call. = cat("Error detail:\n",
"request_id =",dataRaw$error$request_id, "\n",
"error_code =",dataRaw$error$error_code, "\n",
"error_detail =",dataRaw$error$error_detail, "\n",
"error_string =",dataRaw$error$error_string, "\n"))
}

# result data
data <- list()

# parse raw data
for (i in dataRaw$result$Clients) {

data <- append(data, list(
list(Login = i$Login,
FIO = i$ClientInfo,
StatusArch = i$Archived,
DateCreate = i$CreatedAt,
Role = i$Type,
Email = i$Notification$Email,
Phone = i$Phone,
Currency = i$Currency,
VatRate = i$VatRate,
ClientId = i$ClientId,
CountryId = i$CountryId,
AccountQuality = ifelse( is.null(i$AccountQuality), NA, i$AccountQuality),
Grants = paste0(lapply(i$Grants, function(x) paste0(x[["Agency"]], ": ", x[["Privilege"]], " - ", x[["Value"]])), collapse = "; "),
Representatives = paste0(lapply(i$Representatives, function( x ) paste0(x[["Login"]], ": ", x[["Role"]])), collapse = "; "),
Restrictions = paste0(lapply(i$Restrictions, function(x) paste0(x[["Element"]], ": ", x[["Value"]])), collapse = "; "),
Setting = paste0(lapply(i$Settings, function(x) paste0(x[["Option"]], ": ", x[["Value"]])), collapse = "; "))))
}

# binding to data frame
dataTotal <- bind_rows(data)

return(dataTotal)
}

0 comments on commit 394e4f2

Please sign in to comment.