Skip to contents
project_folder <- dirname(getwd())
path_to_env <- file.path(project_folder, ".env")
readRenviron(path_to_env)
api_base_url <- "https://restful-booker.herokuapp.com/"
auth_token <- Sys.getenv("AUTH_TOKEN")
headers <- list("Accept" = "application/json", "Content-type" = "application/json")

Let’s instantiate the API client

booking_client <- httpeasyrest::HttpRestClient$new(
  base_url = api_base_url,
  headers = headers,
  token = auth_token
)

Get all booking IDS as a DATAFRAME

api_data <- booking_client$get_dataframe(end_point = "booking")
head(api_data$http_resp)
#>   bookingid
#> 1        44
#> 2       163
#> 3       281
#> 4       273
#> 5        64
#> 6       101

Get all bookings IDS as a JSON object

api_data <- booking_client$get_object(end_point = "booking")
head(api_data$http_resp)
#> [[1]]
#> [[1]]$bookingid
#> [1] 44
#> 
#> 
#> [[2]]
#> [[2]]$bookingid
#> [1] 163
#> 
#> 
#> [[3]]
#> [[3]]$bookingid
#> [1] 281
#> 
#> 
#> [[4]]
#> [[4]]$bookingid
#> [1] 273
#> 
#> 
#> [[5]]
#> [[5]]$bookingid
#> [1] 64
#> 
#> 
#> [[6]]
#> [[6]]$bookingid
#> [1] 101

Get single specific booking record as JSON object

Notice that this specific API maps the id identifying the record in the URL itself, rather than as a query string

booking_id <- api_data$http_resp[[1]]$bookingid
api_data <- booking_client$get_object(end_point = paste0("booking/", booking_id))
api_data$http_resp
#> $firstname
#> [1] "Jane"
#> 
#> $lastname
#> [1] "Doe"
#> 
#> $totalprice
#> [1] 111
#> 
#> $depositpaid
#> [1] TRUE
#> 
#> $bookingdates
#> $bookingdates$checkin
#> [1] "2018-01-01"
#> 
#> $bookingdates$checkout
#> [1] "2019-01-01"
#> 
#> 
#> $additionalneeds
#> [1] "Extra pillows please"

Create a single booking object afresh

Let’s create a unique name that we can confidently search for it afterwards ensuring that is unique, and therefore its fetching is not down to luck - recall this API is used by many people.

unique_name <- stringi::stri_rand_strings(1, 12)
data <- list(
  firstname = unique_name,
  lastname = "Jimenez",
  totalprice = 111,
  depositpaid = TRUE,
  bookingdates = list(
    checkin = "2018-01-01",
    checkout = "2019-01-01"
  ),
  additionalneeds = "Breakfast"
)
api_data <- booking_client$post_object(end_point = "booking", data)
testit::assert(isTRUE(api_data$success))
api_data$http_resp
#> $bookingid
#> [1] 457
#> 
#> $booking
#> $booking$firstname
#> [1] "5pb90SUHjlsA"
#> 
#> $booking$lastname
#> [1] "Jimenez"
#> 
#> $booking$totalprice
#> [1] 111
#> 
#> $booking$depositpaid
#> [1] TRUE
#> 
#> $booking$bookingdates
#> $booking$bookingdates$checkin
#> [1] "2018-01-01"
#> 
#> $booking$bookingdates$checkout
#> [1] "2019-01-01"
#> 
#> 
#> $booking$additionalneeds
#> [1] "Breakfast"

Let’s ensure that such object was indeed sent to the API successfully.

end_point <- paste0("booking/", api_data$http_resp$bookingid)
inserted_booking <- booking_client$get_object(end_point = end_point)
testit::assert(inserted_booking$http_resp$firstname == unique_name)
inserted_booking$http_resp
#> $firstname
#> [1] "5pb90SUHjlsA"
#> 
#> $lastname
#> [1] "Jimenez"
#> 
#> $totalprice
#> [1] 111
#> 
#> $depositpaid
#> [1] TRUE
#> 
#> $bookingdates
#> $bookingdates$checkin
#> [1] "2018-01-01"
#> 
#> $bookingdates$checkout
#> [1] "2019-01-01"
#> 
#> 
#> $additionalneeds
#> [1] "Breakfast"

Delete a single booking object

Now we want to delete the same object we just inserted. Unfortunately the API does not allow deleting records, probably to avoid clashes among the multiple simultaneous users that may be calling the API. APIs usually accomplish that by returning an HTTP code = 403. You can know more about HTTP codes here https://cheatography.com/kstep/cheat-sheets/http-status-codes/.

end_point <- "booking/"
ret <- booking_client$delete_object(end_point = end_point, field = api_data$http_resp$bookingid)
testit::assert(ret$status_code == 403)

t::assert(isFALSE(deleted_booking$success)) ```