|
146 | 146 | "cell_type": "markdown", |
147 | 147 | "metadata": {}, |
148 | 148 | "source": [ |
149 | | - "## Reading responses" |
| 149 | + "## Reading responses\n", |
| 150 | + "\n", |
| 151 | + "As you can see above, the response returns a JSON (JavaScript Object Notation) object with a few top-level keys. If you're thinking, \"Hm, this JSON looks an awful lot like a Python dictionary,\" you're absolutely right. While the semantics of Python dictionaries and JSON _are_ different, in this case, the `requests` library has already coerced the raw JSON to a Python dictionary for us. You can access its values like you would with any Python dict:" |
150 | 152 | ] |
151 | 153 | }, |
152 | 154 | { |
|
155 | 157 | "metadata": {}, |
156 | 158 | "outputs": [], |
157 | 159 | "source": [ |
158 | | - "# Reading responses" |
| 160 | + "parsed_response = response.json()\n", |
| 161 | + "\n", |
| 162 | + "parsed_response['count']" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "markdown", |
| 167 | + "metadata": {}, |
| 168 | + "source": [ |
| 169 | + ":::{important}\n", |
| 170 | + "Experiment a bit. How, for example, would you get all of the titles in a list?\n", |
| 171 | + ":::" |
159 | 172 | ] |
160 | 173 | }, |
161 | 174 | { |
162 | 175 | "cell_type": "markdown", |
163 | 176 | "metadata": {}, |
164 | 177 | "source": [ |
165 | | - "## Constructing queries" |
| 178 | + "## Constructing queries\n", |
| 179 | + "\n", |
| 180 | + "Naturally, when you're working with an API, you'll want to be able to construct your own queries. Above, we hard-coded the value `weasels` under the querystring parameter `q`. But you can use Python's string interpolation to set any value you want. For example" |
166 | 181 | ] |
167 | 182 | }, |
168 | 183 | { |
|
171 | 186 | "metadata": {}, |
172 | 187 | "outputs": [], |
173 | 188 | "source": [ |
174 | | - "# Constructing queries" |
| 189 | + "my_query = \"foxes\"\n", |
| 190 | + "my_url = f\"https://api.dp.la/v2/items?q={my_query}&api_key={DPLA_API_KEY}\"\n", |
| 191 | + "\n", |
| 192 | + "response = requests.get(my_url)\n", |
| 193 | + "\n", |
| 194 | + "parsed_response = response.json()\n", |
| 195 | + "parsed_response" |
| 196 | + ] |
| 197 | + }, |
| 198 | + { |
| 199 | + "cell_type": "markdown", |
| 200 | + "metadata": {}, |
| 201 | + "source": [ |
| 202 | + "You could even write a function that puts constructs the request URL and returns the parsed response so that you don't have to do these things manually over and over again." |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": null, |
| 208 | + "metadata": {}, |
| 209 | + "outputs": [], |
| 210 | + "source": [ |
| 211 | + "def make_dpla_request(query: str):\n", |
| 212 | + " url = f\"https://api.dp.la/v2/items?q={query}&api_key={DPLA_API_KEY}\"\n", |
| 213 | + " response = requests.get(url)\n", |
| 214 | + "\n", |
| 215 | + " return response.json()" |
| 216 | + ] |
| 217 | + }, |
| 218 | + { |
| 219 | + "cell_type": "markdown", |
| 220 | + "metadata": {}, |
| 221 | + "source": [ |
| 222 | + "There's a problem with this code, however. What happens if you try to make a request with a query that contains spaces, such as `\"red foxes\"`?\n", |
| 223 | + "\n", |
| 224 | + "Can you find the appropriate workaround using the documentation? https://pro.dp.la/developers/requests\n", |
| 225 | + "\n", |
| 226 | + "What other features does this API support?" |
| 227 | + ] |
| 228 | + }, |
| 229 | + { |
| 230 | + "cell_type": "markdown", |
| 231 | + "metadata": {}, |
| 232 | + "source": [ |
| 233 | + "## RESTful APIs\n", |
| 234 | + "\n", |
| 235 | + "Many APIs, including the DPLA's, are built on RESTful principles. REST stands for **Re**presentational **S**tate **T**ransfer. In terms of web APIs, REST means that a given server will respond with a representation of the data that it has available, and that representation will contain additional information for manipulating the data or requesting further data.\n", |
| 236 | + "\n", |
| 237 | + "Although it is not, strictly speaking, a requirement of REST APIs, many REST implementations use a predictable URL scheme.\n", |
| 238 | + "\n", |
| 239 | + "For example, you might find a list of \"collections\" at the `/collections` endpoint. To request a specific collection, you would append its ID — e.g., for Collection 3, `/collections/3`.\n", |
| 240 | + "\n", |
| 241 | + "Each collection might contain items, so to get a list of items in Collection 3 you could send a request to `/collections/3/items`. And then to get a specific item in that collection — you guessed it, `/collections/3/items/12`.\n", |
| 242 | + "\n", |
| 243 | + "DPLA does _not_ implement this kind of schema, and instead relies on facets and other search parameters. But it is worth being aware of such schemes if you want to use other APIs in your work and research." |
175 | 244 | ] |
176 | 245 | }, |
177 | 246 | { |
|
0 commit comments