-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PR for feature #58 (access private datasets and be able to upsert and full replace datasets) #64
Closed
marks
wants to merge
4
commits into
Chicago:dev
from
marks:58-support-update-and-create-datasets-REDO
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#' Methods required for uploading data to Socrata | ||
|
||
#' Wrap httr GET in some diagnostics | ||
#' | ||
#' In case of failure, report error details from Socrata. | ||
#' | ||
#' @param url - Socrata Open Data Application Program Interface (SODA) endpoint (JSON only for now) | ||
#' @param json_data_to_upload - JSON encoded data to update your SODA endpoint with | ||
#' @param http_verb - PUT or POST depending on update mode | ||
#' @param email - email associated with Socrata account (will need write access to dataset) | ||
#' @param password - password associated with Socrata account (will need write access to dataset) | ||
#' @param app_token - optional app_token associated with Socrata account | ||
#' @return httr a response object | ||
#' @importFrom httr GET | ||
#' | ||
#' @noRd | ||
checkUpdateResponse <- function(json_data_to_upload, url, http_verb, email, password, app_token = NULL) { | ||
if(http_verb == "POST"){ | ||
response <- httr::POST(url, | ||
body = json_data_to_upload, | ||
httr::authenticate(email, password), | ||
httr::add_headers("X-App-Token" = app_token, | ||
"Content-Type" = "application/json")) #, verbose()) | ||
} else if(http_verb == "PUT"){ | ||
response <- httr::PUT(url, | ||
body = json_data_to_upload, | ||
httr::authenticate(email, password), | ||
httr::add_headers("X-App-Token" = app_token, | ||
"Content-Type" = "application/json")) # , verbose()) | ||
} | ||
|
||
# TODO: error handling | ||
# errorHandling(response) | ||
|
||
return(response) | ||
} | ||
|
||
#' Write to a Socrata dataset (full replace or upsert) | ||
#' | ||
#' @description Method for updating Socrata datasets | ||
#' | ||
#' @param dataframe - dataframe to upload to Socrata | ||
#' @param dataset_json_endpoint - Socrata Open Data Application Program Interface (SODA) endpoint (JSON only for now) | ||
#' @param update_mode - "UPSERT" or "REPLACE"; consult http://dev.socrata.com/publishers/getting-started.html | ||
#' @param email - The email to the Socrata account with read access to the dataset | ||
#' @param password - The password associated with the email to the Socrata account | ||
#' @param app_token - a (non-required) string; SODA API token can be used to query the data | ||
#' portal \url{http://dev.socrata.com/consumers/getting-started.html} | ||
#' | ||
#' @author Mark Silverberg \email{mark.silverberg@@socrata.com} | ||
#' | ||
#' @importFrom httr parse_url build_url | ||
#' @importFrom plyr rbind.fill | ||
#' | ||
#' @export | ||
write.socrata <- function(dataframe, dataset_json_endpoint, update_mode, email, password, app_token = NULL) { | ||
|
||
# translate update_mode to http_verbs | ||
if(update_mode == "UPSERT"){ | ||
http_verb <- "POST" | ||
} else if(update_mode == "REPLACE") { | ||
http_verb <- "PUT" | ||
} else { | ||
stop("update_mode must be UPSERT or REPLACE") | ||
} | ||
|
||
# convert dataframe to JSON | ||
dataframe_as_json_string <- jsonlite::toJSON(dataframe) | ||
|
||
# do the actual upload | ||
response <- checkUpdateResponse(dataframe_as_json_string, dataset_json_endpoint, http_verb, email, password, app_token) | ||
|
||
return(response) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
\title{Get a full Socrata data set as an R data frame} | ||
\usage{ | ||
read.socrata(url = NULL, app_token = NULL, limit = 50000, domain = NULL, | ||
fourByFour = NULL, query = NULL) | ||
fourByFour = NULL, query = NULL, email = NULL, password = NULL) | ||
} | ||
\arguments{ | ||
\item{url}{- A Socrata resource URL, or a Socrata "human-friendly" URL, | ||
|
@@ -26,15 +26,21 @@ portal \url{http://dev.socrata.com/consumers/getting-started.html}} | |
|
||
\item{query}{- Based on query language called the "Socrata Query Language" ("SoQL"), see | ||
\url{http://dev.socrata.com/docs/queries.html}.} | ||
|
||
\item{email}{- (optional) The email to the Socrata account with read access to the dataset} | ||
|
||
\item{password}{- (optional) The password associated with the email to the Socrata account} | ||
} | ||
\description{ | ||
Manages throttling and POSIX date-time conversions. We support only .json suffix. | ||
} | ||
\examples{ | ||
\dontrun{ | ||
df_1 <- read.socrata(url = "http://soda.demo.socrata.com/resource/4334-bgaj.csv") | ||
df_2 <- read.socrata(domain = "http://data.cityofchicago.org/", fourByFour = "ydr8-5enu") | ||
df_3 <- read.socrata(url = "http://data.cityofchicago.org/resource/ydr8-5enu.json") | ||
} | ||
} | ||
\author{ | ||
Hugh J. Devlin, Ph. D. \email{[email protected]} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
% Generated by roxygen2 (4.1.1): do not edit by hand | ||
% Please edit documentation in R/writeData.R | ||
\name{write.socrata} | ||
\alias{write.socrata} | ||
\title{Write to a Socrata dataset (full replace or upsert)} | ||
\usage{ | ||
write.socrata(dataframe, dataset_json_endpoint, update_mode, email, password, | ||
app_token = NULL) | ||
} | ||
\arguments{ | ||
\item{dataframe}{- dataframe to upload to Socrata} | ||
|
||
\item{dataset_json_endpoint}{- Socrata Open Data Application Program Interface (SODA) endpoint (JSON only for now)} | ||
|
||
\item{update_mode}{- "UPSERT" or "REPLACE"; consult http://dev.socrata.com/publishers/getting-started.html} | ||
|
||
\item{email}{- The email to the Socrata account with read access to the dataset} | ||
|
||
\item{password}{- The password associated with the email to the Socrata account} | ||
|
||
\item{app_token}{- a (non-required) string; SODA API token can be used to query the data | ||
portal \url{http://dev.socrata.com/consumers/getting-started.html}} | ||
} | ||
\description{ | ||
Method for updating Socrata datasets | ||
} | ||
\author{ | ||
Mark Silverberg \email{[email protected]} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
context("Test reading private Socrata dataset with email and password") | ||
|
||
privateResourceToReadCsvUrl <- "https://soda.demo.socrata.com/resource/a9g2-feh2.csv" | ||
privateResourceToReadJsonUrl <- "https://soda.demo.socrata.com/resource/a9g2-feh2.json" | ||
socrataEmail <- Sys.getenv("SOCRATA_EMAIL", "") | ||
socrataPassword <- Sys.getenv("SOCRATA_PASSWORD", "") | ||
|
||
test_that("read Socrata CSV that requires a login", { | ||
# should error when no email and password are sent with the request | ||
expect_error(read.socrata(url = privateResourceToReadCsvUrl)) | ||
# try again, this time with email and password in the request | ||
df <- read.socrata(url = privateResourceToReadCsvUrl, email = socrataEmail, password = socrataPassword) | ||
# tests | ||
expect_equal(2, ncol(df), label="columns") | ||
expect_equal(3, nrow(df), label="rows") | ||
}) | ||
|
||
test_that("read Socrata JSON that requires a login", { | ||
# should error when no email and password are sent with the request | ||
expect_error(read.socrata(url = privateResourceToReadJsonUrl)) | ||
# try again, this time with email and password in the request | ||
df <- read.socrata(url = privateResourceToReadJsonUrl, email = socrataEmail, password = socrataPassword) | ||
# tests | ||
expect_equal(2, ncol(df), label="columns") | ||
expect_equal(3, nrow(df), label="rows") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
context("write Socrata datasets") | ||
|
||
socrataEmail <- Sys.getenv("SOCRATA_EMAIL", "") | ||
socrataPassword <- Sys.getenv("SOCRATA_PASSWORD", "") | ||
|
||
test_that("add a row to a dataset", { | ||
datasetToAddToUrl <- "https://soda.demo.socrata.com/resource/xh6g-yugi.json" | ||
|
||
# populate df_in with two columns, each with a random number | ||
x <- sample(-1000:1000, 1) | ||
y <- sample(-1000:1000, 1) | ||
df_in <- data.frame(x,y) | ||
|
||
# write to dataset | ||
write.socrata(df_in,datasetToAddToUrl,"UPSERT",socrataEmail,socrataPassword) | ||
|
||
# read from dataset and store last (most recent) row for comparisons / tests | ||
df_out <- read.socrata(url = datasetToAddToUrl, email = socrataEmail, password = socrataPassword) | ||
df_out_last_row <- tail(df_out, n=1) | ||
|
||
expect_equal(df_in$x, as.numeric(df_out_last_row$x), label = "x value") | ||
expect_equal(df_in$y, as.numeric(df_out_last_row$y), label = "y value") | ||
}) | ||
|
||
|
||
test_that("fully replace a dataset", { | ||
datasetToReplaceUrl <- "https://soda.demo.socrata.com/resource/kc76-ybeq.json" | ||
|
||
# populate df_in with two columns of random numbers | ||
x <- sample(-1000:1000, 5) | ||
y <- sample(-1000:1000, 5) | ||
df_in <- data.frame(x,y) | ||
|
||
# write to dataset | ||
write.socrata(df_in,datasetToReplaceUrl,"REPLACE",socrataEmail,socrataPassword) | ||
|
||
# read from dataset for comparisons / tests | ||
df_out <- read.socrata(url = datasetToReplaceUrl, email = socrataEmail, password = socrataPassword) | ||
|
||
expect_equal(ncol(df_in), ncol(df_out), label="columns") | ||
expect_equal(nrow(df_in), nrow(df_out), label="rows") | ||
expect_equal(df_in$x, as.numeric(df_out$x), label = "x values") | ||
expect_equal(df_in$y, as.numeric(df_out$y), label = "y values") | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
title is missing