Skip to content

Commit

Permalink
version 2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
csgillespie authored and cran-robot committed Mar 23, 2020
1 parent a0b008a commit 67f0f26
Show file tree
Hide file tree
Showing 30 changed files with 454 additions and 115 deletions.
32 changes: 17 additions & 15 deletions DESCRIPTION
@@ -1,25 +1,27 @@
Package: rtypeform
Type: Package
Package: rtypeform
Title: Interface to 'typeform' Results
Version: 2.0.0
Authors@R: person(given = "Colin",
family = "Gillespie",
email = "csgillespie@gmail.com",
role = c("aut", "cre"))
Version: 2.0.1
Authors@R:
person(given = "Colin",
family = "Gillespie",
role = c("aut", "cre"),
email = "csgillespie@gmail.com")
Maintainer: Colin Gillespie <csgillespie@gmail.com>
Description: An R interface to the 'typeform'
<https://typeform.com> application program interface. Also provides
functions for downloading your results.
License: GPL-2 | GPL-3
URL: https://github.com/csgillespie/rtypeform
BugReports: https://github.com/csgillespie/rtypeform/issues
Description: An R interface to the 'typeform' <https://typeform.com> application program interface.
Also provides functions for downloading your results.
License: GPL-2 | GPL-3
LazyData: TRUE
Imports: jsonlite, httr, purrr, utils, tibble, dplyr, glue, lubridate,
tidyr
Imports: assertthat, dplyr, glue, httr, jsonlite, lubridate, purrr,
tibble, tidyr, utils
Suggests: testthat
RoxygenNote: 6.1.0
Encoding: UTF-8
LazyData: TRUE
RoxygenNote: 7.1.0
NeedsCompilation: no
Packaged: 2018-08-28 21:30:54 UTC; ncsg3
Packaged: 2020-03-23 19:43:14 UTC; ncsg3
Author: Colin Gillespie [aut, cre]
Repository: CRAN
Date/Publication: 2018-08-28 21:40:02 UTC
Date/Publication: 2020-03-23 20:00:03 UTC
49 changes: 28 additions & 21 deletions MD5
@@ -1,33 +1,40 @@
81a646d38b2e3b6db553d7f3ffd2e38c *DESCRIPTION
fd0c5ea391a1e3797a2dfe644dc09102 *NAMESPACE
90b88bd7adc9edc9bbdcbe98f3e40177 *NEWS.md
64db7919b8e760fbf63a2bb97c2c75c8 *DESCRIPTION
b47b07927acac276bdc62867fe0602dd *NAMESPACE
64328b76fb13007b6dcd1362f5fd6528 *NEWS.md
635941dbbd7270c4c0c635937c8b6dda *R/auth.R
ec4f83eeeeae26a3c802e16ffb00c2b4 *R/delete_form.R
ec8572ee036f7a2cd93a1ab42ed57517 *R/delete_responses.R
e9cc7ce1c8c9e1203aff715b872f9344 *R/get_api.R
30e2114d899a902b828a133c5b69dddc *R/get_form.R
448c1de06a5f5849cc44f5eaa83e6d3d *R/get_forms.R
bf1204cf35dff4755fdd64d8802343dc *R/delete_responses.R
fed4ca2e94ebd53398ee0b8c57d285ad *R/get_api.R
f4253d36bfbe6232e55c10b4a3bf9f92 *R/get_form.R
2009cfe179335606d1d0d43e6195d4be *R/get_forms.R
f5454863a2f9e48f5d9464646004fabb *R/get_me.R
a82692f1b62a313e8990ca6fe0895321 *R/get_responses.R
d7a6d109f7ae20e34c68a0d48d365864 *R/httr_responses.R
ab4d23e720b02aaa19acf97462413e5f *R/rest_argument_formatting.R
44cf772e70695b3184a75c6d9e8939e3 *R/get_responses.R
12822260c363e197956490e36655dc54 *R/httr_responses.R
a73a3fcb2639f70c62d416b7ef99c52f *R/options.R
20e5fb77416585c28da4a4690932adb5 *R/rest_argument_formatting.R
b87db923050475b3d983984b85c22979 *R/teams.R
8f444fcabda656089a58bae128a6950c *R/themes.R
8e8866720070cc63a9d3d7a50a848b8b *R/workspaces.R
327d3794c779ad639c6c433427d4aaf7 *R/zzz.R
392e294251744f4b708f1f110f50929f *README.md
0cb161b78d9f53744e8d36486b68f081 *man/create_theme.Rd
5ba71666b6518164bf63ffb38f83b03b *R/themes.R
df7ddd5764f9bfb9987d09d46c44f925 *R/workspaces.R
ecba08dfe5c3f3a583dcb46e6a78fa3d *README.md
790fd12460b0df9d3a8849fbe2e36faf *man/check_api_response.Rd
fd0c0105e44f7e8a7fb88eaa2a992bb8 *man/create_theme.Rd
c85b5c659797d93c9500d50f929579cd *man/delete_form.Rd
1170d23090953608163aae72731d6de9 *man/delete_responses.Rd
e8c3146bdfd477782c289737b9998fa7 *man/get_api.Rd
6b381b23da1a19526413e3337c479795 *man/get_forms.Rd
14ddb1358a450a29860fda1590ac8683 *man/get_authorization.Rd
6fcd8f725fa5ee83555ad4005022bdb1 *man/get_forms.Rd
81715703a1b68bcac259da599f447dca *man/get_me.Rd
c5df267cd29636263e3a3dee1a4f48bb *man/get_responses.Rd
83094a8c848d2792d7079b56bbf02711 *man/get_response.Rd
3e0b01eb43f79f7b5e8b2ae465b7c90b *man/get_responses.Rd
68d55f545cedd37ece882b040bfc4dce *man/get_teams.Rd
52362534a13d5fec3bce790fe966f967 *man/get_workspaces.Rd
d01d39f6588c7f4667064023813e009b *man/get_workspaces.Rd
e2da0017130b8f7f6ce41280113aaa05 *man/make_new_token.Rd
239a728954424430702f5123295d376b *man/post_response.Rd
e3143a85be9ee7f4d12635175cc3eb4b *man/rtypeform_set_scope.Rd
fdd4d6b2f737f7cb02bf00b0bb2af68b *tests/testthat.R
e9d07d124d972d20f7d2a6b7d119e206 *tests/testthat/form.rds
1b514e8eac315a08da3094c47a594048 *tests/testthat/test_api_response.R
5c6649ba2648e22ecf58c91910e0f515 *tests/testthat/test_get_forms.R
20a712abc32c32c14e6c4d8853a985bc *tests/testthat/test_get_responses.R
3e1effe7d783ee66b046d128bc5235ba *tests/testthat/test_api_response.R
4898c69b4c1ee54890fb589e51434dda *tests/testthat/test_get_forms.R
d2c1c21687ea2f97d1456eff5d6765fe *tests/testthat/test_get_responses.R
8fd9aab8902e5a44a860c8416e4e9a0b *tests/testthat/test_teams.R
b59ff56421e4b521cf6628584174ca16 *tests/testthat/test_workspaces.R
13 changes: 13 additions & 0 deletions NAMESPACE
@@ -1,27 +1,37 @@
# Generated by roxygen2: do not edit by hand

export(check_api_response)
export(create_theme)
export(create_workspace)
export(delete_form)
export(delete_responses)
export(delete_theme)
export(delete_workspace)
export(get_api)
export(get_authorization)
export(get_forms)
export(get_me)
export(get_number_of_forms)
export(get_number_of_seats)
export(get_number_of_workspace)
export(get_response)
export(get_responses)
export(get_teams)
export(get_theme)
export(get_themes)
export(get_workspace)
export(get_workspaces)
export(make_new_token)
export(post_response)
export(rtypeform_set_client_id)
export(rtypeform_set_client_secret)
export(rtypeform_set_scope)
export(rtypeform_set_token)
export(update_theme)
export(update_workspace)
import(dplyr)
import(purrr)
importFrom(assertthat,assert_that)
importFrom(dplyr,matches)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
Expand All @@ -32,6 +42,9 @@ importFrom(httr,PUT)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(httr,content_type_json)
importFrom(httr,oauth2.0_token)
importFrom(httr,oauth_app)
importFrom(httr,oauth_endpoint)
importFrom(httr,status_code)
importFrom(httr,user_agent)
importFrom(httr,verbose)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
@@ -1,3 +1,8 @@
# Version 2.0.1
* Tidying up code
* Update docs
* Use Oauth

# Version 2.0.0
* Move to the new typeform API - breaking changes
* Jumped to Version 2 to match the V2 API.
Expand Down
31 changes: 31 additions & 0 deletions R/auth.R
@@ -0,0 +1,31 @@
#' Create an OAuth token
#'
#' Create a token object using the client id and secret options set, together
#' with the defined scopes.
#'
#' @importFrom httr oauth_app oauth2.0_token oauth_endpoint
#' @export
make_new_token = function() {
key = getOption("rtypeform.client_id", "")
secret = getOption("rtypeform.client_secret", "")
scope = getOption("rtypeform.scopes_selected", "")

if (key == "" || is.null(key)) {
stop("option('rtypeform.client_id') has not been set")
}
if (secret == "" || is.null(secret)) {
stop("option('rtypeform.client_secret') has not been set")
}
if ((length(scope) == 1 && scope == "") || is.null(scope)) {
stop("option('rtypeform.scopes_selected') has not been set")
}

app = oauth_app("typeform", key = key, secret = secret)
endpoint = oauth_endpoint(authorize = "https://api.typeform.com/oauth/authorize",
access = "https://api.typeform.com/oauth/token")

typeform_token = oauth2.0_token(endpoint = endpoint,
app = app,
scope = scope)
typeform_token
}
1 change: 0 additions & 1 deletion R/delete_responses.R
Expand Up @@ -16,4 +16,3 @@ delete_responses = function(form_id, api = NULL, included_tokens = NULL) {
{included_tokens}")
delete_response(api = api, url)
}

11 changes: 6 additions & 5 deletions R/get_api.R
Expand Up @@ -6,11 +6,12 @@
#' the environment variable \code{Sys.getenv("typeform_api2")} is used.
#' @details In version 1 of the typeform API, rtypeform looked for the key \code{typeform_api},
#' @export
get_api = function(api = NULL){
if(is.null(api)) api = Sys.getenv("typeform_api2")

if(nchar(api) != 0) return(api)
if(nchar(Sys.getenv("typeform_api")) > 0)
get_api = function(api = NULL) {
if (is.null(api)) api = Sys.getenv("typeform_api2")
# if the passed token is actually an oauth token, then use that instead
if (inherits(api, "Token2.0")) api = api$credentials$access_token
if (is.character(api) && nchar(api) != 0) return(api)
if (nchar(Sys.getenv("typeform_api")) > 0)
stop("Old key")
stop("Invalid api key.", call. = FALSE)
}
1 change: 0 additions & 1 deletion R/get_form.R
Expand Up @@ -4,4 +4,3 @@ get_form = function(form_id, api = NULL) {
content = get_response(api = api, url)
content
}

3 changes: 1 addition & 2 deletions R/get_forms.R
Expand Up @@ -50,7 +50,7 @@ get_forms = function(api = NULL,
items = content$items
if (length(items) == 0) {
items = tibble(form_id = "", title = "", last_updated = "", is_public = "",
is_trial = "", questions = "", theme = "", questionnaire_url = "")[0,]
is_trial = "", questions = "", theme = "", questionnaire_url = "")[0, ]
return(items)
}
items = items %>%
Expand All @@ -70,4 +70,3 @@ get_forms = function(api = NULL,
attr(items, "page_count") = content$page_count
items
}

20 changes: 11 additions & 9 deletions R/get_responses.R
@@ -1,7 +1,10 @@
flatten_answers = function(a) {
tibs = map(a, as_tibble)
tibs %>%
map2(names(tibs), function(i, j) {colnames(i) = paste0(j, "_", colnames(i)); i}) %>%
map2(names(tibs), function(i, j) {
colnames(i) = paste0(j, "_", colnames(i))
i
}) %>%
bind_cols()
}

Expand All @@ -12,9 +15,9 @@ get_meta = function(content) {
items = content$items
if (length(items) == 0) {
empty_meta = tibble("landing_id" = "", "token" = "",
"landed_at" = "", "submitted_at" = "", "user_agent" = "",
"platform" = "", "referer" = "", "network_id" = "",
"browser" = "", "score" = "")[0,]
"landed_at" = "", "submitted_at" = "", "user_agent" = "",
"platform" = "", "referer" = "", "network_id" = "",
"browser" = "", "score" = "")[0, ]
return(empty_meta)
}
meta = items %>%
Expand Down Expand Up @@ -63,9 +66,9 @@ globalVariables(".")
#' @seealso https://developer.typeform.com/responses/reference/retrieve-responses/
#' @export
get_responses = function(form_id, api = NULL,
page_size = 25,
since = NULL, until = NULL, after = NULL, before = NULL,
completed = NULL, query = NULL, fields = NULL) {
page_size = 25,
since = NULL, until = NULL, after = NULL, before = NULL,
completed = NULL, query = NULL, fields = NULL) {
# Format dates
since = format_date_time(since)
until = format_date_time(until)
Expand Down Expand Up @@ -115,9 +118,8 @@ get_responses = function(form_id, api = NULL,
map2(question_types,
~select(.x,
"field_type", "landing_id", starts_with(paste0(.y, "_")))) %>%
map(unnest) %>%
map(~unnest(.x, cols = c())) %>%
map(~rename(.x, type = 1, value = 3))

c(list(meta = meta), all_answers)
}

31 changes: 28 additions & 3 deletions R/httr_responses.R
@@ -1,8 +1,19 @@
#' Get Authorziation
#'
#' A function to get authorization
#' @param api An authentication key
#' @export
get_authorization = function(api) {
api = get_api(api)
httr::add_headers(authorization = glue("bearer {api}"))
}

#' Check api response
#'
#' A function that checks the api response
#' @param resp A response
#' @param content The content
#' @export
#' @importFrom httr status_code
check_api_response = function(resp, content) {
status_code = httr::status_code(resp)
Expand All @@ -12,6 +23,13 @@ check_api_response = function(resp, content) {
stop(msg, call. = FALSE)
}

#' Get response
#'
#' A function that allows you to get a response from the
#' server.
#' @param api An authentication key
#' @param url The URL of the site
#' @export
#' @importFrom httr GET user_agent content add_headers
get_response = function(api, url) {
authorization = get_authorization(api)
Expand Down Expand Up @@ -39,7 +57,6 @@ put_response = function(api, url, ...) {
check_api_response(resp, content)
}


#' @importFrom httr DELETE
delete_response = function(api, url) {
authorization = get_authorization(api)
Expand All @@ -54,6 +71,15 @@ delete_response = function(api, url) {
check_api_response(resp, content)
}

#' Post response
#'
#' A function that allows you to post an update to the
#' server
#' @param api An authentication key
#' @param url The URL of the site
#' @param body The infrormation sent out to the server. e.g. the title
#' @param ... Other arguments
#' @export
#' @importFrom httr POST
post_response = function(api, url, body = NULL, ...) {
authorization = get_authorization(api)
Expand All @@ -73,9 +99,8 @@ patch_response = function(api, url, body = NULL, ...) {
ua = httr::user_agent("https://github.com/csgillespie/rtypeform")

resp = httr::PATCH(url, authorization, body = body, ua, ...)
cont = httr::content(resp, "text", encoding = "UTF-8")
content = httr::content(resp, "text", encoding = "UTF-8")

check_api_response(resp, content)
content
}

0 comments on commit 67f0f26

Please sign in to comment.