This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathazure_request.R
126 lines (120 loc) · 3.31 KB
/
azure_request.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
AzureRequestV2 <- R6::R6Class(
"AzureRequestV2",
public = list(
method = NULL,
path = NULL,
headers = NULL,
query = NULL,
body = NULL,
# Httr Requests Configuration
write = NULL,
progress = NULL,
verbose = NULL,
content = NULL,
initialize = function(method = NULL,
path = NULL,
headers = NULL,
query = NULL,
body = NULL,
write = NULL,
progress = NULL,
verbose = NULL,
content = NULL) {
self$method <- method
self$path <- path
self$headers <- headers
if (is.null(self$headers)) {
self$headers <- character()
}
self$query <- query
self$body <- body
self$write <- write
self$progress <- progress
self$verbose <- verbose
self$content <- content
},
encryptSignature = function(x, key) {
undecodedKey <- RCurl::base64Decode(key, mode = "raw")
RCurl::base64(digest::hmac(
key = undecodedKey,
object = enc2utf8(x),
algo = "sha256",
raw = TRUE
))
}
)
)
AzureServiceClient <- R6::R6Class(
"AzureServiceClient",
public = list(
authentication = NULL,
apiVersion = NULL,
verbose = FALSE,
initialize = function(url = NA, authentication = NA) {
self$url <- url
self$authentication <- authentication
},
executeRequest = function(url, request) {
requestHeaders <- httr::add_headers(request$headers)
verbose <- options("azureHttpTraffic")
if (!is.null(verbose$azureHttpTraffic)
&& verbose$azureHttpTraffic == TRUE) {
request$verbose <- httr::verbose()
}
if (request$method == "GET" ||
request$method == "POST" ||
request$method == "DELETE" ||
request$method == "PUT" ||
request$method == "PATCH") {
httr::VERB(
request$method,
url,
config = requestHeaders,
body = request$body,
query = request$query,
encode = "json",
request$write,
request$verbose,
request$progress
)
}
else if (request$method == "HEAD") {
httr::HEAD(
url,
config = requestHeaders,
body = request$body,
query = request$query,
encode = "json",
request$write,
request$verbose,
request$progress
)
}
else {
stop(
sprintf(
"This HTTP Verb is not found: %s - Please try again with GET, POST, HEAD, PUT, PATCH or DELETE",
request$method
)
)
}
},
extractAzureResponse = function(response, content) {
httr::warn_for_status(response)
if (is.null(content)) {
httr::content(response, encoding = "UTF-8")
}
else if (content %in% c("raw", "text", "parsed")) {
httr::content(response, content, encoding = "UTF-8")
}
else if (content == "response") {
response
}
# Legacy code: By default it will, automatically attempt
# figure out which one is most appropriate
else {
httr::content(response, encoding = "UTF-8")
}
}
)
)