Last active
April 19, 2020 02:10
-
-
Save mdneuzerling/b989e6973df7dc8d38a7eae110de4e53 to your computer and use it in GitHub Desktop.
Example HTTP GET request for accessing the PTV API in R
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 function formulates and submits a HTTP GET request to the Public | |
# Transport Victoria (PTV) API. In particular, it calculates the "signature" | |
# component of the URL required for authentication. While I've included the | |
# version number as an argument, I haven't tested this function on versions | |
# other than 3. | |
# | |
# See URL below for information on obtaining a user ID (also called a devid) and | |
# an API key: | |
# https://www.ptv.vic.gov.au/footer/data-and-reporting/datasets/ptv-timetable-api/ | |
# | |
# Example usage: ptv_get("routes", user_id, api_key) | |
# Required libraries: httr, digest, glue | |
ptv_get <- function(request, user_id, api_key, version = 3) { | |
base_url <- "http://timetableapi.ptv.vic.gov.au" | |
signature <- digest::hmac( | |
key = api_key, | |
object = glue::glue("/v{version}/{request}?devid={user_id}"), | |
algo = "sha1" | |
) | |
complete_url <- glue::glue( | |
base_url, | |
"/v{version}", | |
"/{request}", | |
"?devid={user_id}", | |
"&signature={toupper(signature)}" | |
) | |
httr::GET(url = complete_url) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment