|
| 1 | + |
| 2 | +#' Generate a DOI for a Bioconductor package |
| 3 | +#' |
| 4 | +#' This function makes calls out to the REST API (v2) described |
| 5 | +#' here: \url{https://support.datacite.org/docs/api-create-dois}. This |
| 6 | +#' function creates a new DOI for a Bioconductor package (cannot already |
| 7 | +#' exist). The target URL for the DOI is the short Bioconductor |
| 8 | +#' package URL. |
| 9 | +#' |
| 10 | +#' The login information for the "real" Bioconductor account |
| 11 | +#' should be stored in the environment variables "EZID_USERNAME" |
| 12 | +#' and "EZID_PASSWORD". |
| 13 | +#' |
| 14 | +#' The GUI is available here: \url{https://doi.datacite.org/}. |
| 15 | +#' |
| 16 | +#' @param pkg character(1) package name |
| 17 | +#' @param authors character vector of authors (will be "pasted" together) |
| 18 | +#' @param pubyear integer(1) publication year |
| 19 | +#' @param testing logical(1) If true, will use the apitest |
| 20 | +#' user with the password apitest. These DOIs will expire. |
| 21 | +#' The same apitest:apitest combination can be used to |
| 22 | +#' login to the EZID website for doing things using the |
| 23 | +#' web interface. If false, the Bioconductor-specific |
| 24 | +#' user credentials should be in the correct environment |
| 25 | +#' variables |
| 26 | +#' |
| 27 | +#' @return The DOI as a character(1) vector. |
| 28 | +#' |
| 29 | +#' @importFrom httr POST status_code PUT authenticate timeout content_type accept content |
| 30 | +#' |
| 31 | +#' @keywords Internal |
| 32 | +#' |
| 33 | +#' @examples |
| 34 | +#' \dontrun{ |
| 35 | +#' x = generateBiocPkgDOI('RANDOM_TEST_PACKAGE','Sean Davis',1972) |
| 36 | +#' } |
| 37 | +# |
| 38 | +# draft concept |
| 39 | +# |
| 40 | +# { |
| 41 | +# "data": { |
| 42 | +# "type": "dois", |
| 43 | +# "attributes": { |
| 44 | +# "doi": "10.18129/B9" |
| 45 | +# } |
| 46 | +# } |
| 47 | +# } |
| 48 | +# BIOC prefix: 10.18129/B9 |
| 49 | +# $ curl -X POST -H "Content-Type: application/vnd.api+json" --user YOUR_REPOSITORY_ID:YOUR_PASSWORD -d @my_draft_doi.json https://api.test.datacite.org/dois |
| 50 | +# |
| 51 | +generateBiocPkgDOI_REST = function(pkg, authors, pubyear, testing=TRUE) { |
| 52 | + if(testing) { |
| 53 | + # View results at: https://doi.test.datacite.org |
| 54 | + # The testing piece does not work with new API? |
| 55 | + username='TESTING_USERNAME' |
| 56 | + password='TESTING_PASSWORD' |
| 57 | + bioc_shoulder='doi:10.5072/FK2' |
| 58 | + base_url = 'https://ez.test.datacite.org/id' |
| 59 | + } else { |
| 60 | + username=Sys.getenv('EZID_USERNAME') |
| 61 | + password=Sys.getenv('EZID_PASSWORD') |
| 62 | + bioc_shoulder='doi:10.18129/B9' |
| 63 | + base_url = "https://ez.datacite.org/id" |
| 64 | + } |
| 65 | + bioc_doi_namespace = ".bioc." |
| 66 | + pkg_doi = paste0(bioc_shoulder,bioc_doi_namespace,pkg) |
| 67 | + url0 = file.path(base_url,pkg_doi) |
| 68 | + body = paste(c(sprintf("datacite.title: %s",pkg), |
| 69 | + sprintf("_target: https://bioconductor.org/packages/%s",pkg), |
| 70 | + sprintf("datacite.creator: %s",gsub('\n','',paste(authors,collapse=", "))), |
| 71 | + "datacite.publisher: Bioconductor", |
| 72 | + sprintf("datacite.publicationyear: %d",pubyear), |
| 73 | + sprintf("datacite.resourcetype: %s","Software")),collapse="\n") |
| 74 | + res = httr::POST(url0, |
| 75 | + content_type('text/plain'), |
| 76 | + accept("text/plain"), |
| 77 | + httr::authenticate(username,password), |
| 78 | + body=body,timeout(30)) |
| 79 | + if(status_code(res)>=400) { |
| 80 | + res = httr::PUT(url0, |
| 81 | + content_type('text/plain'), |
| 82 | + accept("text/plain"), |
| 83 | + httr::authenticate(username,password), |
| 84 | + body=body,timeout(30)) |
| 85 | + } |
| 86 | + message(res) |
| 87 | + return(res) |
| 88 | + tmp = strsplit(content(res),' ')[[1]][c(2,4)] |
| 89 | + return(tmp[1]) |
| 90 | +} |
0 commit comments