Skip to content

Commit

Permalink
paws.common prepare for cran 0.7.5 release
Browse files Browse the repository at this point in the history
  • Loading branch information
DyfanJones committed Aug 19, 2024
2 parents 355870e + 12272f5 commit fc1f3a0
Show file tree
Hide file tree
Showing 20 changed files with 192 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:

- name: Style
run: |
styler::style_pkg(pkg = 'paws.common')
styler::style_pkg(pkg = 'paws.common', exclude_files = c("R/RcppExports\\.R", "R/cpp11\\.R", "R/import-standalone.*\\.R", "R/service_parameter_helper\\.R"))
styler::style_pkg(pkg = 'make.paws')
shell: Rscript {0}

Expand Down
7 changes: 4 additions & 3 deletions make.paws/R/sdk_helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ paws_check_rhub_sub_cat <- function(in_dir = "../cran",

#' @rdname paws_check_rhub
#' @export
paws_rhub_action_check <- function (packages = "paws", platforms = c("linux", "macos", "macos-arm64", "windows")) {
paws_rhub_action_check <- function(packages = "paws", platforms = c("linux", "macos", "macos-arm64", "windows")) {
url <- "https://api.github.com/repos/paws-r/paws-rhub/actions/workflows/rhub.yaml/dispatches"
pat <- gitcreds::gitcreds_get(url = "https://github.com/paws-r/paws-rhub")$password
config <- list(platforms = platforms)
Expand All @@ -143,10 +143,11 @@ paws_rhub_action_check <- function (packages = "paws", platforms = c("linux", "m
httr2::req_method("POST")
}) |> httr2::req_perform_parallel()
names(resps) <- unlist(packages)

for (pkg in names(resps)) {
if (resps[[pkg]]$status_code != 204)
if (resps[[pkg]]$status_code != 204) {
stop(sprintf("Failed to start rhub action for package: %s", pkg))
}
}
writeLines("Please check results: https://github.com/paws-r/paws-rhub/actions")
invisible(NULL)
Expand Down
1 change: 1 addition & 0 deletions paws.common/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ useDynLib(paws.common,"_paws_common_endpoint_unescape")
useDynLib(paws.common,"_paws_common_get_region_pattern")
useDynLib(paws.common,"_paws_common_json_convert_string")
useDynLib(paws.common,"_paws_common_paws_url_encoder")
useDynLib(paws.common,"_paws_common_uuid_v4")
2 changes: 2 additions & 0 deletions paws.common/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ client$copy_object(
)
```

* convert `uuid` to `cpp` for performance improvement.

# paws.common 0.7.4
* fix `transpose` to correctly parse lists with empty first elements (#791), thanks to @FMKerckhof for raising issue.
* support refreshable credentials for `sso` (#793)
Expand Down
6 changes: 6 additions & 0 deletions paws.common/R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ char_sort <- function(str) {
.Call('_paws_common_char_sort', PACKAGE = 'paws.common', str)
}

#' @useDynLib paws.common _paws_common_uuid_v4
#' @importFrom Rcpp evalCpp
uuid_v4 <- function(n = 1L) {
.Call('_paws_common_uuid_v4', PACKAGE = 'paws.common', n)
}

3 changes: 2 additions & 1 deletion paws.common/R/client.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ resolver_endpoint <- function(service, region, endpoints, sts_regional_endpoint
}
signing_region <- if (e[["global"]]) "us-east-1" else region
endpoint <- endpoint_unescape(e[["endpoint"]], service, signing_region)
if (grepl(HOST_PREFIX_RE, host_prefix))
if (grepl(HOST_PREFIX_RE, host_prefix)) {
endpoint <- sprintf("%s%s", host_prefix, endpoint)
}
endpoint <- gsub("^(.+://)?", sprintf("%s://", scheme), endpoint)

return(list(
Expand Down
28 changes: 14 additions & 14 deletions paws.common/R/custom_s3.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#' @include head_bucket.R
NULL

VERSION_ID_SUFFIX <- '?versionId='
VERSION_ID_SUFFIX <- "?versionId="
ACCESSPOINT_ARN <- paste0(
"^arn:(aws).*:(s3|s3-object-lambda):[a-z\\-0-9]*:[0-9]{12}:accesspoint[/:]",
"[a-zA-Z0-9\\-.]{1,63}$"
)
OUTPOST_ARN = paste0(
'^arn:(aws).*:s3-outposts:[a-z\\-0-9]+:[0-9]{12}:outpost[/:]',
'[a-zA-Z0-9\\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\\-]{1,63}$'
OUTPOST_ARN <- paste0(
"^arn:(aws).*:s3-outposts:[a-z\\-0-9]+:[0-9]{12}:outpost[/:]",
"[a-zA-Z0-9\\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\\-]{1,63}$"
)

VALID_S3_ARN <- paste(ACCESSPOINT_ARN, OUTPOST_ARN, sep = "|")
Expand Down Expand Up @@ -499,26 +499,26 @@ handle_copy_source_param <- function(request) {
quote_source_header <- function(source) {
result <- strsplit(source, VERSION_ID_SUFFIX, fixed = T)[[1]]
if (is.na(result[2])) {
return(paws_url_encoder(result[1], '-._~/'))
return(paws_url_encoder(result[1], "/"))
} else {
return(paste0(paws_url_encoder(result[1], '-._~/'), VERSION_ID_SUFFIX, result[2]))
return(paste0(paws_url_encoder(result[1], "/"), VERSION_ID_SUFFIX, result[2]))
}
}

quote_source_header_from_list <- function(source) {
if(is.null(bucket <- source[['Bucket']])) {
stopf('CopySource list is missing required parameter: Bucket')
if (is.null(bucket <- source[["Bucket"]])) {
stopf("CopySource list is missing required parameter: Bucket")
}
if (is.null(key <- source[['Key']])) {
stopf('CopySource list is missing required parameter: Key')
if (is.null(key <- source[["Key"]])) {
stopf("CopySource list is missing required parameter: Key")
}
if (grepl(VALID_S3_ARN, bucket, perl = T)) {
final <- sprintf('%s/object/%s', bucket, key)
final <- sprintf("%s/object/%s", bucket, key)
} else {
final <- sprintf('%s/%s', bucket, key)
final <- sprintf("%s/%s", bucket, key)
}
final <- paws_url_encoder(final, '-._~/')
if (!is.null(version_id <- source[['VersionId']])) {
final <- paws_url_encoder(final, "/")
if (!is.null(version_id <- source[["VersionId"]])) {
final <- paste0(final, VERSION_ID_SUFFIX, version_id)
}
return(final)
Expand Down
2 changes: 1 addition & 1 deletion paws.common/R/error.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ serialization_error <- function(request) {
)
}

get_retry_info <- function(retry_count){
get_retry_info <- function(retry_count) {
if (retry_count) {
retry_msg <- sprintf(" (reached max retries: %d)", retry_count)
} else {
Expand Down
19 changes: 3 additions & 16 deletions paws.common/R/idempotency.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @include RcppExports.R

IDEMPOTENCY_TOKEN_FILL_TAG <- "idempotencyToken"
IDEMPOTENCY_RAND_FN <- function(len) sample(0:(2^8 - 1), len, replace = TRUE)

# Return whether the idempotency token can be automatically set.
can_set_idempotency_token <- function(value) {
Expand All @@ -8,19 +9,5 @@ can_set_idempotency_token <- function(value) {

# Return a randomly-generated idempotency token.
get_idempotency_token <- function() {
rand <- getOption("idempotency_rand_fn", default = IDEMPOTENCY_RAND_FN)
return(uuid(rand(16)))
}

# Return a UUID version 4 based on the given `bytes`.
uuid <- function(bytes) {
bytes[7] <- bitwAnd(bitwOr(bytes[7], 0x40), 0x4F)
bytes[9] <- bitwAnd(bitwOr(bytes[9], 0x80), 0xBF)
h <- sprintf("%02X", bytes)
p <- function(x) paste(x, collapse = "")
u <- sprintf(
"%s-%s-%s-%s-%s",
p(h[1:4]), p(h[5:6]), p(h[7:8]), p(h[9:10]), p(h[11:16])
)
return(u)
return(uuid_v4(1))
}
4 changes: 2 additions & 2 deletions paws.common/R/net.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ issue <- function(http_request) {

resp_body <- function(resp, path) {
if (is.null(path)) {
body <- httr::content(resp, as = "raw")
# return error message if call has failed or needs redirecting
body <- resp$content
# return error message if call has failed or needs redirecting
} else if (resp$status_code %in% c(301, 400)) {
body <- readBin(path, "raw", file.info(path)$size)
unlink(path)
Expand Down
3 changes: 1 addition & 2 deletions paws.common/R/queryutil.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ query_parse_structure <- function(values, value, prefix, tag, is_ec2 = FALSE) {
}

if (can_set_idempotency_token(field)) {
token <- get_idempotency_token()
field[[1]] <- token
field[[1]] <- get_idempotency_token()
}

name <- ""
Expand Down
13 changes: 3 additions & 10 deletions paws.common/R/signer_v4.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ sign_sdk_request_with_curr_time <- function(request,

# set headers for anonymous credentials
if (isTRUE(request$config$credentials$anonymous)) {
request$http_request$header <- anonymous_headers(
request$http_request$header
)
# Clear down headers for anonymous credentials
# https://github.com/aws/aws-sdk-go/blob/a7b02935e4fefa40f175f4d2143ec9c88a5f90f5/aws/signer/v4/v4_test.go#L321-L355
request$http_request$header["Authorization"] <- ""
}

return(request)
Expand Down Expand Up @@ -462,10 +462,3 @@ get_uri_path <- function(url) {

return(uri)
}

# Clear down headers for anonymous credentials
# https://github.com/aws/aws-sdk-go/blob/a7b02935e4fefa40f175f4d2143ec9c88a5f90f5/aws/signer/v4/v4_test.go#L321-L355
anonymous_headers <- function(headers) {
headers["Authorization"] <- ""
return(headers)
}
12 changes: 12 additions & 0 deletions paws.common/src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// uuid_v4
CharacterVector uuid_v4(size_t n);
RcppExport SEXP _paws_common_uuid_v4(SEXP nSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< size_t >::type n(nSEXP);
rcpp_result_gen = Rcpp::wrap(uuid_v4(n));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_paws_common_paws_url_encoder", (DL_FUNC) &_paws_common_paws_url_encoder, 2},
Expand All @@ -88,6 +99,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_paws_common_endpoint_unescape", (DL_FUNC) &_paws_common_endpoint_unescape, 3},
{"_paws_common_get_region_pattern", (DL_FUNC) &_paws_common_get_region_pattern, 2},
{"_paws_common_char_sort", (DL_FUNC) &_paws_common_char_sort, 1},
{"_paws_common_uuid_v4", (DL_FUNC) &_paws_common_uuid_v4, 1},
{NULL, NULL, 0}
};

Expand Down
20 changes: 20 additions & 0 deletions paws.common/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Rcpp.h>
#include "uuid.h"
using namespace Rcpp;

// Sort character vector
Expand All @@ -11,3 +12,22 @@ CharacterVector char_sort(CharacterVector str) {
std::sort(idx.begin(), idx.end(), [&](int i, int j){return str[i] < str[j];});
return str[idx];
}


// Creates uuid v4
// Developed from: https://github.com/rkg82/uuid-v4
// param n A integer, number of uuid v4 to generate.
//' @useDynLib paws.common _paws_common_uuid_v4
//' @importFrom Rcpp evalCpp
// [[Rcpp::export]]
CharacterVector uuid_v4(size_t n = 1) {
CharacterVector uuids(n);

for (size_t i = 0; i < n; ++i) {
std::string s = uuid::v4::UUID::New().String();
uuids[i] = s;
}

return uuids;
}

58 changes: 58 additions & 0 deletions paws.common/src/uuid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef __UUID__
#define __UUID__

#include <random>
#include <string>

namespace uuid::v4
{
// Encaasulate the genaeration of a Version 4 UUID object
// A Version 4 UUID is a universally unique identifier that is generated using random numbers.
class UUID
{
public:
// Factory method for creating UUID object.
static UUID New()
{
UUID uuid;
std::random_device rd;
std::mt19937 engine{rd()};
std::uniform_int_distribution<int> dist{0, 256}; //Limits of the interval

for (int index = 0; index < 16; ++index)
{
uuid._data[index] = (unsigned char)dist(engine);
}

uuid._data[6] = ((uuid._data[6] & 0x0f) | 0x40); // Version 4
uuid._data[8] = ((uuid._data[8] & 0x3f) | 0x80); // Variant is 10

return uuid;
}

// Returns UUID as formatted string
std::string String()
{
// Formats to "0065e7d7-418c-4da4-b4d6-b54b6cf7466a"
char buffer[256] = {0};
std::snprintf(buffer, 255,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
_data[0], _data[1], _data[2], _data[3],
_data[4], _data[5],
_data[6], _data[7],
_data[8], _data[9],
_data[10], _data[11], _data[12], _data[13], _data[14], _data[15]);

std::string uuid = buffer;

return uuid;
}

private:
UUID() {}

unsigned char _data[16] = {0};
};
};

#endif // #ifndef __UUID__
Loading

0 comments on commit fc1f3a0

Please sign in to comment.