-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreachDemo.R
74 lines (56 loc) · 2.15 KB
/
reachDemo.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
library(httr)
library(rjson)
library(jsonlite)
library(tidyr)
library(tidyjson)
library(dplyr)
options(stringsAsFactors = FALSE)
user <- "[email protected]"
pass <- "cc0fa701711c91453d9a6b2e0fdc047d"
apiURL <- "https://api.reach.ai"
body <- '{"input":{"url":"http://mediaskunkworks.com", "maxPages" : 10}, "type" : "ai.reach.Event.ContactFinder"}'
# Form encoded
response <- POST(apiURL, body = body, encode = "json", authenticate(user, pass))
api_r <- content(response, as="text", encoding = "utf8" )
# gather (stack) the array by index
spread_values( # spread (widen) values to widen the data.frame
name = jstring("name"), # value of "name" becomes a character column
age = jnumber("age") # value of "age" becomes a numeric column
)
usable <- jsonlite::fromJSON(api_r,simplifyVector = TRUE, flatten=TRUE)
str(usable)
encQuery <- URLencode(query)
response <- GET(encQuery, authenticate(user, pass))
api_r <- content(response, as="text")
api_r <- jsonlite::fromJSON(api_r,simplifyVector = TRUE, flatten=TRUE)
r <- POST("http://httpbin.org/post", body = list(a = 1, b = 2, c = 3))
url <- "http://httpbin.org/post"
body <- list(a = 1, b = 2, c = 3)
# Form encoded
r <- POST(url, body = body, encode = "form")
# Multipart encoded
r <- POST(url, body = body, encode = "multipart")
# JSON encoded
r <- POST(url, body = body, encode = "json")
json <- '{
"name": "bob",
"shopping cart":
[
{
"date": "2014-04-02",
"basket": {"books": 2, "shirts": 0}
},
{
"date": "2014-08-23",
"basket": {"books": 1}
}
]
}'
json %>% as.tbl_json %>%
spread_values(customer = jstring("name")) %>% # Keep the customer name
enter_object("shopping cart") %>% # Look at their cart
gather_array %>% # Expand the data.frame and dive into each array element
spread_values(date = jstring("date")) %>% # Keep the date of the cart
enter_object("basket") %>% # Look at their basket
gather_keys("product") %>% # Expand the data.frame for each product and capture it's name
append_values_number("quantity")