-
Notifications
You must be signed in to change notification settings - Fork 0
/
suso_gql_credentials.R
94 lines (82 loc) · 2.33 KB
/
suso_gql_credentials.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#' Utility function to check if credentials are correct
#'
#' This function returns a 200 status code if credentials are correct and a 400 code otherwise.
#'
#' @param endpoint Survey Solutions GraphQl
#' @param user API user
#' @param password API password
#' @param workspace endpoint workspace Name, if nothing provided, defaults to primary
#' @param token If Survey Solutions endpoint token is provided \emph{user} and \emph{password} will be ignored
#'
#' @return 200 code if correct, 400 if incorrect.
#'
#'
#' @examples
#'
#'
#' # With default credentials returns 400
#' suso_gql_pwcheck()
#'
#'
#'
#' @export
suso_gql_pwcheck<-function(endpoint=NULL,
user=NULL,
password=NULL,
workspace = NULL,
token = NULL) {
if(is.null(endpoint) | is.null(user) | is.null(password)) {
# return 400 if any is missing
return(400)
}
# also skip when empty string
if((endpoint=="") | (user=="") | (password=="")) {
# return 400 if any is missing
return(400)
}
## workspace default
workspace<-.ws_default(ws = workspace)
# check (.helpers.R)
.check_basics(token, endpoint, user, password)
query <- sprintf('
query($workspace: String $where: InterviewsFilter $order: [InterviewSort!] $take: Int $skip: Int){
interviews(workspace: $workspace where: $where order: $order take: $take skip: $skip) {
totalCount
filteredCount
}
}
')
variables <- list()
if (!is.null(workspace)) {
variables$workspace <- workspace
}
# 2. Filter
# 2.1 Filter default is NULL
variables$where<-NULL
variables$take<-1
# create the body of the request
body <- list(query = query)
if (!is.null(variables)) {
body$variables <- variables
}
# Build the URL, first for token, then for base auth
if(!is.null(token)){
#url<-.baseurl_token(endpoint, body, token, retry = 3)
} else {
url<-.baseurl_baseauth(endpoint, body, user, password, retry = 3)
}
# perform the request
result<-tryCatch(
{checker<-function(url) {
rq<-.perform_request(url)
if(is.numeric(rq$data$interviews$totalCount)) {
return(200)
} else {
return(400)
}
}
checker(url)},
error = function(e) return(400)
)
return(result)
}