Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: zhapir
Title: Wrapper around the API of the Datacatalogue of the cantonal administration
Version: 1.0.0
Version: 1.0.1
Authors@R: c(
person("Philipp", "Bosch", , "philipp.bosch@statistik.ji.zh.ch", role = c("aut", "cre")),
person("Tabea", "Eggler", , "tabea.eggler@statistik.ji.zh.ch", role = "aut"),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export(create_file)
export(dataset_is_valid_for_status)
export(get_api_key)
export(get_dataset)
export(get_formats)
export(get_keywords)
export(get_licenses)
export(get_organisations)
export(get_periodicities)
export(get_statuses)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# zhapir 1.0.1

## Minor improvements and bug fixes

* Exporting all `get`-functions
* All exported functions have an explicit `api_key` & `use_dev` argument.

# zhapir 1.0.0

* Added a `NEWS.md` file to track changes to the package.
22 changes: 17 additions & 5 deletions R/create_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
#' @param relation_ids Optional integer vector
#' @param theme_ids Optional character vector
#' @param periodicity_id Optional character
#' `1` = "Jährlich",
#' `21` = "Kontinuierlich",
#' `42` = "Zweijährlich",
#' `43` = "Halbjährlich",
#' `45` = "Vierteljährlich",
#' `47` = "Monatlich",
#' `49` = "Alle zwei Wochen",
#' `51` = "Wöchentlich",
#' `52` = "Halbwöchentlich",
#' `54` = "Täglich",
#' `55` = "Unregelmässig",
#' `56` = "Niemals"
#' @param see_also_ids Optional integer vector
#' @param api_key API key (optional; falls back to env var)
#' @param use_dev Logical; use development base URL
Expand Down Expand Up @@ -66,11 +78,11 @@ create_dataset <- function(


# resolve all lookups up front
keyword_ids_api <- if (is.character(keyword_ids)) convert_keywords_to_id(keyword_ids) else keyword_ids
zh_web_catalog_ids_api <- if (is.character(zh_web_datacatalog_ids)) convert_zh_web_catalog_to_id(zh_web_datacatalog_ids) else zh_web_datacatalog_ids
theme_ids_api <- if (is.character(theme_ids)) convert_themes_to_id(theme_ids) else theme_ids
periodicity_id_api <- if (is.character(periodicity_id)) convert_periodicities_to_id(periodicity_id) else periodicity_id
see_also_ids_api <- if (is.character(see_also_ids)) convert_datasets_to_id(see_also_ids) else see_also_ids
keyword_ids_api <- if (is.character(keyword_ids)) convert_keywords_to_id(keyword_ids, use_dev = use_dev, api_key = api_key) else keyword_ids
zh_web_catalog_ids_api <- if (is.character(zh_web_datacatalog_ids)) convert_zh_web_catalog_to_id(zh_web_datacatalog_ids, use_dev = use_dev, api_key = api_key) else zh_web_datacatalog_ids
theme_ids_api <- if (is.character(theme_ids)) convert_themes_to_id(theme_ids, use_dev = use_dev, api_key = api_key) else theme_ids
periodicity_id_api <- if (is.character(periodicity_id)) convert_periodicities_to_id(periodicity_id, use_dev = use_dev, api_key = api_key) else periodicity_id
see_also_ids_api <- if (is.character(see_also_ids)) convert_datasets_to_id(see_also_ids, use_dev = use_dev, api_key = api_key) else see_also_ids



Expand Down
39 changes: 35 additions & 4 deletions R/create_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,41 @@
#' @param access_url Optional URL to access the distribution (must start with http:// or https://).
#' @param byte_size Optional file size in bytes (must be a positive number).
#' @param status_id Optional character; status ID (will be set via follow-up PATCH).
#' `0` = "verworfen,
#' `1` = "✍️ Entwurf ",
#' `2` = "🔍 in Prüfung",
#' `3` = "📜 publiziert",
#' @param license_id integer; license ID.
#' Use `1` = "CC BY 4.0 (Attribution required)" or `2` = "CC0 (No attribution required)".
#' @param file_format_id Optional file format ID.
#' Häufige Formate:
#' `3` = "CSV"
#' `20` = "PDF"
#' `33` = "XLSX"
#' `12` = "JSON"
#' `29` = "TXT"
#' `34` = "XML"
#' `32` = "XLS"
#' `35` = "ZIP"
#' `8` = "HTML"
#' `13` = "JSONLD"
#' `5` = "DOCX"
#' `40` = "GPKG"
#' `11` = "JS"
#' `25` = "RTF"
#' @param periodicity_id Optional character periodicity ID.
#' `1` = "Jährlich",
#' `21` = "Kontinuierlich",
#' `42` = "Zweijährlich",
#' `43` = "Halbjährlich",
#' `45` = "Vierteljährlich",
#' `47` = "Monatlich",
#' `49` = "Alle zwei Wochen",
#' `51` = "Wöchentlich",
#' `52` = "Halbwöchentlich",
#' `54` = "Täglich",
#' `55` = "Unregelmässig",
#' `56` = "Niemals"
#' @param file_path Optional local file path; if provided, the file will be uploaded and linked.
#' @param start_date POSIXct or ISO datetime string; new start (optional)
#' @param end_date POSIXct or ISO datetime string; new end (optional)
Expand Down Expand Up @@ -91,10 +122,10 @@ create_distribution <- function(
}

# Resolve all lookups up front
status_id_api <- if (is.character(status_id)) convert_statuses_to_id(status_id) else status_id
license_id_api <- if (is.character(license_id)) convert_licenses_to_id(license_id) else license_id
file_format_id_api <- if (is.character(file_format_id)) convert_formats_to_id(file_format_id) else file_format_id
periodicity_id_api <- if (is.character(periodicity_id)) convert_periodicities_to_id(periodicity_id) else periodicity_id
status_id_api <- if (is.character(status_id)) convert_statuses_to_id(status_id, use_dev = use_dev, api_key = api_key) else status_id
license_id_api <- if (is.character(license_id)) convert_licenses_to_id(license_id, use_dev = use_dev, api_key = api_key) else license_id
file_format_id_api <- if (is.character(file_format_id)) convert_formats_to_id(file_format_id, use_dev = use_dev, api_key = api_key) else file_format_id
periodicity_id_api <- if (is.character(periodicity_id)) convert_periodicities_to_id(periodicity_id, use_dev = use_dev, api_key = api_key) else periodicity_id

# Build pure Distribution (no HTTP here)
dist <- Distribution(
Expand Down
9 changes: 0 additions & 9 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#' Generic method to create a new object in the data catalog via the API.
#'
#' @param object The object to create (Dataset or Distribution)
#' @param api_key Authentication information from login_to_api()
#' @param use_dev Whether to use the development environment
#'
#' @return The created object with updated information from the API response
#' @keywords internal
Expand All @@ -19,9 +17,6 @@ create <- S7::new_generic("create", "object")
#' Generic method to update an existing object in the data catalog via the API.
#'
#' @param object The object to update
#' @param id ID of the object to update
#' @param api_key Authentication information from login_to_api()
#' @param use_dev Whether to use the development environment
#'
#' @return The updated object with updated information from the API response
#' @keywords internal
Expand All @@ -31,10 +26,6 @@ update <- S7::new_generic("update", "object")
#' Set the status of a Dataset or Distribution via the "/set-status" endpoint
#'
#' @param object A `Dataset` or `Distribution` S7 object with `@id` and `@status_id` set
#' @param api_key API key string for authentication (optional)
#' @param use_dev Logical; use the development API endpoint (default `TRUE`)
#' @param verbosity Integer; httr2 verbosity level (default `0`)
#'
#' @return Invisibly returns parsed response as a list.
#' @keywords internal
set_status <- S7::new_generic("set_status", "object")
16 changes: 10 additions & 6 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ get_base_url <- function(use_dev = FALSE) {
#'
#' Attempts to fetch the API key from (in order):
#' 1. Provided argument
#' 2. Environment variable `MDV_API_KEY`
#' 2. Environment variable `ZHAPIR_API_KEY`
#' 3. Interactive prompt (RStudio or askpass)
#'
#' @param key Optional plain API key.
#' @return API key string.
#' @export
get_api_key <- function(key = NULL) {
# 1. Direct argument

# 1. Direct argument wins
if (!is.null(key) && nzchar(key)) {
return(key)
}
Expand All @@ -38,21 +39,24 @@ get_api_key <- function(key = NULL) {
return(env_key)
}

# 3. Interactive prompt (last resort)
prompt_key <- "" # create empty
# 3. Interactive prompt (last resort, and only if really interactive)
if (interactive()) {
prompt_key <- askpass::askpass("Please enter your ZHAPIR_API_KEY key")

# askpass() kann NULL liefern (Abbruch) → robust abfangen
if (!is.null(prompt_key) && nzchar(prompt_key)) {
return(prompt_key)
}
}
if (nzchar(prompt_key)) return(prompt_key)

# 4. Nichts gefunden → Fehler
stop(
"No API key found. Supply via argument or set ZHAPIR_API_KEY environment variable.",
call. = FALSE
)
}



#' Convert an S7 object into a JSON-ready payload list
#'
#' This helper extracts all properties, formats dates, and removes empty
Expand Down
Loading