Skip to content

Commit

Permalink
Merge pull request #40 from jread-usgs/master
Browse files Browse the repository at this point in the history
update to file replace/remove
  • Loading branch information
Luke Winslow committed Mar 11, 2015
2 parents 174816c + ef86da9 commit 613ff65
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 25 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ before_install:
- ./travis-tool.sh bootstrap
install:
- ./travis-tool.sh install_deps
script: ./travis-tool.sh run_tests

after_failure:
- ./travis-tool.sh dump_logs
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: sbtools
Title: USGS SB tools
Maintainer: Luke Winslow <lwinslow@usgs.gov>
Version: 0.7.1
Version: 0.8.1
Author: Luke Winslow
Description: Tools for interacting with SB interfaces.
Imports:
Expand Down
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

export(authenticate_sb)
export(folder_create)
export(item_append_file)
export(item_append_files)
export(item_create)
export(item_exists)
export(item_file_download)
export(item_get)
export(item_get_parent)
export(item_list_children)
export(item_list_files)
export(item_replace_files)
export(item_rm)
export(item_rm_files)
export(item_update)
export(item_update_identifier)
export(item_upload_create)
Expand Down
12 changes: 12 additions & 0 deletions R/file_helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
multi_file_body <- function(files){
body = list()
for(i in 1:length(files)){
if(!file.exists(files[i])){
stop('This file does not exist or cannot be accessed: ', files[i])
}

body[[paste0('file', i)]] = upload_file(files[i])
}
names(body) = rep('file', length(body))
return(body)
}
13 changes: 7 additions & 6 deletions R/item_append_file.R → R/item_append_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'@description Adds a file to an item
#'
#'@param id A ScienceBase item ID to upload to.
#'@param filename A file path to upload.
#'@param files A file path to upload.
#'@param sb_session A ScienceBase session object from authenticate_sb.
#'
#'@return ScienceBase Item.
Expand All @@ -11,20 +11,21 @@
#'
#'@examples \dontrun{
#' sb_session<-authenticate_sb(sbusername)
#' item_append_file("54e265a4e4b08de9379b4dfb", '/foo/bar/baz.zip', sb)
#' item_append_files("54e265a4e4b08de9379b4dfb", '/foo/bar/baz.zip', sb)
#' }
#'@export
item_append_file = function(id, filename, session){
item_append_files = function(id, files, session){

if(!session_validate(session)){
stop('Session state is invalid, please re-authenticate')
}

body <- multi_file_body(files)
url<-paste0(pkg.env$url_upload,'?id=', id)

POST(url, accept_json(),
body=list(file=upload_file(filename)), handle=session)
r = POST(url, accept_json(),
body=body, handle=session)

return(item_get(id,session))
return(content(r))

}
8 changes: 8 additions & 0 deletions R/item_remove_files.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#'@title Remove all files associated with an item
#'@param item_id a sciencebase item identifier
#'@param session Authenticated session object (from \link{authenticate_sb})
#'@description Removes existing files associated with an item.
#'@export
item_rm_files <- function(item_id, session){
item_update(item_id,list('files'=vector()), session = session)
}
13 changes: 13 additions & 0 deletions R/item_replace_files.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#'@title Replace files associated with an item
#'@param item_id a sciencebase item identifier
#'@param files a character vector of file paths
#'@param session Authenticated session object (from \link{authenticate_sb})
#'@description replaces existing files associated with an item with a new one.
#'(Currently does not support multi-file uploads.)
#'This function will not append an existing collection of files. If that
#'is desired, use \code{\link{item_append_file}}
#'@export
item_replace_files <- function(item_id, files, session){
item_rm_files(item_id, session)
item_append_files(id = item_id, filename = files, session)
}
2 changes: 1 addition & 1 deletion R/item_rm.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ item_rm = function(id, session){

r = DELETE(paste0(pkg.env$url_item, id), handle=session, accept_json())

return(url_ok(r))
return(r)

}
10 changes: 1 addition & 9 deletions R/item_upload_create.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ item_upload_create = function(parent_id, files, session){
stop('Session state is invalid, please re-authenticate')
}

body = list()
for(i in 1:length(files)){
if(!file.exists(files[i])){
stop('This file does not exist or cannot be accessed: ', files[i])
}

body[[paste0('file', i)]] = upload_file(files[i])
}
names(body) = rep('file', length(body))
body <- multi_file_body(files)

url = paste0(pkg.env$url_upload_create, parent_id)
r = POST(url, body=body, accept_json(), handle=session, query=list(title='title'))
Expand Down
1 change: 1 addition & 0 deletions R/set_endpoint.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set_endpoint = function(endpoint="production"){
pkg.env$url_item = paste0(pkg.env$url_base, "item/")
pkg.env$url_upload = paste0(pkg.env$url_base, 'file/uploadAndUpsertItem/')
pkg.env$url_upload_create = paste0(pkg.env$url_base, 'file/uploadAndCreateItem/')
pkg.env$url_upload_update = paste0(pkg.env$url_base, 'file/uploadAndUpdateItem/')
pkg.env$url_download = paste0(pkg.env$url_base, 'file/get/')
pkg.env$url_login = 'https://my.usgs.gov/josso/signon/usernamePasswordLogin.do'
}
12 changes: 6 additions & 6 deletions man/item_append_file.Rd → man/item_append_files.Rd
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/item_append_file.R
\name{item_append_file}
\alias{item_append_file}
% Please edit documentation in R/item_append_files.R
\name{item_append_files}
\alias{item_append_files}
\title{Upload File to Item}
\usage{
item_append_file(id, filename, session)
item_append_files(id, files, session)
}
\arguments{
\item{id}{A ScienceBase item ID to upload to.}

\item{filename}{A file path to upload.}
\item{files}{A file path to upload.}

\item{sb_session}{A ScienceBase session object from authenticate_sb.}
}
Expand All @@ -22,7 +22,7 @@ Adds a file to an item
\examples{
\dontrun{
sb_session<-authenticate_sb(sbusername)
item_append_file("54e265a4e4b08de9379b4dfb", '/foo/bar/baz.zip', sb)
item_append_files("54e265a4e4b08de9379b4dfb", '/foo/bar/baz.zip', sb)
}
}

22 changes: 22 additions & 0 deletions man/item_replace_files.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/item_replace_files.R
\name{item_replace_files}
\alias{item_replace_files}
\title{Replace files associated with an item}
\usage{
item_replace_files(item_id, files, session)
}
\arguments{
\item{item_id}{a sciencebase item identifier}

\item{files}{a character vector of file paths}

\item{session}{Authenticated session object (from \link{authenticate_sb})}
}
\description{
replaces existing files associated with an item with a new one.
(Currently does not support multi-file uploads.)
This function will not append an existing collection of files. If that
is desired, use \code{\link{item_append_file}}
}

17 changes: 17 additions & 0 deletions man/item_rm_files.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/item_remove_files.R
\name{item_rm_files}
\alias{item_rm_files}
\title{Remove all files associated with an item}
\usage{
item_rm_files(item_id, session)
}
\arguments{
\item{item_id}{a sciencebase item identifier}

\item{session}{Authenticated session object (from \link{authenticate_sb})}
}
\description{
Removes existing files associated with an item.
}

0 comments on commit 613ff65

Please sign in to comment.