Skip to content

Commit

Permalink
Merge pull request #15 from andodet/dev
Browse files Browse the repository at this point in the history
CRAN 0.0.3
  • Loading branch information
andodet committed Jan 16, 2022
2 parents d14dda8 + a658d34 commit 70ead40
Show file tree
Hide file tree
Showing 25 changed files with 189 additions and 36 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: googlePubsubR
Title: R Interface for Google 'Cloud Pub/Sub' REST API
Version: 0.0.2
Version: 0.0.3
Authors@R:
person(given = "Andrea",
family = "Dodet",
Expand All @@ -26,7 +26,7 @@ Suggests:
promises
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
VignetteBuilder: knitr
Config/testthat/edition: 3
Config/testthat/parallel: false
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export(Subscription)
export(Topic)
export(msg_decode)
export(msg_encode)
export(ps_project_get)
export(ps_project_set)
export(pubsub_auth)
export(schemas_create)
export(schemas_delete)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# googlePubsubR 0.0.3

* Add helper methods (`ps_project_set/get`) to switch between GCP projects (thanks MarkEdmonson1234,
#13).
* Quieter warnings when env variables are not set (#13)
* Deleted extra `name` argument from `topics_create` (thanks muschellij2)

# googlePubsubR 0.0.2

* Add helpers to encode/decode messages: `msg_encode`, `msg_decode`.
Expand Down
1 change: 1 addition & 0 deletions R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' @return None, called for side effects
#'
#' @import googleAuthR
#' @family Auth functions
#' @export
pubsub_auth <- function(json_file = Sys.getenv("GCP_AUTH_FILE"),
token = NULL,
Expand Down
10 changes: 5 additions & 5 deletions R/schemas.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @return `character`
#' @noRd
#' @keywords internal
as.schema_name <- function(x, project = Sys.getenv("GCP_PROJECT")) {
as.schema_name <- function(x, project = ps_project_get()) {
# Can it be done with a switch case?
if (is.character(x) && x != "") {
if (already_formatted(x)) {
Expand Down Expand Up @@ -42,7 +42,7 @@ as.schema_name <- function(x, project = Sys.getenv("GCP_PROJECT")) {
schemas_create <- function(name,
type = c("AVRO", "PROTOCOL_BUFFER", "TYPE_UNSPECIFIED"),
definition,
project = Sys.getenv("GCP_PROJECT")) {
project = ps_project_get()) {
schema_name <- as.schema_name(name)
schema <- Schema(
type = type,
Expand Down Expand Up @@ -72,7 +72,7 @@ schemas_create <- function(name,
#' @importFrom googleAuthR gar_api_generator
#' @family Schema functions
#' @export
schemas_validate <- function(schema, project = Sys.getenv("GCP_PROJECT")) {
schemas_validate <- function(schema, project = ps_project_get()) {
parent <- sprintf("projects/%s", project)
body <- list(
schema = schema
Expand Down Expand Up @@ -102,7 +102,7 @@ schemas_validate <- function(schema, project = Sys.getenv("GCP_PROJECT")) {
#' @importFrom googleAuthR gar_api_generator
#' @family Schema functions
#' @export
schemas_list <- function(project = Sys.getenv("GCP_PROJECT"), pageSize = NULL,
schemas_list <- function(project = ps_project_get(), pageSize = NULL,
view = c("SCHEMA_VIEW_UNSPECIFIED", "BASIC", "FULL"),
pageToken = NULL) {
view <- match.arg(view)
Expand Down Expand Up @@ -196,7 +196,7 @@ schemas_delete <- function(name) {
schemas_validate_message <- function(schema,
message,
encoding = c("ENCODING_UNSPECIFIED", "JSON", "BINARY"),
project = Sys.getenv("GCP_PROJECT")) {
project = ps_project_get()) {

# API expects a base64 encoded string, extract it from the message object
if (inherits(message, "PubsubMessage")) {
Expand Down
4 changes: 2 additions & 2 deletions R/snapshots.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @return `character`
#' @noRd
#' @keywords internal
as.snapshot_name <- function(x, project = Sys.getenv("GCP_PROJECT")) {
as.snapshot_name <- function(x, project = ps_project_get()) {
if (is.character(x) && x != "") {
if (already_formatted(x)) {
out <- x
Expand Down Expand Up @@ -98,7 +98,7 @@ snapshots_delete <- function(snapshot) {
#' @importFrom googleAuthR gar_api_generator
#' @family Snapshot functions
#' @export
snapshots_list <- function(project = Sys.getenv("GCP_PROJECT"), pageSize = NULL,
snapshots_list <- function(project = ps_project_get(), pageSize = NULL,
pageToken = NULL) {

url <- sprintf("https://pubsub.googleapis.com/v1/projects/%s/snapshots", project)
Expand Down
2 changes: 1 addition & 1 deletion R/subscriptions.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @return `character`
#' @noRd
#' @keywords internal
as.sub_name <- function(x, project = Sys.getenv("GCP_PROJECT")) {
as.sub_name <- function(x, project = ps_project_get()) {
# Can it be done with a switch case?
if (is.character(x) && x != "") {
if (already_formatted(x)) {
Expand Down
9 changes: 4 additions & 5 deletions R/topics.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @return (`character`)
#' @keywords internal
#' @noRd
as.topic_name <- function(x, project = Sys.getenv("GCP_PROJECT")) {
as.topic_name <- function(x, project = ps_project_get()) {
if (is.character(x) && x != "") {
if (already_formatted(x)) {
out <- x
Expand Down Expand Up @@ -64,8 +64,7 @@ topics_create <- function(name,
satisfies_pzs = satisfies_pzs,
message_storage_policy = message_storage_policy,
schema_settings = schema_settings,
message_retention_duration = message_retention_duration,
name = topic_name
message_retention_duration = message_retention_duration
)

url <- sprintf("https://pubsub.googleapis.com/v1/%s", topic_name)
Expand Down Expand Up @@ -133,7 +132,7 @@ topics_get <- function(topic) {
#' @importFrom googleAuthR gar_api_generator
#' @family Topic functions
#' @export
topics_list <- function(project = Sys.getenv("GCP_PROJECT"), pageSize = NULL,
topics_list <- function(project = ps_project_get(), pageSize = NULL,
pageToken = NULL) {
url <- sprintf("https://pubsub.googleapis.com/v1/projects/%s/topics/", project)
pars <- list(pageSize = pageSize, pageToken = pageToken)
Expand All @@ -154,7 +153,7 @@ topics_list <- function(project = Sys.getenv("GCP_PROJECT"), pageSize = NULL,
#' @return `logical`, TRUE if topic exists, FALSE otherwise
#' @family Topic functions
#' @export
topics_exists <- function(topic, project = Sys.getenv("GCP_PROJECT")) {
topics_exists <- function(topic, project = ps_project_get()) {
topic <- as.topic_name(topic)
all_topics <- topics_list(project)
if (any(grepl(topic, all_topics$topics$name))) {
Expand Down
51 changes: 51 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ msg_decode <- function(x) {
#'
#' @examples
#' \dontrun{
#' library(jsonlite)
#'
#' mtcars %>%
#' toJSON(auto_unbox = TRUE) %>%
#' msg_encode() %>%
#' PubsubMessage()
#' }
Expand All @@ -73,6 +76,54 @@ msg_encode <- function(x) {
base64enc::base64encode()
}

.ps_env <- new.env(parent = emptyenv())
#' Set GCP projectId
#'
#' @param project_id `character` A valid GCP projectId
#'
#' @examples
#' \dontrun{
#' ps_project_set("my-new-project")
#' # Do whatever...
#' # Jump back on the default project
#' ps_project_set(Sys.getenv("GCP_PROJECT"))
#' }
#'
#' @return `character` ProjectId string
#' @family Auth functions
#' @export
ps_project_set <- function(project_id) {
if(project_id == "") {
stop("You must pass a valid GCP projectId string")
}
.ps_env$project <- project_id
cli::cli_alert_info("GCP project successfully set!")
.ps_env$project
}

#' Get GCP projectId
#'
#' @return `character` A valid GCP projectId, defaults to `GCP_PROJECT` env var
#' @family Auth functions
#' @export
ps_project_get <- function() {
# Fallback logic taken from `{googleCloudRunner/R/init.R}`
if(!is.null(.ps_env$project)) {
return(.ps_env$project)
}

if(Sys.getenv("GCP_PROJECT") != "") {
.ps_env$project <- Sys.getenv("GCP_PROJECT")
}
if(is.null(.ps_env$project)) {
stop("No projectId set - use ps_project_set() or set GCP_PROJECT env var",
call. = FALSE
)
}

return(.ps_env$project)
}

#' Pipe operator
#'
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
Expand Down
7 changes: 4 additions & 3 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
.onLoad <- function(libname, pkgname) {
.onAttach <- function(libname, pkgname) {
# Check if necessary env variables have been set
if(Sys.getenv("GCP_PROJECT") == "") {
cli::cli_alert_warning(
"GCP_PROJECT environment not found, please set it up before starting!"
)
}


if(Sys.getenv("GCP_AUTH_FILE") == "") {
cli::cli_alert_warning(
"GCP_AUTH_FILE environment not found, please set it up before authenticating"
)
"googlePubsubR: GCP_AUTH_FILE environment variable not found, please set it up before
authenticating")
}
}
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ In order to use the library, you will need:
* The Pub/Sub API correctly activated
* JSON credentials for a service account or another method of authentication (e.g token). You can pass the
path of the file as an argument to `pubsub_auth` or setting an `GCP_AUTH_FILE` env variable.
* A `GCP_PROJECT` env variable set with a valid GCP project id
* A `GCP_PROJECT` env variable set with a valid GCP project id. Since `0.0.3`, GCP project id can also be set
using `ps_project_set`.

## Usage

Expand Down Expand Up @@ -99,3 +100,18 @@ The main use-cases for Pub/Sub messaging queue:
* Trigger workflows hosted in Cloud Run or Cloud Functions
* Expand interactivity in Shiny dashboards (more on this [here](inst/shiny/consumer_example/readme.md)).
* Add event driven actions in [`{plumbr}`](https://www.rplumber.io/)

## Contributing

In order to contribute to `googlePubsubR` you'll need to go through the following steps:

1. Set up a GCP project and create a service account with Pub/Sub admin rights.
2. Download a JSON key for the newly created account. Naming the file `.gcp_creds.json` and placing
it in the package root folder will make it automatically gitignored.
3. Set up the following env vars (either through a tool like `direnv` or a `.Renviron` file).

```
GCP_AUTH_FILE=<paht_to_json_auth_file>
GCP_PROJECT=<gcp_project_id_string>
```
4. Check everything is set up correctly by running a test run via `devtools::test()`.
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ reference:
- title: "Auth"
contents:
- pubsub_auth
- ps_project_get
- ps_project_set
- title: "Utils"
contents:
- msg_encode
Expand Down
5 changes: 3 additions & 2 deletions cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

This is a resubmission. In this version I have:

* Add helpers to encode/decode messages: `msg_encode`, `msg_decode`.
* `pubsub_auth` now prompts the correct package name (fixes #6)
* Add helper methods (`ps_project_set/get`) to switch between GCP projects (#13)
* Quieter warnings when env variables are not set (#13)
* Deleted extra `name` argument from `topics_create` (thanks muschellij2)

## Test environments

Expand Down
3 changes: 3 additions & 0 deletions man/msg_encode.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/ps_project_get.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions man/ps_project_set.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions man/pubsub_auth.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/schemas_create.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/schemas_list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/schemas_validate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/schemas_validate_message.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 70ead40

Please sign in to comment.