Skip to content

Commit

Permalink
Merge pull request #18 from ScotGovAnalysis/dev-0.0.0.9004
Browse files Browse the repository at this point in the history
Fix bug in `check_pages` and other minor tweaks
  • Loading branch information
alice-hannah authored Aug 30, 2024
2 parents 88b9310 + b45bf58 commit 204b6d7
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
matrix:
config:
- {os: windows-latest, r: 'release'}
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-latest, r: 'release'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export(new_reply)
export(new_thread)
export(new_version)
export(objr)
export(objr_auth)
export(participant_bypass_2fa)
export(participants)
export(upload_file)
Expand Down
8 changes: 7 additions & 1 deletion R/assets.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ asset_info <- function(asset_uuid,
httr2::resp_body_json()

# Return useful information as list
response[c("uuid", "name", "type", "extension", "description")]
list(
uuid = response$uuid,
name = response$name,
type = response$type,
extension = response$extension,
description = response$description
)

}

Expand Down
7 changes: 3 additions & 4 deletions R/objr.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ objr <- function(endpoint,
#' token <- "test"
#' httr2::request("http://example.com") |> objr_auth()
#'
#' @export
#' @noRd

objr_auth <- function(req) {

Expand Down Expand Up @@ -227,9 +227,8 @@ error <- function(response) {

check_pages <- function(response, call = rlang::caller_env()) {

metadata <- if(httr2::resp_has_body(response)) {
try(httr2::resp_body_json(response)$metadata)
}
metadata <- tryCatch(httr2::resp_body_json(response)$metadata,
error = function(e) NULL)

if(!is.null(metadata)) {

Expand Down
39 changes: 39 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,42 @@ random_uuid <- function(seed = NULL) {
paste0(collapse = "-")

}


#' Check string is valid UUID format
#'
#' @param uuid Character string
#'
#' @return `uuid`, invisibly.
#'
#' @details See the [Getting Started vignette]
#' (https://scotgovanalysis.github.io/objr/
#' articles/objr.html#universally-unique-identifiers) for valid UUID format.
#'
#' @noRd

check_uuid <- function(uuid,
error_arg = rlang::caller_arg(uuid),
error_call = rlang::caller_env()) {

if(!rlang::is_string(uuid)) {
cli::cli_abort("{.arg {error_arg}} must be a string.",
call = error_call)
}

valid <- grepl(paste(rep("[A-Za-z0-9]{4}", 8), collapse = "-"),
uuid)

if(!valid) {
cli::cli_abort(c(
"{.arg {error_arg}} must be valid UUID format.",
"i" = paste0("See the {.href [Getting Started vignette]",
"(https://scotgovanalysis.github.io/objr/",
"articles/objr.html#universally-unique-identifiers)} ",
"for valid format.")
), call = error_call)
}

invisible(uuid)

}
13 changes: 4 additions & 9 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,19 @@ figures:

reference:

- title: API calls
contents:
- contents:
- objr
- my_user_id

- subtitle: Workspaces
- title: Workspaces
desc: >
Workspaces are the containers that connect a collection of documents and
folders (Assets) with a collection of Users (Participants)
contents:
- my_workspaces
- participants

- subtitle: Assets
- title: Assets
desc: Assets represent Documents and Folders that are added to Workspaces
contents:
- workspace_assets
Expand All @@ -67,17 +66,13 @@ reference:
- new_version
- delete_asset

- subtitle: Comments
- title: Comments
desc: View comments and create new threads and replies in workspaces
contents:
- comments
- new_thread
- new_reply

- title: API authentication
contents:
- objr_auth

- title: Two-factor authentication
contents:
- allow_bypass_2fa
Expand Down
29 changes: 0 additions & 29 deletions man/objr_auth.Rd

This file was deleted.

26 changes: 26 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,29 @@ test_that("Setting seed returns consistent value", {
)

})


# check_uuid ----

test_that("Function returns UUID invisibly", {

expect_invisible(check_uuid(random_uuid()))

uuid <- random_uuid(1)
expect_equal(uuid, random_uuid(1))

})

test_that("Error returned if string not supplied", {

expect_error(check_uuid(1))
expect_error(check_uuid(c(random_uuid(1), random_uuid(2))))

})

test_that("Error returned if string not valid UUID", {

expect_error(check_uuid("invalid_uuid"))
expect_error(check_uuid(substr(random_uuid(1), 1, 9)))

})

0 comments on commit 204b6d7

Please sign in to comment.