Get adoptable pets from Petfinder
Source:R/get_petfinder_data.R
, R/petfinder_get_token.R
get_petfinder_data.Rd
Downloads records of adoptable pets. You will need to have a key and a secret from the [PetFinder API](https://www.petfinder.com/developers/).
Usage
get_petfinder_data(
token = NULL,
endpoint = "animals",
params = list(type = "cat", location = "68528"),
page = 1,
base_url = "https://api.petfinder.com/v2/"
)
petfinder_get_token(
key = get_api_key("PETFINDER_API"),
secret = get_api_secret("PETFINDER_API"),
base_url = "https://api.petfinder.com/v2"
)
Arguments
- token
authentication token. Can be passed in as a result of calling `petfinder_get_token`. If absent, one attempt is made at creating a new token.
- endpoint
character value - which endpoint of the Petfinder API is being used? API v2 has endpoints `animals` and `organizations`.
- page
positive integer value, defaults to one. For multi-page results, a way to pick up on a previous result.
- base_url
characterstring of the API
- key
character of the API key, if not specified, loaded from the environment
- secret
character of the API secret, if not specified, loaded from the environment
- param
(list of) parameters to pass to the endpoint. These will be incorporated into the query in the form of `name='value'&...`
Value
A list of (pages of) records returned from the Petfinder API (JSON-style list). Each element in the list represents one page of results. Each page of results is organized in form of 'animals' (records of about 20 animals) and 'pagination'. Records are returned in increasing page order.
Examples
# example code
# Make sure to get a token before your first use:
petf_token <- petfinder_get_token()
#> Searching for key <PETFINDER_API_KEY> ...
#> Error in get_api_key("PETFINDER_API"):
#> No API key found, please supply with `key` argument or store in environment
cats <- get_petfinder_data(token=petf_token)
#> Error: object 'petf_token' not found
length(cats)
#> Error: object 'cats' not found
# each element is a list of animals and pagination information:
cats[1:5] |> str(max.level = 2)
#> Error: object 'cats' not found
cats[[21]]$pagination[5] # the last element in the pagination vector contains the link to the next set of records, if available
#> Error: object 'cats' not found
cats2 <- get_petfinder_data(token=petf_token, page = 22)
#> Error: object 'petf_token' not found
# not a complete list anymore:
cats2[[21]]$animals
#> Error: object 'cats2' not found
animals <- cats |> purrr::map(.f = function(x) x$animals)
#> Error: object 'cats' not found
animals[21+(1:21)] <- cats2 |> purrr::map(.f = function(x) x$animals)
#> Error: object 'cats2' not found
animals <- animals |> purrr::compact() # get rid of empty elements
#> Error: object 'animals' not found
length(animals)
#> Error: object 'animals' not found
# flatten the structure once:
animal_record_list <- animals |> purrr::flatten()
#> Error: object 'animals' not found
jsonlite::write_json(animal_record_list, path = "cats.json")
#> Error: object 'animal_record_list' not found
# Authenticate with the petfinder API and get a token
token <- petfinder_get_token()
#> Searching for key <PETFINDER_API_KEY> ...
#> Error in get_api_key("PETFINDER_API"):
#> No API key found, please supply with `key` argument or store in environment