-
Notifications
You must be signed in to change notification settings - Fork 37
/
client.R
128 lines (117 loc) · 3.49 KB
/
client.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
127
128
#' @include credentials.R
#' @include handlers.R
#' @include struct.R
NULL
# A Config object provides service configuration for service clients.
Config <- struct(
credentials = Credentials(),
endpoint = "",
endpoint_resolver = "",
enforce_should_retry_check = FALSE,
region = "",
disable_ssl = FALSE,
close_connection = FALSE,
max_retries = -1,
timeout = 60,
retryer = NULL,
disable_param_validation = FALSE,
disable_compute_checksums = FALSE,
s3_force_path_style = FALSE,
s3_disable_100_continue = FALSE,
s3_use_accelerate = FALSE,
s3_disable_content_md5_validation = FALSE,
ec2_metadata_disable_timeout_override = FALSE,
use_dual_stack = FALSE,
sleep_delay = NULL,
disable_rest_protocol_uri_cleaning = FALSE
)
# A Session object stores configuration and request handlers for a service.
Session <- struct(
config = Config(),
handlers = Handlers()
)
# A ClientConfig object stores the information required to configure a service
# client instance.
ClientConfig <- struct(
config = Config(),
handlers = Handlers(),
endpoint = "",
signing_region = "",
signing_name = "",
signing_name_derived = FALSE
)
# A ClientInfo object stores immutable data about a service.
ClientInfo <- struct(
service_name = "",
service_id = "",
api_version = "",
endpoint = "",
signing_name = "",
signing_region = "",
json_version = "",
target_prefix = ""
)
# A Client object stores everything needed to create a service client.
Client <- struct(
retryer = NULL,
client_info = ClientInfo(),
config = Config(),
handlers = Handlers()
)
#-------------------------------------------------------------------------------
# new_session returns a Session with default configuration.
new_session <- function() {
s <- Session()
return(s)
}
# resolver_endpoint returns the endpoint for a given service.
# e.g. "https://ec2.us-east-1.amazonaws.com"
resolver_endpoint <- function(service, region, endpoints, scheme = "https") {
get_region_pattern <- function(region, endpoints) {
patterns <- names(endpoints)
matches <- patterns[sapply(patterns, function(pattern) grepl(pattern, region))]
match <- matches[order(nchar(matches), decreasing = TRUE)][1]
return(match)
}
e <- endpoints[[get_region_pattern(region, endpoints)]]
# TODO: Delete old endpoint format handling once all packages are updated.
if (is.character(e)) {
e <- list(endpoint = e, global = FALSE)
}
endpoint <- gsub("{service}", service, e$endpoint, fixed = TRUE)
endpoint <- gsub("{region}", region, endpoint, fixed = TRUE)
endpoint <- gsub("^(.+://)?", sprintf("%s://", scheme), endpoint)
signing_region <- ifelse(e$global, "us-east-1", region)
return(list(
endpoint = endpoint,
signing_region = signing_region
))
}
# client_config returns a ClientConfig configured for the service.
client_config <- function(service_name, endpoints, cfgs) {
s <- new_session()
if (!is.null(cfgs)) {
s$config <- cfgs
}
# If region not defined, set it
if (nchar(s$config$region) == 0) {
s$config$region <- get_region(cfgs$credentials$profile)
}
region <- s$config$region
if (s$config$endpoint != "") {
endpoint <- s$config$endpoint
signing_region <- region
} else {
e <- resolver_endpoint(service_name, region, endpoints)
endpoint <- e$endpoint
signing_region <- e$signing_region
}
c <- ClientConfig(
config = s$config,
handlers = s$handlers,
endpoint = endpoint,
signing_region = signing_region,
signing_name = service_name
)
return(c)
}