How to use apis with python

How To Use Web APIs in Python 3

An API, or Application Program Interface, enables developers to integrate one app with another. They expose some of a program’s inner workings in a limited way.

You can use APIs to get information from other programs or to automate things you normally do in your web browser. Sometimes you can use APIs to do things you just can’t do any other way. A surprising number of web properties offer web-based APIs alongside the more familiar website or mobile app, including Twitter, Facebook, GitHub, and DigitalOcean.

If you’ve worked your way through some tutorials on how to code in Python 3, and you’re comfortable with Python’s syntax, structure, and some built-in functions, you can write Python programs that take advantage of your favorite APIs.

In this guide, you will learn how to use Python with the DigitalOcean API to retrieve information about your DigitalOcean account. Then we’ll look at how you can apply what you’ve learned to GitHub’s API.

When you’re finished, you’ll understand the concepts common across web APIs, and you’ll have a step-by-step process and working code samples that you can use to try out APIs from other services.

Prerequisites

Before you begin this guide you’ll need the following:

  • A local development environment for Python 3. You can follow How To Install and Set Up a Local Programming Environment for Python 3 to configure everything you need.
  • A text editor you are comfortable using. If you don’t already have a favorite, choose one with syntax highlighting. Notepad++ for Windows, BBEdit for macOS, and Sublime Text or Atom for any platform are all good choices.
  • A DigitalOcean account and API key. The first few paragraphs in How To Use the DigitalOcean API v2 show how to do this.
Читайте также:  Php множественный запрос mysql

Step 1 — Getting Familiar with an API

The first step in using a new API is to find the documentation, and get your bearings. The DigitalOcean API documentation starts at https://developers.digitalocean.com/. To find APIs for other services, search for the name of the site and “API” — not all services promote them on their front pages.

Some services have API wrappers. An API wrapper is code that you install on your system to make the APIs easier to use in your chosen programming language. This guide doesn’t use any wrappers because they hide much of the inner workings of the APIs, and often don’t expose everything the API can do. Wrappers can be great when you want to get something done quickly, but having a solid understanding of what the APIs themselves can do will help you decide if the wrappers make sense for your goals.

First, look at the DigitalOcean API Introduction at https://developers.digitalocean.com/documentation/v2/ and try to understand only the basics about how to send a request, and what to expect in the response. At this point, you’re trying to learn only three things:

  1. What does a request look like? Are they all just URLs? For more detailed requests, how is the data formatted? It’s usually JSON or querystring parameters like a web browser uses, but some use XML or a custom format.
  2. What does a response look like? The API documents will show sample requests and responses. Are you going to get JSON, XML, or some other kind of response?
  3. What goes into the request or response headers? Often, the request headers include your authentication token, and the response headers provide current information about your use of the service, such as how close you are to a rate limit.

The DigitalOcean API uses HTTP methods (sometimes called verbs) to indicate whether you’re trying to read existing information, create new information, or delete something. This part of the documentation explains what methods are used, and for what purposes. Generally, a GET request is simpler than a POST, but by the time you’re done here, you won’t notice much difference.

The next section of the API documentation discusses how the server will respond to your requests. In general, a request either succeeds or it fails. When it fails, the cause is either something bad with the request, or a problem on the server. All of this information is communicated using HTTP status codes, which are 3-digit numbers divided into categories.

  • The 200 series means “success” — your request was valid, and the response is what logically follows from it.
  • The 400 series means “bad request” — something was wrong with the request, so the server did not process it as you wanted it to. Common causes for HTTP 400 -level errors are badly-formatted requests and authentication problems.
  • The 500 series means “server error” — your request may have been OK, but the server couldn’t give you a good response right now for reasons out of your control. These should be rare, but you need to be aware of the possibility so you can handle them in your code.

Your code should always check the HTTP status code for any response before trying to do anything with it. If you don’t do this, you’ll find yourself wasting time troubleshooting with incomplete information.

Now that you have a general idea of how to send a request, and what to look for in the response, it’s time to send that first request.

Step 2 — Getting Information from the Web API

Your DigitalOcean account includes some administrative information that you may not have seen in the Web UI. An API can give you a different view of familiar information. Just seeing this alternate view can sometimes spark ideas about what you might want to do with an API, or reveal services and options you didn’t know about.

Let’s start by creating a project for our scripts. Create a new directory for the project called apis :

Then navigate into this new directory:

Create a new virtualenv for this project:

Источник

Оцените статью