Skip to content

Refactor to an internal "kernel" function? #51

@wibeasley

Description

@wibeasley

@adam3smith wrote in #49 (comment)

A different approach would be to make it easier to construct native API queries from scratch using the client so that we don't have to code everything but it's more readily accessible. I don't know how hard that would be, but when I wrote API functionality (for downloading .zip files) locally it required quite a bit of code duplication from dataverse library.

We encountered something similar in REDCapR. It's a similar package as this one --in the sense that it's basically a glove around curl calls to a server's api. Then it adds to convenience functions and validation to make R development easier because it returns R's native data.frame objects.

When we saw all the duplication in REDCapR, we refactored that core functionality into kernel_api(). That central location makes it easier to improve things consistently like character encoding and error handling. It returns raw result to the calling function, which decides whether to save it as a local file, or return a data.frame.

In dataverse::get_file(), I see the replication @adam3smith's talking about, with three instances similar to

u <- paste0(api_url(server), "access/datafile/", file, "/metadata/", format)
r <- httr::GET(u, httr::add_headers("X-Dataverse-key" = key), ...)
httr::stop_for_status(r)

This could become its own function like

kernel <- function(command, server, key, ...) {
  u <- paste0(api_url(server), command)
  r <- httr::GET(u, httr::add_headers("X-Dataverse-key" = key), ...)
  httr::stop_for_status(r)
  r
}

The code inside dataverse::get_file_metadata() would look like

command <- paste0("access/datafile/", file, "/metadata/", format)
kernel(command, server, key)

The code inside the current dataverse::get_file() would have three different calls:

command <- paste0("access/datafiles/", file)
kernel(command, server, key)
...
command <- paste0("access/datafile/bundle/", file)
kernel(command, server, key)
...
command <- paste0("access/datafile/", file)
kernel(command, server, key)
...

@adam3smith, @kuriwaki, or anyone else, please share your impressions if you'd like. It looks like @skasberger and the pyDataverse do something very similar with their get_request()

Metadata

Metadata

Assignees

Labels

data-downloadFunctions that are about downloading, not uploading, data

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions