|
| 1 | +# APIs |
| 2 | + |
| 3 | +> ... there is software that, using “APIs,” |
| 4 | +**treats other web sites as just another part of the software infrastructure, another |
| 5 | +function it can call to get things done.** |
| 6 | +> |
| 7 | +>Our computers are so small and the Web so great and vast that this last scenario |
| 8 | +seems like part of an inescapable trend. Why wouldn’t you depend on other |
| 9 | +web sites whenever you could, making their endless information and bountiful abilities |
| 10 | +a seamless part of yours? And so, I suspect, such uses will become increasingly common until, one day, your computer is as tethered to the Web as you yourself are |
| 11 | +now. |
| 12 | +> |
| 13 | +>\- [Swartz: Introduction](https://web.archive.org/web/20220121020916/https://www.morganclaypool.com/doi/pdfplus/10.2200/S00481ED1V01Y201302WBE005) |
| 14 | +
|
| 15 | + |
| 16 | +## How Web Apps are Structured |
| 17 | + |
| 18 | +> The other week I made one of my rare excursions from my plushly-appointed bed |
| 19 | +and attended a local party. There I met a man who made a website for entering |
| 20 | +and visualizing data. I asked him whether he had an API, since it seemed so useful |
| 21 | +for such a data-intensive site. He didn’t, he said; it would be too much work to |
| 22 | +maintain both a normal application and an API. |
| 23 | +> |
| 24 | +> I tell you this story because the fellow at the party was wrong, but probably |
| 25 | +in the same way that you are wrong, and I don’t want you to feel bad. If even well dressed |
| 26 | +young startup founders at exclusive Williamsburg salons make this mistake, |
| 27 | +it’s no grave sin. |
| 28 | +> |
| 29 | +> See, the mistake is, that if you design your website following the principles |
| 30 | +in this book, **the API isn’t a separate thing from your normal website, but a natural |
| 31 | +extension of it**. All the principles we’ve talked about—smart URLs, GET and |
| 32 | +POST, etc.—apply equally well to web sites or APIs. The only difference is that |
| 33 | +instead of returning HTML, you’ll want to return JSON instead. |
| 34 | +> |
| 35 | +> \- [Swartz (Chapter 5)](https://web.archive.org/web/20220121020916/https://www.morganclaypool.com/doi/pdfplus/10.2200/S00481ED1V01Y201302WBE005) |
| 36 | +
|
| 37 | + |
| 38 | +## Resources |
| 39 | + |
| 40 | +> “The key abstraction of information in REST is a resource. **Any information that can be named can be a resource**: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g. a person), and so on. In other words, any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time.” - Roy Fielding’s dissertation. |
| 41 | +
|
| 42 | +### URLs (Uniform Resource Locators) |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +[https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL) |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +### Thinking in terms of "Resources" |
| 52 | + |
| 53 | +#### ❇️ Example |
| 54 | + |
| 55 | +What are the "Resources" on Crunchbase? (Lets try to map out their database, what do we think it looks like?). [https://www.crunchbase.com/](https://www.crunchbase.com/). |
| 56 | + |
| 57 | + |
| 58 | +### What a URL reveals about a web app |
| 59 | + |
| 60 | +> Furthermore, URLs have to last. Those t-shirts and links and blogs will |
| 61 | +not disappear simply because you decided to reorganize your server, or move to a |
| 62 | +different operating system, or got promoted and replaced by a subordinate (or voted |
| 63 | +out of office). They will last for years and years to come, so your URLs must last |
| 64 | +with them. |
| 65 | +> |
| 66 | +> Moreover, URLs do not just exist as isolated entities (like “http:// |
| 67 | +example.org/lunch/bacon.html”). They combine to form patterns |
| 68 | +(“bacon.html”, “lettuce.html”, “tomato.html”). And each of these |
| 69 | +patterns finds its place in a larger path of interaction (“/”, “/lunch/”, |
| 70 | +“/lunch/bacon.html”). |
| 71 | +> |
| 72 | +> Because of all this, URLs cannot be some side-effect or afterthought, as |
| 73 | +many seem to wish. Designing URLs is the most important part of building a web |
| 74 | +application and has to be done first. Encoded in their design are a whole series of |
| 75 | +implicit assumptions about what your site is about, how it is structured, and how it |
| 76 | +should be used; all important and largely-unavoidable questions. |
| 77 | +> |
| 78 | +> \- [Swartz (Chapter 2:: Building for Users: Designing URLs)](https://web.archive.org/web/20220121020916/https://www.morganclaypool.com/doi/pdfplus/10.2200/S00481ED1V01Y201302WBE005) |
| 79 | +
|
| 80 | + |
| 81 | +## A Case Study |
| 82 | + |
| 83 | +Let's take a look at https://www.fec.gov/. This is the website for the Federal Election Commission (FEC). It contains data about federal campaign finance in the United States in a format for humans to consume. |
| 84 | + |
| 85 | +And let's look at the FEC API: [https://api.open.fec.gov/developers/](https://api.open.fec.gov/developers/). This API has the same data as the website, but in a format that computers can consume (JSON). |
| 86 | + |
| 87 | +## Querying APIs With Python |
| 88 | + |
| 89 | +To query an API with python, we're going to use the `requests` library (http://docs.python-requests.org/en/master/). The `requests` library makes it very simple to run basic HTTP commands like issue a GET request and parse the response or issue a POST request. |
| 90 | + |
| 91 | +Here is a simple example using that library to get some data on candidate H8WI01024, Paul Ryan. |
| 92 | + |
| 93 | +```python |
| 94 | +import requests |
| 95 | + |
| 96 | +url = "https://api.open.fec.gov/v1/candidate/H8WI01024/?sort=name&page=1&api_key=DEMO_KEY&per_page=20" |
| 97 | + |
| 98 | +# Issue a GET reequest to the URL specified |
| 99 | +response = requests.get(url) |
| 100 | + |
| 101 | +# Parse JSON data out from the response |
| 102 | +data = response.json() |
| 103 | +``` |
| 104 | + |
| 105 | +### Querying the FEC API |
| 106 | + |
| 107 | +Lets take a look at the script Ihsaan has written to hit the FEC API. You can find the script here: https://github.com/dmil/code4policy/blob/master/apis/ |
| 108 | + |
| 109 | +or pasted below: |
| 110 | + |
| 111 | +```python |
| 112 | +import requests |
| 113 | +import json |
| 114 | +import os |
| 115 | + |
| 116 | +# base url for specific api |
| 117 | +base_url = 'https://api.open.fec.gov/v1/' |
| 118 | + |
| 119 | +# operation to execute for the api |
| 120 | +operation = 'candidates' |
| 121 | + |
| 122 | +# get key from environment variable |
| 123 | +key = os.environ['FECKEY'] |
| 124 | + |
| 125 | +# additional api parameters specific to the operation |
| 126 | +api_parameters = {'api_key': key, 'office':'H', 'sort':'name', 'state':'MA', 'election_year':[2016]} |
| 127 | + |
| 128 | +# ping api |
| 129 | +response = requests.get(base_url + operation, params = api_parameters) |
| 130 | + |
| 131 | +# print status code and load returned data into json |
| 132 | +print('Response Code: {0}\n'.format(response.status_code)) |
| 133 | +data = json.loads(response.text) |
| 134 | + |
| 135 | +# save raw data |
| 136 | +with open('fec_api_results.json', 'w') as outfile: |
| 137 | + json.dump(data, outfile) |
| 138 | + |
| 139 | +# loop through results and print name |
| 140 | +for candidate in data['results']: |
| 141 | + print(candidate['name']) |
| 142 | +``` |
| 143 | + |
| 144 | +## JSON/XML & More about APIs |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +source: [https://www.infoq.com/presentations/API-design-mistakes](https://www.infoq.com/presentations/API-design-mistakes) |
| 149 | + |
| 150 | + |
| 151 | +APIs from Programmable Web till December 2013 |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +source: [https://www.infoq.com/presentations/API-design-mistakes](https://www.infoq.com/presentations/API-design-mistakes) |
| 157 | + |
| 158 | +> And so the “Semantic Web Activity” at the Worldwide Web Consortium |
| 159 | +(W3C) has spent its time writing standard upon standard...Few have received any widespread use and those that have (XML) are uniformly |
| 160 | +scourges on the planet, offenses against hardworking programmers that have |
| 161 | +pushed out sensible formats (like JSON) in favor of overly-complicated hairballs |
| 162 | +with no basis in reality (I’m not done yet!—more on this in chapter 5). |
| 163 | + |
| 164 | +[Aaron Swartz’s A Programmable Web](https://web.archive.org/web/20220121020916/https://www.morganclaypool.com/doi/pdfplus/10.2200/S00481ED1V01Y201302WBE005): Chapter 1 - Introduction: A Programmable Web |
| 165 | + |
| 166 | +> JSON (pronounced like “Jason”), for the uninitiated, is a simple format for |
| 167 | +exchanging basic pieces of data between software. Originally based on JavaScript |
| 168 | +but quickly adopted by nearly every major language, it makes it easy to share data |
| 169 | +over the Web. |
| 170 | + |
| 171 | +> Wait!, you may cry, I thought XML was for sharing data on the Web. Sadly, |
| 172 | +you have been misled by a sinister and harmful public relations campaign. XML is |
| 173 | +probably just about the worst format for sharing data. Here’s why: |
| 174 | + |
| 175 | + |
| 176 | +[Aaron Swartz’s A Programmable Web](https://web.archive.org/web/20220121020916/https://www.morganclaypool.com/doi/pdfplus/10.2200/S00481ED1V01Y201302WBE005): Chapter 5 - BUILDING A PLATFORM: PROVIDING APIS |
0 commit comments