Coder Social home page Coder Social logo

python-p4-working-with-apis's Introduction

Working with APIs

NOTE: The API used in this lab can't be accessed anymore. We have an updated lesson here.

Learning Goals

  • Learn some of the basic ways to work with APIs.

Key Vocab

  • Request: an attempt by one machine to contact another over the internet.
  • Client: an application or machine that accesses services being provided by a server through the internet.
  • Web Server: a combination of software and hardware that uses Hypertext Transfer Protocol (HTTP) and other protocols to respond to requests made over the internet.

Define API

An API, or application programming interface, is a manner in which companies and organizations, like Twitter or the New York City government, expose their data and/or functionality to the public (i.e. talented programmers like yourself) for use. APIs allow us to add important data and functionality to the applications we build. Here's just a few examples of some of the cool things you can do by using APIs:

  • Create an app that allows users to sign up/sign in via Facebook/Google/Twitter/Github/etc.
  • Use the NYC Open Data API to get and map data––everything from Health Department restaurant ratings to public park locations and hours to New York City Public Housing repair issues to noise complaints to public school construction, you name it!
  • Use the Yelp API to find and deliver popular local spots to your users.
  • Use the Weather Underground API to give your users up-to-date weather alerts.
  • Use the Ticket Master API to inform your users if their favorite musician has an upcoming show.
  • So much more!

This is just a small sample of what working with APIs allows us to do as developers. Throughout the course of your programming life, you'll likely be exposed to working with many different APIs. You'll even learn how to build your own API later on in this course. This reading seeks to introduce the topic, emphasize some of the benefits of getting comfortable working with APIs and offer a brief intro into some of the common methods of working with APIs.


How to Work with APIs

Different APIs expose their data and functionalities in different ways. However, there are commonalities among them and there are common approaches that we'll discuss here. Generally speaking, there are two main uses for APIs––getting data and adding functionality (i.e. signing in with Facebook or posting to Instagram). We'll be discussing the "getting data" part of working with APIs here.

Many APIs are built on what is referred to as a REST-ful framework. That means that the "endpoints", or URLs to which we can send a request for data, follow certain conventions. These URLs should allow you to request information, send information, update information and delete information. Let's focus on the "getting information" request.

Retrieving Data from an API

For this walk-through, we'll be working with the NYC Open Data API. Let's say we've been hired by the city to create an app that will help parents sign their children up for after-school activities. To connect parents to after-school clubs around the city, we need a data set of such clubs. Luckily for us, the city has collected that information and allows the public to access it via their Open Data API. Let's check out the documentation for their after-school club data set.

Finding the API Endpoint

Take a few minutes and familiarize yourself with resource linked to above. Learning how to read API documentation is an important skill. It's okay if you don't understand everything in the above document. For now, just look it over briefly.

As you read through, you'll see that the documentation lists the API endpoint. Endpoint refers to the URL we can submit a request to and that will return to us the desired data.

Open up a new tab in your browser and paste in the following URL: http://data.cityofnewyork.us/resource/uvks-tn5n.json

The page brings you to is the desired set of data! Notice that the data is laid out in what looks like a big list of nested dictionaries. This is actually a JSON object, which behaves just like a Python dictionary. Working with the JSON data returned to you by requests to an API is one of the reasons why we spent so much time in previous lessons learning how to manipulate and operate on nested hashes.

Top Tip: Once you find the right URL for retrieving your data, test it out directly in your browser before you try to request the data from inside your program. If pasting the URL into your browser brings you to the right data, you can move on. This cuts down on debugging time. This way, once you try to request the data from within your program, if it doesn't work, at least you'll know it's something wrong with your code, as opposed to something wrong with the API.

Sending a Request to an API from a Program or Application

Now that we understand what an API is and have even dealt with a URL that takes us to a real API endpoint, let's use that same URL to send a request for data from a Python program. Fork and clone this repo locally.

Open up lib/nyc_api.py. Let's take a look at the code here:

import requests
import json

class GetPrograms:

  def get_programs(self):
    URL = "http://data.cityofnewyork.us/resource/uvks-tn5n.json"

    response = requests.get(URL)
    return response.content


programs = GetPrograms().get_programs()
print(programs)

The requests library allows your program or application to send HTTP requests.

We define a get_programs() method that assigns our API endpoint to a variable name URL. The method submits a request with that URL using the get() method defined in the requests library, and returns the content of the response.

Now, in your terminal in the directory of this lab, run python lib/nyc_api.py. It should output the JSON response from the NYC Open Data API!

Working with API Data

Take another look at the API described at after-school club data set: https://dev.socrata.com/foundry/data.cityofnewyork.us/szgz-awuh

If you scroll down to the section that lists the data fields available, you'll see many fields including agency. We'll write a new method to list this field for each program.

Let's write a method, program_agencies, that just returns a list of the organizations that are running our after school programs.

Copy and paste the following code into our GetPrograms class:

def program_agencies(self):
    # we use the JSON library to parse the API response into nicely formatted JSON
        programs_list = []
        programs = json.loads(self.get_programs())
        for program in programs:
            programs_list.append(program["agency"])

        return programs_list

At the bottom of the file, comment out:

programs = GetPrograms().get_programs()
print(programs)

Let's print out a unique list of the agencies. Paste in the following two lines right underneath where you commented out the above two lines. We will create a set from the resulting list of agencies to avoid printing duplicates.

programs = GetPrograms()
agencies = programs.program_agencies()

for agency in set(agencies):
    print(agency)

Now, run the program with python lib/nyc_api.py in your terminal. You should see something like this:

Harlem Educational Activities Fund, Inc.
New Life Child Development Corporation
Hudson Guild
Big Five Block Association Inc.
Shuang Wen Academy Network
Sesame Flyers International, Inc.
Women's Housing and Economic Development Corporation (WHEDCO
Latino Pastoral Action Center, Inc.
Inwood Community Services, Inc.
Yeshiva Kehilath Yakov
Filipino American Human Services, Inc.
Manhattan Youth Recreation & Resources Center
Greater Ridgewood Youth Council, Inc
...

So far we've used the requests to send a request for data to an API endpoint, a URL. We've operated on the data that was returned to us, making sure it was formatted properly as JSON and iterating over that JSON to retrieve the name of the school hosting each after-school program. That was a lot of work! I wonder if there is an easier way to work with popular APIs...


Solution

The final version of nyc_api.py:

import requests
import json

class GetPrograms:

  def get_programs(self):
    URL = "http://data.cityofnewyork.us/resource/uvks-tn5n.json"

    response = requests.get(URL)
    return response.content

  def program_agencies(self):
    # we use the JSON library to parse the API response into nicely formatted JSON
        programs_list = []
        programs = json.loads(self.get_programs())
        for program in programs:
            programs_list.append(program["agency"])

        return programs_list

#programs = GetPrograms().get_programs()
#print(programs)

programs = GetPrograms()
agencies = programs.program_agencies()

for agency in set(agencies):
    print(agency)

Conclusion

To recap: APIs generally either provide a user with data or added functionality. We can use APIs that serve data to get information for our own applications and projects. To get this data, we need to send a request to the URL of the API and know how to work with the response we receive. Many APIs serve data in JSON format, which needs to be parsed before we can use it. Once parsed, it becomes a dictionary we can work with and extract data from.

In our example, we were able to retrieve remote information from an API using the requests library. By putting this implementation inside a class, we can develop highly reusable code that lets us access all sorts of information remotely.

The GetPrograms class used a hard-coded URL, stored as a class constant, and included an instance method called get_programs:

def get_programs(self):
    URL = "http://data.cityofnewyork.us/resource/uvks-tn5n.json"

    response = requests.get(URL)
    return response.content

But we could easily adapt this code to be flexible and accept any URL we pass in using an initialize method and an instance variable. The get_programs method is really just getting the response body from the requested URL, so we could name this get_response_body to be more accurate. We could replace the custom program_school method with a general load_json method, as well. Instead of a specific class, we would instead have a class that retrieves JSON from any provided URL!


Resources

python-p4-working-with-apis's People

Contributors

alveem avatar jlboba avatar linda-seiter avatar lizbur10 avatar pgill97 avatar professor-ben avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-p4-working-with-apis's Issues

Solution code matches main branch code, so some of the instructions do not make sense/are not applicable

Canvas Link

https://learning.flatironschool.com/courses/6259/assignments/233483?module_item_id=547391

Concern

Hi - I don't know if this is an actual issue, but I just wanted to let you know that there's some discrepancies between the main branch code and the README instructions.

The instructions tell you to:
Open up lib/nyc_api.py. Let's take a look at the code here:

import requests
import json

class GetPrograms:

  def get_programs():
    URL = "http://data.cityofnewyork.us/resource/uvks-tn5n.json"

    response = requests.get(URL)
    return response.content


programs = GetPrograms.get_programs()
print(programs)

This does not match the code that's in this file already when you clone it down:

import requests
import json

class GetPrograms:
    def __init__(self):
        pass

    def get_programs(self):
        URL = "http://data.cityofnewyork.us/resource/uvks-tn5n.json"

        response = requests.get(URL)
        return response.content

    def program_school(self):
    # we use the JSON library to parse the API response into nicely formatted JSON
        programs_list = []
        programs = json.loads(self.get_programs())
        for program in programs:
            programs_list.append(program["agency"])

        return programs_list

programs = GetPrograms()
programs_schools = programs.program_school()

for school in set(programs_schools):
    print(school)

So when you get to the part of the instructions that tell you to:
Copy and paste the following code into our GetPrograms class:

def program_school(self):
    # we use the JSON library to parse the API response into nicely formatted JSON
    programs_list = []
        programs = json.loads(self.get_programs())
        for program in programs:
            programs_list.append(program["agency"])

        return programs_list

you do not have to copy/paste because that code is already there. Then when the instructions tell you to:

At the bottom of the file, comment out:

programs = GetPrograms.get_programs()
print(programs)

...you don't have to do this either because that code wasn't there to begin with.

Same goes for the next set of instructions:

Let's print out a unique list of the schools. Paste in the following two lines right underneath where you commented out the above two lines:

programs = GetPrograms()
programs_schools = programs.program_school()

for school in set(programs_schools):
    print(school)

You don't have to do this either because this code is already there when you clone it down.

Just letting you know that the instructions do not really match or apply to the main branch code and currently, no real updates are needed to the file.

Additional Context

No response

Suggested Changes

No response

API's used as examples are no longer open source

Canvas Link

https://learning.flatironschool.com/courses/7039/assignments/269767?module_item_id=648815

Concern

When following links in this lesson to APIs which were previous free and open source, the user now finds that they require an account, api key, and authentication, which is beyond the scope of this lesson and module.

Additional Context

No response

Suggested Changes

As it appears that much of this lesson is built around the data that was previously easily accessible from this API, and now at least requires authentication (if not a fee), it seems that much of this lesson will need to be reworked around a new, free API.

API Changed and Dataset Appears to Not be Available

Canvas Link

https://learning.flatironschool.com/courses/6560/assignments/243036?module_item_id=571687

Concern

When I opened the link like it says, I got an error.

// 20240102164658
// https://data.cityofnewyork.us/resource/uvks-tn5n.json

{
"error": true,
"message": "You must be logged in to access this resource"
}

It appears the dataset is no longer available. A TC made an account and still had trouble accessing it or could not figure it out quickly. Copying and pasting solution code, then submitting that to CodeGrade let the placeholder test pass. So we need a different API. Probably something like Weather API for instance that is not going to go away.

Additional Context

No response

Suggested Changes

Use a different API something like Weather API for instance.

Update Inaccessible API URL in README

Canvas Link

https://my.learn.co/courses/654/assignments/33307?module_item_id=83831

Concern

The URL for the API endpoint provided in the README file on line 90 (http://data.cityofnewyork.us/resource/uvks-tn5n.json) is currently inaccessible.

Additional Context

No response

Suggested Changes

I suggest updating the URL to an archived version of the same resource available on the Wayback Machine. The URL is: https://web.archive.org/web/20180303155827/https://data.cityofnewyork.us/resource/uvks-tn5n.json

Update in text needed

Canvas Link

https://my.learn.co/courses/654/assignments/33307?module_item_id=83831

Concern

After posting the code sample, the text reads:
"We stored our API endpoint URL in a constant at the top of our class.
Then, we have a get_programs method that uses the requests library to send an HTTP request from our program. "
But the URL is not stored as a constant at the top of the class, it is stored in the get_programs() method.

Additional Context

I know this seems like a small detail, but many small errors throughout the lessons makes me question the accuracy of what I’m learning.

Suggested Changes

I believe the text should read something like:

"We define a get_programs() method which assigns our API endpoint to a variable name URL. Then we submit a request with that URL using the request library get() method. We record the response in a variable aptly named - response, and return the content of the response.”

Hope that helps. Thanks

Does parsed json become a hash? or an object

Canvas Link

https://my.learn.co/courses/654/assignments/33307?module_item_id=83831

Concern

Conclusion
To recap: APIs generally either provide a user with data or added functionality.
We can use APIs that serve data to get information for our own applications and projects.
To get this data, we need to send a request to the URL of the API and know how to work with the response we receive. Many APIs serve data in JSON format, which needs to be parsed before we can use it. Once parsed, it becomes a hash we can work with and extract data from.

Additional Context

This conclusion says of json " Once parsed, it becomes a hash we can work with" However, so far, I can only find references that say parsed json becomes an object, but none that say it is hashed.

Suggested Changes

I'm not certain on this, but please check this for accuracy and if it should say about parsed json, "it becomes an object" (not a hash) please make the change.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.