12 APIs

12.1 REST APIs

RESTful

TODO - Foundation for Council services

12.2 Wrappers

TODO

12.3 Rectangling

Data returned from REST APIs are typically provided in a hierarchical structure and JSON is the most common format. The tidyverse Rectangling vignette explains how to structure the data from the API into a more familiar tabular structure.

TODO
scc-pi functions with rectangling e.g. postcode and geocoding

12.4 API via Council proxy

When in Council buildings and connected directly to the Council’s network, rather than via AlwaysOn VPN, you may have difficulty accessing external API’s from RStudio (and installing packages and using GitHub). The following post from Posit outlines a method to resolve this: Configuring R to Use an HTTP or HTTPS Proxy.

In summary:

  1. Run the command file.edit('~/.Renviron') within RStudio.

  2. Add the following settings to your .Renviron file, amended for your own Council login ID and password.

http_proxy=<proxyip>
http_proxy_user=<username>:<password>

https_proxy=<proxyip>
https_proxy_user=<username>:<password>
  1. Uncheck the setting for Use Internet Explorer library/proxy for HTTP in Global Options:
  1. Restart RStudio.

When using AlwaysOn VPN comment out the settings:

# http_proxy=<proxyip>
# http_proxy_user=<username>:<password>
# 
# https_proxy=<proxyip>
# https_proxy_user=<username>:<password>

These notes will be updated if we get to figuring out a setting that automatically toggles the proxy setting rather than you having to change this manually.

To test it’s working you could try the following:

#install.packages("httr2")
library(httr2)
req <-  request("https://data.police.uk/api/forces")
resp <- req_perform(req)
resp |> resp_body_html(check_type = FALSE)
## {html_document}
## <html>
## [1] <body><p>[{"id":"avon-and-somerset","name":"Avon and Somerset Constabular ...

Which should show you some content similar to what you see when you just use the same URL (https://data.police.uk/api/forces) in a browser:

These are the breadcrumbs for determining the proxy server and port:

  • Windows settings

  • Proxy settings

  • Type the script address into Edge i.e. http://172.29.3.101/proxy.pac

  • Open the downloaded proxy.pac file in a text editor

  • Try some of the proxy servers and ports listed in proxy.pac