-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.R
123 lines (92 loc) · 2.91 KB
/
demo.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
library(httr2)
req <- request("https://api.github.com/")
req_dry_run(req)
resp <- req_perform(req, verbosity = 3)
resp |> resp_content_type()
resp |>
resp_body_json() |>
str()
req |> req_dry_run()
req <- request("https://r-project.org")
req |> req_perform(verbosity = 1)
req |> req_perform(verbosity = 2)
req |> req_perform(verbosity = 3)
with_verbosity(req |> req_perform(), verbosity = 1)
# Errors ------------------------------------------------------------------
req <- request("adslkfjdsalkjfadl;kfskd")
resp <- req_perform(req)
req <- request("https://api.github.com/doesntexist")
resp <- req_perform(req)
req <- request("https://api.github.com/user")
resp <- req_perform(req)
last_request()
last_response()
last_response() |> resp_body_json()
# Auth --------------------------------------------------------------------
# Revoke in https://github.com/settings/applications/1636322
# Then clear the cache: rm("7b91122ab057511c0376f115ab9a96dd", envir = httr2:::the$token_cache)
request("https://api.github.com/user") |>
req_oauth_auth_code(
client = example_github_client(),
auth_url = "https://github.com/login/oauth/authorize"
) |>
req_perform() |>
resp_body_json() |>
str()
request("https://api.github.com/user") |>
req_oauth_device(
client = example_github_client(),
auth_url = "https://github.com/login/device/code"
) |>
req_perform() |>
resp_body_json() |>
str()
# Show how to create a new app in https://github.com/settings/developers
# Pagination --------------------------------------------------------------
req_auth_github <- function(req) {
req_oauth_auth_code(
req,
client = example_github_client(),
auth_url = "https://github.com/login/oauth/authorize"
)
}
# https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repositories-for-a-user
req <- request("https://api.github.com") |>
req_template("/users/{username}/repos", username = "hadley") |>
req_auth_github()
resp <- req |> req_perform()
resp |>
resp_body_json() |>
View()
resp |> resp_headers()
resp |> resp_header("link")
resp |> resp_link_url("next")
resp2 <- req |>
req_url(resp |> resp_link_url("next")) |>
req_perform()
resp2 |> resp_body_json() |> View()
resp2 |> resp_link_url("next")
# Wouldn't it be nice if we could keep going until the end??
resps <- req_perform_iterative(
req,
next_req = function(resp, req) {
req |> req_url(resp |> resp_link_url("next"))
}
)
# Need to return NULL to indicate that there no pages left to scrape
next_req <- function(resp, req) {
next_url <- resp |> resp_link_url("next")
if (is.null(next_url)) {
return(NULL)
}
req |> req_url(next_url)
}
resps <- req_perform_iterative(req, next_req, max_reqs = Inf)
length(resps)
resps[[1]] |> resp_body_json() |> View()
resps[[2]] |> resp_body_json() |> View()
data <- resps_data(resps, resp_body_json)
data |> View()
library(tidyverse)
tibble(json = data) |>
unnest_wider(json)