Skip to content
This repository was archived by the owner on May 10, 2023. It is now read-only.

Commit 84ba464

Browse files
author
ironholds
committed
start
0 parents  commit 84ba464

21 files changed

+554
-0
lines changed

.Rbuildignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$
3+
^\.httr-oauth$

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.httr-oauth

DESCRIPTION

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Package: driver
2+
Type: Package
3+
Title: What the package does (short line)
4+
Version: 1.0
5+
Date: 2015-02-13
6+
Author: Who wrote it
7+
Maintainer: Who to complain to <yourfault@somewhere.net>
8+
Description: More about what it does (maybe more than one line)
9+
License: What license is it under?

NAMESPACE

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by roxygen2 (4.1.0): do not edit by hand
2+
3+
export(copy_file)
4+
export(create_file)
5+
export(delete_file)
6+
export(driver_connect)
7+
export(file_metadata)
8+
export(list_files)
9+
importFrom(httr,DELETE)
10+
importFrom(httr,GET)
11+
importFrom(httr,POST)
12+
importFrom(httr,config)
13+
importFrom(httr,content)
14+
importFrom(httr,oauth2.0_token)
15+
importFrom(httr,oauth_app)
16+
importFrom(httr,oauth_endpoints)
17+
importFrom(httr,stop_for_status)
18+
importFrom(httr,upload_file)

R/base_query_functions.R

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
driver_get <- function(url, ...){
2+
result <- GET(url, user_agent("driver - https://github.com/Ironholds/driver"))
3+
stop_for_status(result)
4+
return(result)
5+
}
6+
7+
driver_post <- function(url, ...){
8+
result <- POST(url, user_agent("driver - https://github.com/Ironholds/driver"))
9+
stop_for_status(result)
10+
return(result)
11+
}

R/driver.R

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#' @title API client library for Google Drive
2+
#' @name driver
3+
#' @docType package
4+
#' @aliases driver driver-package
5+
#' @importFrom httr config content stop_for_status
6+
NULL

R/files.R

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#'@title Retrieve the metadata for a specific file.
2+
#'
3+
#'@description \code{file_metadata} retrieves the metadata for a specific file the user
4+
#'has access to. To retrieve the metadata for all files, see \code{\link{list_files}}
5+
#'
6+
#'@param token a token, generated with \code{\link{driver_connect}}.
7+
#'
8+
#'@param file_id a file ID, as a string. This can be retrieved from the URL bar when you're accessing
9+
#'the file: for example, "https://docs.google.com/document/d/1gOxog56F2bCnxwum7VhmN3JqTX7usTYcK5X3V4QDnxg"
10+
#'has the file_id "1gOxog56F2bCnxwum7VhmN3JqTX7usTYcK5X3V4QDnxg".
11+
#'
12+
#'@param simplify whether or not to perform some (small) simplification of the returned
13+
#'list, to make it less nested, headachey and impossible to read. Set to FALSE by default.
14+
#'
15+
#'@param ... further arguments to pass to httr's GET.
16+
#'
17+
#'@seealso \code{\link{list_files}}, for requesting the metadata associated with many files.
18+
#'
19+
#'@examples
20+
#'\dontrun{
21+
#'#Once we've authenticated and grabbed a token, we can grab the metadata for the example file:
22+
#'example_metadata <- file_metadata(token, "1gOxog56F2bCnxwum7VhmN3JqTX7usTYcK5X3V4QDnxg")
23+
#'}
24+
#'@export
25+
file_metadata <- function(token, file_id, simplify = FALSE, ...){
26+
parameters <- paste0("files/", file_id)
27+
result <- driver_get(parameters, "file_metadata", token, ...)
28+
if(simplify){
29+
result <- simplify_response(result)
30+
}
31+
return(result)
32+
}
33+
34+
#'@title create an empty file, or upload a local file
35+
#'
36+
#'@importFrom httr upload_file
37+
#'@export
38+
create_file <- function(token, file_path = NULL, ...){
39+
parameters <- "files?uploadType=media"
40+
if(is.null(file_path)){
41+
result <- driver_post(parameters, token, ...)
42+
} else {
43+
result <- driver_post(parameters, token, body = upload_file(file_path), ...)
44+
}
45+
return(result)
46+
}
47+
48+
update_file <- function(){
49+
50+
}
51+
52+
#'@title copy a Google Drive file
53+
#'@description takes a Google Drive file and creates a copy of it, with the same
54+
#'access restrictions.
55+
#'
56+
#'@param token a token, generated with \code{\link{driver_connect}}.
57+
#'
58+
#'@param file_id the ID of the file; see \code{\link{file_metadata}} for further
59+
#'commentary.
60+
#'
61+
#'@param ... further arguments to pass to httr's POST.
62+
#'
63+
#'@return a set of metadata associated with the copy of the file, matching
64+
#'the output of \code{\link{file_metadata}}.
65+
#'
66+
#'@export
67+
copy_file <- function(token, file_id, ...){
68+
parameters <- paste0("files/", file_id, "/copy")
69+
result <- driver_post(parameters, token, ...)
70+
return(result)
71+
}
72+
73+
#'@title delete a Google Drive file
74+
#'@description \code{delete_file} removes a file completely, assuming the user has
75+
#'permission to do so. In the process it completely bypasses the trash bin, rendering
76+
#'the file unrecoverable by the user.
77+
#'
78+
#'@param token a token, generated with \code{\link{driver_connect}}.
79+
#'
80+
#'@param file_id the ID of the file; see \code{\link{file_metadata}} for further
81+
#'commentary.
82+
#'
83+
#'@param ... further arguments to pass to httr's DELETE.
84+
#'
85+
#'@return TRUE if the file was successfully deleted, FALSE or an error otherwise.
86+
#'
87+
#'@export
88+
delete_file <- function(token, file_id, ...){
89+
parameters <- paste0("files/", file_id)
90+
result <- driver_delete(parameters, token)
91+
if(result$status_code %in% c(200, 202, 204)){
92+
return(TRUE)
93+
} else {
94+
return(FALSE)
95+
}
96+
}
97+
98+
#'@title Retrieve the metadata for all files
99+
#'
100+
#'@description \code{list_files} allows an authenticated user to retrieve the metadata
101+
#'associated with each file they have access to. For the metadata for a single file, see
102+
#'\code{\link{get_file_metadata}}.
103+
#'
104+
#'@param token a token, generated with \code{\link{driver_connect}}.
105+
#'
106+
#'@param max_results the maximum number of results to return; any number between 1 and 1000.
107+
#'Set to 100 by default.
108+
#'
109+
#'@param page_token in the event that the requested files are split over multiple pages,
110+
#'each object returned from \code{list_files} will contain an element named "nextPageToken".
111+
#'Plugging this into the \code{page_token} parameter provides for query continuation.
112+
#'
113+
#'@param simplify whether or not to perform some (small) simplification of the returned
114+
#'list, to make it less nested, headachey and impossible to read. Set to FALSE by default.
115+
#'
116+
#'@param ... further arguments to pass to httr's GET.
117+
#'
118+
#'@export
119+
list_files <- function(token, max_results = 100, page_token = NULL, simplify = FALSE, ...){
120+
parameters <- paste0("files?", "maxResults=", max_results)
121+
if(!is.null(page_token)){
122+
parameters <- paste0(parameters, "&pageToken=", page_token)
123+
}
124+
result <- driver_get(parameters, "file_list", token, ...)
125+
if(simplify){
126+
result <- simplify_response(result)
127+
}
128+
return(result)
129+
}
130+
131+
update_file_time <- function(){
132+
133+
}
134+
135+
trash_file <- function(){
136+
137+
}
138+
139+
untrash_file <- function(){
140+
141+
}
142+
143+
empty_trash <- function(){
144+
145+
}
146+
watch_file <- function(){
147+
148+
}
149+

R/http_wrappers.R

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#'@title send a GET request to the Google Drive API
2+
#'
3+
#'@param parameters a constructed query string passed from one of the exported functions.
4+
#'
5+
#'@param out_class a classname to set, used to distinguish between simplifier methods
6+
#'
7+
#'@param token a token retrieved with \code{\link{driver_connect}}
8+
#'
9+
#'@param ... further arguments to httr's GET function.
10+
#'
11+
#'@importFrom httr GET
12+
driver_get <- function(parameters, out_class, token, ...){
13+
result <- GET(create_url(parameters), config(token = token, useragent = "driver - https://github.com/Ironholds/driver"))
14+
stop_for_status(result)
15+
result <- content(result)
16+
class(result) <- out_class
17+
return(result)
18+
}
19+
20+
#'@title send a DELETE request to the Google Drive API
21+
#'
22+
#'@param parameters a constructed query string passed from one of the exported functions.
23+
#'
24+
#'@param token a token retrieved with \code{\link{driver_connect}}
25+
#'
26+
#'@param ... further arguments to httr's DELETE function.
27+
#'
28+
#'@importFrom httr DELETE
29+
driver_delete <- function(parameters, token, ...){
30+
result <- DELETE(create_url(parameters), config(token = token, useragent = "driver - https://github.com/Ironholds/driver"))
31+
stop_for_status(result)
32+
return(result)
33+
}
34+
35+
#'@title send a POST request to the Google Drive API
36+
#'
37+
#'@param parameters a constructed query string passed from one of the exported functions.
38+
#'
39+
#'@param token a token retrieved with \code{\link{driver_connect}}
40+
#'
41+
#'@param ... further arguments to httr's DELETE function.
42+
#'
43+
#'@importFrom httr POST
44+
driver_post <- function(parameters, token, ...){
45+
result <- POST(create_url(parameters), config(token = token, useragent = "driver - https://github.com/Ironholds/driver"), ...)
46+
stop_for_status(result)
47+
result <- content(result)
48+
return(result)
49+
}

R/simplifiers.R

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
simplify_response <- function(x){
2+
UseMethod("simplify_response", x)
3+
}
4+
5+
file_simp <- function(x){
6+
x$labels <- unlist(x$labels)
7+
x$exportLinks <- unlist(x$exportLinks)
8+
x$userPermission <- unlist(x$userPermission)
9+
x$ownerNames <- unlist(x$ownerNames)
10+
return(x)
11+
}
12+
13+
simplify_response.file_list <- function(x){
14+
x$items <- lapply(x$items, file_simp)
15+
return(x)
16+
}
17+
18+
simplify_response.file_metadata <- function(x){
19+
return(file_simp(x))
20+
}

R/utilities.R

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
create_url <- function(parameters){
2+
return(paste0("https://www.googleapis.com/drive/v2/",parameters))
3+
}
4+
5+
#'@title connect to Google Drive and authorise driver to access your files.
6+
#'@description \code{driver_connect} produces an OAuth 2.0 token which can be passed
7+
#'into other functions in the driver package, enabling driver to access your Google Drive
8+
#'files on your behalf.
9+
#'
10+
#'A necessary prerequisite is creating a client app:
11+
#'
12+
#' - Go to \url{https://console.developers.google.com}
13+
#' - Create a client app, with http://localhost:1410/ as the redirect URI
14+
#' - Connect it to the Drive API.
15+
#' - note the provided clientID and secret.
16+
#'
17+
#'@param id the clientID of the client app you've created (see above).
18+
#'
19+
#'@param the secret of the client app you've created (see above).
20+
#'
21+
#'@return an OAuth token that can be passed into other function calls.
22+
#'
23+
#'@examples
24+
#'\dontrun{
25+
#'#Use driver_connect to generate a token, pass that token in to list_files
26+
#'token <- driver_connect("foo", "bar")
27+
#'}
28+
#'
29+
#'@importFrom httr oauth_app oauth_endpoints oauth2.0_token
30+
#'@export
31+
driver_connect <- function(id, secret){
32+
app <- oauth_app("driver", id, secret)
33+
token <- oauth2.0_token(oauth_endpoints("google"), app = app,
34+
scope = "https://www.googleapis.com/auth/drive", cache = FALSE)
35+
return(token)
36+
}

driver.Rproj

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
BuildType: Package
16+
PackageUseDevtools: Yes
17+
PackageInstallArgs: --no-multiarch --with-keep.source
18+
PackageRoxygenize: rd,collate,namespace

man/copy_file.Rd

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
% Generated by roxygen2 (4.1.0): do not edit by hand
2+
% Please edit documentation in R/files.R
3+
\name{copy_file}
4+
\alias{copy_file}
5+
\title{copy a Google Drive file}
6+
\usage{
7+
copy_file(token, file_id, ...)
8+
}
9+
\arguments{
10+
\item{token}{a token, generated with \code{\link{driver_connect}}.}
11+
12+
\item{file_id}{the ID of the file; see \code{\link{file_metadata}} for further
13+
commentary.}
14+
15+
\item{...}{further arguments to pass to httr's POST.}
16+
}
17+
\value{
18+
a set of metadata associated with the copy of the file, matching
19+
the output of \code{\link{file_metadata}}.
20+
}
21+
\description{
22+
takes a Google Drive file and creates a copy of it, with the same
23+
access restrictions.
24+
}
25+

man/create_file.Rd

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
% Generated by roxygen2 (4.1.0): do not edit by hand
2+
% Please edit documentation in R/files.R
3+
\name{create_file}
4+
\alias{create_file}
5+
\title{create an empty file, or upload a local file}
6+
\usage{
7+
create_file(token, file_path = NULL, ...)
8+
}
9+
\description{
10+
create an empty file, or upload a local file
11+
}
12+

0 commit comments

Comments
 (0)