Skip to content
Home » University of Michigan » Python for Everybody Specialization » Using Python to Access Web Data » Week 6: JSON and the REST Architecture (Chapter 13)

Week 6: JSON and the REST Architecture (Chapter 13)

In this module, we work with Application Program Interfaces / Web Services using the JavaScript Object Notation (JSON) data format.


Lectures


Video: 13.5 – JavaScript Object Notation (JSON)

Summary of JSON Introduction

Background:

  • XML previously dominated data serialization, but its complexity led to a desire for simpler alternatives.
  • JSON arose from JavaScript object syntax, offering a lightweight and human-readable data format.

Comparison with XML:

  • JSON uses key-value pairs like dictionaries, similar to XML elements and attributes.
  • Less overhead: no tags, attributes, or text areas like XML.
  • Simpler parsing and representation within programming languages.

Python Example:

  • Loads JSON data into a Python dictionary.
  • Access nested data structures using dictionary key syntax (e.g., info["name"]).
  • Lists are also supported and accessed similarly.

Popularity and Usage:

  • Increasingly popular due to simplicity and direct mapping to programming language structures.
  • Considered easier to use than XML, especially for simpler data.
  • Preferred choice for many modern web-based applications due to JavaScript prevalence.

Overall:

  • JSON offers a convenient and user-friendly solution for data serialization, particularly for straightforward data structures.
  • While less rich than XML, its simplicity and ease of use make it a popular choice for many modern development scenarios.

Demystifying JSON: A Beginner’s Guide to the Lightweight Data Format

Welcome to the world of JSON (JavaScript Object Notation), a ubiquitous data format rapidly conquering the web! This tutorial delves into the core concepts of JSON, empowering you to understand, work with, and harness its power for your projects.

What is JSON?

Imagine data that’s lightweight, human-readable, and easily understood by various programming languages. That’s JSON! Derived from JavaScript, it uses key-value pairs and nested structures like dictionaries and lists to represent data in a clean and organized manner. Unlike XML, it boasts simplicity without sacrificing essential functionality.

Why Use JSON?

  • Simplicity: It’s easy to learn and write, reducing development time and complexity.
  • Readability: Its human-readable nature allows for clear understanding, even without prior knowledge.
  • Interoperability: Works seamlessly with diverse programming languages, facilitating data exchange between systems.
  • Efficiency: Lightweight structure minimizes transmission size, ideal for web applications and APIs.

Exploring the Structure:

JSON data resembles a dictionary or tree, with key-value pairs forming the foundation. Keys are strings, while values can be strings, numbers, booleans, arrays (lists), or even nested objects.

Example Breakdown:

JSON

{
  "name": "John Doe",
  "age": 30,
  "address": {
    "street": "123 Main Street",
    "city": "New York"
  },
  "hobbies": ["coding", "music", "reading"]
}

Here, “name”, “age”, “address”, and “hobbies” are keys. Their values reveal details about John. The “address” is another object nested within the main one, holding further information.

Working with JSON:

  • Accessing Data: Use key names to retrieve specific values. For example, data["name"] would return “John Doe”.
  • Iterating Over Lists: Utilize loops to access elements within an array.
  • Parsing and Processing: Programming languages provide libraries to convert JSON strings into usable data structures.

Popular Use Cases:

  • Web APIs: Exchanging data between front-end and back-end applications.
  • Configuration Files: Storing settings and preferences for applications.
  • Data Interchange: Sending information between different systems and services.

Learning Resources:

  • Interactive Tutorials: Explore online platforms like JSONLint or JSONFormatter to experiment and visualize JSON data.
  • Documentation: Dive deeper with official resources like the JSON.org website for detailed specifications and examples.
  • Online Courses: Numerous platforms offer free or paid courses to guide you through advanced JSON concepts and applications.

Ready to Start?

With its straightforward nature and vast applications, JSON opens doors to exciting possibilities. So grab your favorite text editor, explore online resources, and embark on your JSON journey! Remember, practice makes perfect, and soon you’ll be effortlessly working with this versatile data format, building efficient and interconnected systems.

import json

data = '''
[
  { "id" : "001",
    "x" : "2",
    "name" : "Chuck"
  } ,
  { "id" : "009",
    "x" : "7",
    "name" : "Chuck"
  }
]'''

info = json.loads(data)
print('User count:', len(info))

for item in info:
    print('Name', item['name'])
    print('Id', item['id'])
    print('Attribute', item['x'])

# Code: http://www.py4e.com/code3/json2.py
# Or select Download from this trinket's left-hand menu
User count: 2
Name Chuck
Id 001
Attribute 2
Name Chuck
Id 009
Attribute 7

So now we’re going to talk about a
new serialization format. We’ve talked about XML, which is
kind of complex. And there’s a simple one is JSON and it’s
increasingly popular to the point where it’s entirely possible you might go a
whole career and not really run into XML. XML is not necessarily bad, it’s just very powerful and more
powerful than we need in simple situations. And so here’s one of the videos that I
that I’ll show you. It’s a video of a fellow named Douglas Crockford
and he’s credited with discovering JSON. JSON really is not a new format. JSON was invented by the constant syntax, the literal notation for constants in
JavaScript for both arrays of variable constants, values,
and objects. And that’s what it is. And so he makes it
really clear that he just noticed that this could be a way
for us to serialize data. So when I talked about serialization, I talked about how we’re not going to
use the Python syntax, we’re not going to use the Java syntax, we’re going to use XML in the middle. But what happens in JSON is we’re not
going use the Python syntax, we’re not going use the Java syntax, but we are going to use the JavaScript
syntax in the middle. So JSON is very native to JavaScript. And interestingly, because so many things
are being built in browsers these days, JavaScript is becoming an increasingly
interesting language. And so JSON’s popularity is not driven
so much because of JavaScript, but it certainly doesn’t hurt to have
JavaScript become increasingly increasingly popular and JSON
coming along with it. So Douglas Crockford is an interesting fellow. He’s got sort of a dry sense of humor and I encourage you to watch the interview
that I did with Douglas Crockford. As part of the dry sense of humor, you can see two books here written
from O’Reilly. One is the JavaScript Definitive Guide and then there is another book
that’s almost a joke, which is I mean, it’s a very good book called
JavaScript: The Good Parts. And the thickness of the parts of JavaScript
that are there that are not the good parts, as Crockford would say, and so
this is a Crockford book. And so this is just kind of Crockford’s
wry sense of humor that, about what he thinks about
JavaScript. Now Crockford would say, and I
would agree, that the good parts of JavaScript make it a
really great programming language, you just have to be careful not to do the stuff that he considers the
“not such good parts” of JavaScript. And so Crockford basically established
JSON as a standard sort of by fiat, he didn’t ask anybody’s permission and he
registered json.org and put a couple of documents up that
said this is what JSON is. And it’s kind of taken the world by storm. The last few years, like I say, because of so much data is being moved
between the browsers and back ends and JSON is pretty much the serialization
format that’s being used there. XML is still used in some situations. And so here is the similar kind of problem that we solved except
we’re going to use JSON now. And so just like in all of Python,
we import something. We import the JSON library. We’re going to use the triple-quoted
string again. And again, we’re not going to pull this data. Normally, we’d pull this data from
the Internet with urllib, or open it into a file, or whatever. And it starts with a curly brace. We’ll see another syntax where it starts
with a square brace. Curly brace is like a dictionary, right? And it’s kind of like a dictionary. In JavaScript, it’s called an object, but it’s very much like a Python dictionary
in that we have key-value pairs, there is a series of keys, a key followed by a colon; the keys are strings, the values are
can be anything, not just a string – phone. And now here, we have a dictionary
within a dictionary. So this is the phone keyword in the
outer one, phone, email. and name are in the outer one. And then there’s a child dictionary
here that starts from here to here and it has two things, one is type and one is number. And then this one has a child that has
hide as yes. And you could think of this as a tree as well, if you really wanted to. You know, we could have this outer thing
which has a name, a phone. and email. And under phone, we have type and number. Sorry, I have to make these long. And then we have hide. Right? Now, one of the things that’s different between JSON is there’s no start tag and there’s
no end tag and there’s no attributes. And so this I’m kind of cheating and making
this attribute and moving it down. And there’s no text area. So it’s got some different things that are
sort of different nature than XML. But it has some things that we absolutely love. And that is. so we have this blob of text that we got from somewhere and we’re
going to pass it in. This loads stands for load from string. So use the JSON library method or
function loads from the JSON library and that does all the
parsing and looks all these things up. This might blow up, it might give you
traceback if there’s a syntax error, like you forgot that quote or something
like, so you get a traceback here. Or not, assuming it all works well. And in this particular example, it’s
going to work well. We get back an object that sort of represents, internally, all of this data. Now the interesting thing is the thing
we get back is really a Python dictionary. So this ends up being a type Python dictionary as if you
built this Python dictionary with name, phone, and email as keywords and then
the values are Chuck. And then another dictionary and
another dictionary. Okay. And so info is a dictionary. So when we talk to this dictionary, we just use the brackets syntax sub name. If we print that out, if we do
info sub name, it is this. So there’s no, like, pull out the text thing, do a get, find it. There is a dictionary and
it’s got a key named name. And that’s what you get back. If we are going to go find this hide, we go get info sub email, which is a dictionary, which is then
gives us back this thing right here, which is also a dictionary. And so then we say sub hide within that
and that pulls out the yes to print it out. So this is one of the things that
works really well with JSON, is because its structure is more like the kinds of structures that we
have of lists and dictionaries. And every programming language has
something that’s roughly equivalent to a list and
roughly equivalent to a dictionary. And so what happens is is that this JSON is
not as rich a representation as XML, but because it’s simple and it maps directly onto the internal structures that we
have in all these languages, it’s a lot easier to use. And it’s not as rich, but in its simplicity, it makes it so much easier to use. And so if you just compare this to the previous XML example with the
gets and the dot text and like, what am I doing here, this is a lot more direct. You look at it, you write this stuff
and you’re done with it. Here is an example of a list. So we’ve got lists and dictionaries
and they can be nested in any way you want. So what we have here is our JSON starts here and ends here. Because it’s a
square bracket, it’s a list. And so that is a list. In this case, it’s two items. And this is a dictionary. And this is a dictionary. So we end up with dictionary
and dictionary. We end up with a list of two dictionaries. And so when we take this and
run it through loads, assuming that there’s no syntax errors, we get back a info is a list. It’s not an object, it’s a list. And we can say because it’s a list, we can ask how many are there and that’ll be
there’s going to be two things in there. And then we can write a for loop
we can write a for loop that has the stuff in here and
the stuff in there. item is going to iterate through
those two things, which it’s going to iterate through here
and then it’s going to iterate through that. This is going to be a dictionary
and this is going to be a dictionary. So this loop is going to run twice,
loop’s going to run twice, once with this as item, which is a dictionary, and the next time
with that as item as a dictionary. And then inside the loop so item points
to this and we say item sub name. well, items sub name is just this Chuck. And then item sub id is 0.01. And item sub x is 2. And again, if you look back at the XML, it does the same thing. You see, it’s just a lot more complex. Right? A lot more complex. JSON is simpler, so the code we
write to work with JSON is simpler and that’s why
a lot of people like it. A lot of people would say, you know, what you should do is
if you can use JSON, you probably should use JSON, but if you have something more complex, you might want to use XML. But these days, there’s sort of
a strong movement towards JSON. And so that gives us a summary
of how JSON works. And now, we’re going to talk
move up a little bit and talk about the
Service Oriented Approach.

Video: Worked Example: JSON (Chapter 13)

Summary of Python for Everybody: Playing with Web Services

This video explains how to use JSON (JavaScript Object Notation) in Python for web services.

Key Points:

  • JSON is a simpler alternative to XML for data exchange.
  • It uses key-value pairs, similar to Python dictionaries.
  • The json library is built into Python for parsing JSON data.
  • json.loads() converts a JSON string into a Python object.
  • Lists in JSON become Python lists, and nested objects become nested dictionaries.
  • Accessing data within the parsed object uses dictionary syntax.
  • JSON is easier to read and write compared to XML.

Examples:

  • Parsing a JSON string with json.loads().
  • Accessing values within nested dictionaries using key and subkey.
  • Iterating through a list of dictionaries using a loop.

Additional Notes:

  • The video mentions syntax errors and how to identify them.
  • It emphasizes the direct access to data in JSON compared to XML.
  • Remember that JSON objects don’t have attributes like XML tags.

Python for Everybody: Playing with Web Services Tutorial

This tutorial takes you through the exciting world of web services in Python, using examples from the “Python for Everybody” course. Get ready to explore data exchange, APIs, and practical applications!

Prerequisites:

  • Basic understanding of Python programming
  • Familiarity with the “Python for Everybody” course materials (Chapter 12: Networked Programs)

What you’ll learn:

  • What web services are and their significance
  • Working with different data formats: JSON and XML
  • Accessing data from web services using Python libraries
  • Practical examples of web service applications

Let’s dive in!

1. Introduction to Web Services:

  • Web services provide a way for applications to communicate and exchange data over the internet.
  • They use standardized protocols like HTTP and XML/JSON for data exchange.
  • Benefits include platform independence, reusability, and efficient data access.

2. Exploring JSON:

  • JSON (JavaScript Object Notation) is a popular data format for web services due to its simplicity and human-readability.
  • It resembles Python dictionaries with key-value pairs nested within objects.
  • We’ll use the json library to load, parse, and access data from JSON strings.

3. Example: Processing JSON Data:

  • Let’s work with the json1.py example from the course.
  • This code loads a JSON string representing user information and extracts specific details like name and email.
  • We’ll explore how to navigate nested structures and efficiently retrieve data using key references.

4. Understanding XML:

  • XML (Extensible Markup Language) is another data format used in web services, offering more structured data representation.
  • It utilizes tags and attributes to define data elements and their relationships.
  • While less common than JSON today, understanding XML basics can be helpful.

5. Example: Working with XML APIs:

  • APIs (Application Programming Interfaces) provide programmatic access to web service functionalities.
  • We’ll explore accessing data from an XML-based weather API using the urllib library.
  • The code will fetch weather information like temperature and forecast for a specific location.

6. Beyond the Basics:

  • Explore more advanced web service APIs and libraries like requests for improved functionality and convenience.
  • Consider real-world applications like fetching news feeds, interacting with social media platforms, or using machine learning APIs.

Further Resources:

Remember:

  • Practice is key! Experiment with different APIs and data formats to solidify your understanding.
  • Explore error handling and exception management for robust web service interactions.
  • Stay updated with the evolving landscape of web technologies and APIs.

By following this tutorial and exploring further resources, you’ll gain a solid foundation in using web services with Python. Remember, the journey of learning is continuous, so keep exploring and have fun!

[MUSIC] Hello everybody,
welcome to Python for Everybody. We’re playing with the web
services chapter right now. And if you want to get the materials for
this course, you can go here and
download the sample zip, samplecode.zip. I’ve got this all sitting
already on my computer. I also have the whole thing in GitHub
if you want to get it out of GitHub. So the thing we’re talking about now
is talking about the json1.py example from the book. So JSON is kind of like XML
except a lot simpler and that’s why a lot of people like it. It’s not that JSON is always better, but JSON is better in a lot of situations
that don’t require the complexity of XML. So we start to import JSON,
JSON is built into Python, but we have to ask to import it. Again, we’re using a triple-coded
string to put the JSON in there. And JSON looks a lot like Python
dictionaries, key value pairs. Key value pairs, in this case,
this is a key and the value itself is another dictionary,
or in JSON terms, an object. But again, key value pairs, within key
pairs values, within key value pairs. And all these little
cursor guides have to, all these little curly brace
guids have to line up properly. And so like all the time, this is a string which we normally would
read and decode from the Internet. But for now, we’re just going to
have it in there load json.loads. That’s going to the JSON library,
pull out load string. And parse this,
which turns this set of curly braces, spaces, commas, and perhaps syntax errors,
into a structured object. And if we had made a syntax error in here,
then this would blow up. But if this doesn’t make a syntax error,
if this doesn’t blow up, then we have a structured representation. Now, the difference between XML and
JSON is that this turns into a Python dictionary,
with key value pairs, okay? And so once we have this, this is
a dictionary And we can say info sub name. And that’s the exact syntax that we
would use to get the dictionary. And that’s going to extract
this value out of there. And if we want to go in deeper,
we can say info sub email and that is what info sub
email is right there. And then sub hide, so that’s
a dictionary within a dictionary and so if we run this, python3 json1.py it digs in really fast. And so this is why people
tend to like JSON is because you read the JSON which is actually
a syntax derived from JavaScript, when it looked just like the syntax for
Python. So, that’s moving an object, a JSON object that turns directly into a
Python dictionary with nested dictionary. So now we’re going to look at json2 and
so json2 we’re going to see a list. An array and JSON terms but
it turns into a list of Python terms. So this is a list of dictionaries, in JavaScript that would
be an array of objects. But in Python it’s a list of dictionaries. So we’ll just pretend that
it’s a list of dictionaries. Again, we load the string, parsing,
looking for syntax errors. So let’s just make a syntax error here and
run our python json2.py. And you’ll see where it blows up. It blows up at line 15, which is right
here, it’s like this loads blows up. Now, you could put a try accept around it
to save it but we’re not going to do that. And it even complains it says, look we’re
expecting something here In the line 11, its the line 11 of the JSON
which starts at line 4. And so I put my little square brace
back in so its not syntactically broken. So, let’s run it again and
make sure that she runs and yes she does. So, this parses and converts from the JSON
syntax into a python in this case list. Because it’s got square braces
instead of curly braces. The previous example had square braces and
we can then take a line of it and it’s an array, it’s a list, and
we see that there are two things in there. And then we’re going to iterate through
and this item is going to iterate through these dictionaries, that dictionary
followed by that dictionary. So the first time, it’s item sub name, which is this value right here, and
then item sub id, which is this value. So you can dig right into this,
but you’re not using get, and you’re not using the weird extra find,
or find all, or anything you just are going
at these structures directly. And so you can quickly extract this
stuff out and we read through id’s, name is Chuck, oops, name is Chuck. There are no attributes, by the way,
x is 2, and so we had to make x. So if you look at the XML, we had this
concept of attributes on the outer tag. These things are also not named, we just
have to know what we’re looking for. So JSON represents simple structures,
but it’s much simpler to use. So I hope this has been useful to you and talked to you in a bit
about some more JSON. [MUSIC]

Video: Interview: Douglas Crockford – Discovering JSON

The speaker of this passage describes his discovery and development of JSON (JavaScript Object Notation), a popular data format used for data interchange. Here are the key points:

Discovery:

  • The speaker claims to have discovered JSON in 2001, although similar ideas existed earlier.
  • He recognized its potential for simplifying data communication between programs.

Development:

  • He initially used JSON for communication between JavaScript and Java programs.
  • He aimed to make JSON language-independent and simplified its structure for efficient data transfer.
  • He standardized JSON by creating json.org and promoting its adoption.

Benefits of JSON:

  • Easier to use and understand compared to XML.
  • Data structures directly match how programming languages represent data.
  • Efficient for data exchange, especially in AJAX applications.

Key Design Decision:

  • No versioning ensures stability and prevents breaking changes.
  • JSON acts as a foundational layer for building more complex data structures.

Future of JSON:

  • It might be replaced if its limitations become significant.
  • Existing JSON applications will still work due to its unchanging nature.

[MUSIC] [MUSIC] [MUSIC] So JSON is the world’s best-loved data
interchange format. It, I discovered it in 2001. I don’t claim to have invented it, because
it already existed in nature. I just saw it and recognized the value
of it, gave it a name and a description, and
showed its benefits. But I did not invent it. I don’t claim to be the first person to
have discovered it. There were other people who I later found
out had come with, along the same idea in 2000. The earliest instance I found of
JavaScript being used as a data interchange format
was at Netscape in 1996, so it’s a, an idea
that’s been around for a while. And if you look at other data
representations like the property lists that were used at, at NeXT and then
later at Apple, except for a couple of cosmetic changes,
it’s the JSON notation as well. So it seems like it’s an inevitable sort
of representation for data, at least data that is intended to be
consumed by programming languages. And ultimately, that’s all data. I started with JavaScript. But my first application was facilitating
communication between programs written in JavaScript and servers
written in Java. So I recognized that even though it was
born out of JavaScript, it could be and should be
language independent. So I simplified it as much as possible,
took as much out, tried to make the simplest possible specification for how to
structure data and put it on the wire. And that ultimately became called JSON. [MUSIC] In 2001, I was in a company I had started
called State Software. And we’d developed a platform for doing
applications which could be delivered through unmodified web
browsers, what today is called AJAX. But in 2001 that was kind of a radical idea,
and not many people would believe that was even possible or, if it
were, that was a good idea. But we produced some brilliant
demonstrations and we were starting to make some progress in trying to convince, you know, potential
customers that they should adopt the style of application
and development. And as part of the description, we’d say
and then we use this JSON idea for communicating the
stuff back and forth. And they’d say JSON, what’s that? And we’d say it’s this thing we found in
JavaScript and it’s really great. And they’d say oh, we can’t use that, we
just committed to XML. So, no we can’t. I’d say, but XML was wrong for all of
these reasons, it’s, it’s hugely expensive, it’s much harder to
use, and all of that. Well, we can’t use that thing you did
because it’s not a standard. I said, it is a standard, it’s a proper
subset of ECMA-262, which is a standard. They said no, that’s not a standard. So I decided if I want to be able to use
this thing, I need to make it a standard. So I bought json.org, and put up a web page, and sort of declared it’s a
standard. That’s it, that’s all I did. I didn’t go around trying to convince
industry and government and, and everybody that this is what they
should do. I just put up a website. Basically, a one-page website. And over the years, people discovered it
and realized, oh yeah, this is so much easier. I’m just going to do that. [MUSIC] The thing I never understood about XML for
data interchange, ok, so basic, generally the pattern is,
you’ve got a query. You send it to the server. It gives it to the database. And you get back this XML thing. Then you have to send queries to that in
order to get the data out of it. And I said, why can’t you just give it to
me in a form where I know what it is and I can use it immediately, and so that was the main benefit of JSON, I think. It wasn’t that curly braces are so much
better than angle brackets. I mean, ultimately none of that matters. The thing that mattered was that the data
structures that JSON likes to represent are exactly the same data structures that programming languages
represent. You know, when AJAX was formulated, the X
in AJAX was supposed to be for XML. And the smart kids right away realized, oh
this is too hard. We, we don’t want to be doing XML here. And some of them discovered, hey, you can
use JSON here instead and it’s so much easier. So much faster. So they started doing that. And for a while there was a debate, you
know, where some people were arguing Jesse James Garrett said the X stands for
XML, so you can’t use anything but XML. That didn’t last very long. There were a number of other alternatives
to XML that were being considered around those times, but JSON was the only one
that was designed specifically for AJAX. The, probably the boldest design decision
I made in designing JSON was not to put a version number on it, so there is no
mechanism for revising it. So, JSON, we’re stuck with it. Whatever it is in its current form,
that’s it. And that turns out to be its best feature. And because it wants to be a low-level
thing and it’s, it’s basic infrastructure and it’s the
thing that you pile everything else on. You know, it’s sort of the equivalent of
alphabet in a language. I mean, we might make up lots of words and
lots of ways of having sentences, but it’s very uncommon to make up new
letters. And that’s sort of the place where JSON
lives. So it’s good that it’s not going to
change. I, I expect maybe someday we’ll find that
there are really important things that JSON doesn’t do, like cyclical
structures. Graphs are not easily represented in
JSON. They can be, but it requires a level of
indirection, a little bit more work. Some day we might decide we don’t want to do that work and then we replace JSON with
something else. We will not extend JSON to do that, we’ll
replace JSON. And even after we do that replacement,
everything that was ever developed that still uses JSON will still work,
because JSON will never change. [MUSIC] [MUSIC]

Video: 13.6 – Service Oriented Approach

Summary of Service-Oriented Architecture:

  • Evolution: Started with data exchange, progressed to structured contracts and formats, leading to service-oriented applications.
  • Concept: Applications built from smaller, independent services offering specific functionalities.
  • Example: Airline websites integrating with car and hotel services through web services (e.g., sending search requests and receiving data).
  • Benefits:
    • Modular development and deployment.
    • Reusability of services across applications.
    • Potential for revenue generation through service integrations (e.g., commissions).
  • Challenges:
    • Requires complex code to connect and parse data from different services.
    • Relies on well-defined APIs and protocols for smooth integration.
  • Overall: Service-oriented architecture enables building complex systems by integrating independent services, but requires careful design and implementation.

Service-Oriented Architecture (SOA) Tutorial: Building Modular Applications

Welcome to the world of SOA! This tutorial will introduce you to the key concepts and guide you through the steps of building your own service-oriented architecture.

What is SOA?

Imagine building a house not brick by brick, but by assembling pre-built modules like kitchens, bathrooms, and bedrooms. SOA works similarly with software applications. Instead of monolithic applications, you create independent, reusable services that communicate with each other. This modular approach offers several advantages:

  • Flexibility: Easily adapt and scale your applications by adding or modifying services.
  • Reusability: Share services across different applications, saving time and development effort.
  • Loose Coupling: Changes in one service don’t impact others, making maintenance easier.

Key Ingredients of SOA:

  • Services: Independent units of functionality offering a well-defined interface.
  • APIs: (Application Programming Interfaces) Define how services interact with each other, like contracts.
  • Web Services: Technologies like SOAP and REST enable communication between services over the internet.
  • Service Registry: Catalogues available services and their descriptions.
  • Service Bus: Manages communication between services, routing messages and ensuring reliability.

Building Your First SOA Application:

  1. Identify Services: Break down your application’s functionality into independent services.
  2. Design APIs: Define clear and concise interfaces for each service, specifying inputs and outputs.
  3. Implement Services: Develop each service using your preferred programming language and technology.
  4. Deploy Services: Make your services accessible on a network, often using web servers.
  5. Register Services: Add service descriptions to the service registry for easy discovery.
  6. Develop Client Applications: Create applications that consume services through their APIs.
  7. Test and Monitor: Thoroughly test your services and monitor their performance for stability.

Example Scenario:

Imagine an e-commerce website. Services could include:

  • Product Catalog: Manages product information and search functionality.
  • Shopping Cart: Handles adding and removing items from the cart.
  • Payment Processing: Integrates with secure payment gateways.
  • Order Management: Processes orders, including inventory checks and shipping.

Each service exposes its functionality through APIs. The website application interacts with these services to provide a seamless user experience.

Learning Resources:

Remember: SOA is a vast topic, and this is just a starting point. Experiment, explore, and build your own modular software empires!

Okay, so we started
by just moving data back and forth, and now we’re starting
to talk about fancy syntaxes, and contracts, and
different serialization formats. And where we’re ultimately going is,
you end up with these applications that provide
a service to the rest of an application. So, in a way you would start building
applications up by parts and pieces. And probably the best example of this is if you go to an airline web site
and you’re booking an airplane, as soon as you’re done they’ll say,
do you want a car, or do you want a hotel? And you think, well how come the airline,
has cars and hotels, and the answer is they don’t. They just have connections behind
the scenes using web services, to car and hotel systems. And if you start searching, they take your
search, and send it to the hotel system, and get a bunch of JSON or XML data back,
and show it to you like, whoa, the airline knows something about cars,
and it knows how much the cars cost at this city, one of 10,000 cities
around the world or whatever. And so, what happens now is this is called
a service-oriented architecture or, a service-oriented approach, where
basically, you’re the hotel reservation, and you have a web site, but
you also give out a service, and then other systems can sort
of work with your service. And you’d be surprised,
there’s money going back and forth, and so if you’re booking hotels
from an airline site, the airline gets a little kickback for
bringing a customer to the hotel. because the hotels want customers,
and if the airline brings hotels, but then there’s this service, and
then somebody has to, the airline company person has to write some code that reads
the data from the car rental company, and back and forth, and so
everyone has to figure all this stuff out. And we talk about these
things in the form of APIs, application program interfaces,
which are ways to use web protocols to access data on systems, using
well-defined and structured approaches. And so the idea is, we start with
two systems, and we’re just like, okay, we have this problem,
we’re going to break it into two, we’re going to write
a protocol between these. And then what happens is, it goes from two
systems, to like five system, to whatever. And then you end up with these systems
that will end up with lives of their own, and the overall system
that we see as end users are the integration across
many of these systems. And I’ve got a video
that I’ll show you next, that shows how this evolution happens.

Video: Service Oriented Architectures

Service-Oriented Approach: Transforming Data Sharing

Traditional data sharing between applications:

  • Works for simple systems.
  • Becomes challenging and time-consuming with more applications.
  • Leads to data silos and duplication.

Service-oriented approach:

  • Introduces a service layer for data sharing.
  • Works with existing systems, no need for replacements.
  • Enables easy data reuse and integration of new applications.
  • Data becomes a service consumable by any application.

Benefits:

  • Improved data sharing within organizations and even across organizations.
  • Enables better decision-making and resource allocation.
  • Promotes standardization and collaboration (e.g., XCRI project).
  • Facilitates virtual research collaborations (e.g., MyGrid project).

Resources:

  • e-Framework website provides guidance and tools for implementing service-oriented approach in education and research.

Overall:

Service-oriented approach offers a scalable and efficient way to share data and enable new functionalities across diverse applications and even organizations.

Service-Oriented Approach: Transforming Data Sharing – A Tutorial

Data sharing is crucial for modern organizations, but traditional methods often face challenges with rigidity, duplication, and inefficiency. Here’s where the Service-Oriented Approach (SOA) steps in, offering a transformative solution.

What is SOA?

Imagine a waiter taking your order in a restaurant. They don’t cook the food themselves, but rather act as a service layer connecting you (the customer) with the kitchen (the data source). Similarly, SOA introduces a service layer between systems and applications, enabling them to interact and share data seamlessly.

Benefits of SOA for Data Sharing:

  • Flexibility: Services are independent and loosely coupled, allowing easy addition, removal, or modification without impacting other systems.
  • Standardization: Data is shared in consistent formats, eliminating duplication and simplifying integration.
  • Reusability: Services can be used by multiple applications, reducing development time and effort.
  • Agility: SOA fosters a dynamic environment, adapting to changing data needs and business requirements.
  • Security: Access control and governance are implemented at the service layer, enhancing data security.

Getting Started with SOA for Data Sharing:

  1. Define your data and services: Identify the data you want to share and the functionalities different applications need.
  2. Design your service layer: Choose appropriate technologies and protocols (e.g., APIs, web services) for service implementation.
  3. Develop and deploy services: Build and test services according to defined standards and security protocols.
  4. Integrate applications: Configure applications to connect and consume services through the service layer.
  5. Monitor and govern: Continuously monitor performance, security, and access control of services and data.

Tools and Technologies:

  • API Management Platforms: Manage APIs, enforce access control, and track usage. (e.g., Apigee, MuleSoft)
  • Integration Platforms: Facilitate data transformation and message routing between services. (e.g., Boomi, Mulesoft)
  • Data Governance Tools: Manage data quality, security, and compliance across services. (e.g., Collibra, Informatica)

Remember:

  • SOA is an iterative process. Adapt and refine your approach based on your specific needs and challenges.
  • Security is paramount. Implement robust access control and encryption mechanisms.
  • Data governance is essential. Ensure data quality, consistency, and compliance across services.

By embracing SOA, you can unlock the true potential of data sharing, fostering collaboration, agility, and data-driven decision-making within your organization.

Additional Resources:

[MUSIC] The service-oriented approach
to linking software systems and applications is transforming the way
many organizations share data. This approach improves existing
methods of data sharing, by providing a service
layer between systems. Sharing data between applications
is a well-established principle that works on a simple level. As systems expand, the solution that
connects two software applications will often not work for a third. What began as a workable system,
then locks valuable data in a silo. Sharing the data is still possible,
but requires adaptations and tweaks. The process is laborious and time consuming,
leading to bottlenecks and overload. The reality of this world is unnecessary
effort expended in duplication of data. [MUSIC] By applying a service layer to
the applications you wish to connect up, data is offered up in a common format for
reuse elsewhere. The service-oriented approach works
with existing software systems and does not require you to remove
the monolithic application. Replacement applications reuse
the links already made and can plug into the service layer
without affecting other users of the data. New applications can be added to
grow overall system architectures in the same way. The data in each application, is offered up as a service which
any other application can consume. When the service-oriented approach is
evolved into a system-wide architecture, it allows connections and service sharing opportunities between
organizations nationally and globally. [MUSIC] Take monitoring of student progress for
example. Results are logged by a virtual learning
environment, but how much of that data is shared with a management system that is
tracking progression against funding? If the motivation and commitment of
struggling students is not addressed and they decide to walk away,
there is a direct impact on funding. If the data is freed up and shared across a system using
a service-oriented approach, it could contribute to retaining student
motivation, and thereby maximizing income. [MUSIC] And what if across the education
community, there was one standard and agreed way to describe a course? It would allow course information to be
made available as a consumable service, both internally and externally. This is the subject of a current project
called Exchanging Course Related Information or XCRI,
encompassing course marketing, quality assurance, enrollment, and
reporting requirements. This service-oriented approach
enables aggregated organizations, like UCAS, to gather the standard and agreed descriptions of
courses from each provider. Comprehensive details of approved
enrollment opportunities, are then returned to a prospective
student for easy and accurate comparison. [MUSIC] Amongst the research community,
it is the same principle. Here, the service-oriented approach
is enabling researchers to work across institutions
in virtual organizations. MyGrid is a project that provides a shared
toolkit for creating experiments by coordinating the information flow between
distributed resources and services. Workflows and query specifications
link together remote and local resources using
web service protocols. Workflows created by myGrid
represent the scientific process of the experiments they enact,
making them a rich resource for scientists creating their
own new experiments. The service layer to this data
enables users to share, re-use, and re-purpose experiments
within the myGrid community. [MUSIC] Many education and research services
are already developing a service-oriented approach taking their lead
from the business world and major software developers. It is in this context that
the e-Framework, a successful and expanding international initiative,
works to make sense of the service-oriented approach for
the education and research community. The e-Framework website identifies and
describes the service components needed. It provides the blueprints and
reference example for those in the community who want to
gain a greater understanding and knowledge of how to implement
a service-oriented approach. The e-Framework website is thus becoming
a knowledge base that is developing a shared vocabulary and way of thinking
for all in education and research. [MUSIC]

Video: 13.7 – Using Application Programming Interfaces

This passage discusses how to use an API (Application Programming Interface) called Google Geocoding API to convert addresses into GPS coordinates. Here’s a summary:

Key points:

  • APIs provide access to services offered by other applications.
  • Google Geocoding API takes an address and returns its GPS coordinates.
  • To use the API, you need to understand its documentation and send requests in a specific format.
  • The code snippet demonstrates how to send a request, receive JSON response, and extract relevant data.
  • APIs can have complex data structures, requiring careful navigation to extract information.

Additional notes:

  • The passage compares APIs to XML data exchange, highlighting the complexity of XML parsing.
  • It mentions a future topic: Twitter API, which is said to be more complex than Google Geocoding API.

So now we’re going to sort of change
our perspective a little bit. We’ve been looking at sort of both
sides of this data interchange, and now we’re going to look from sort
of the producer and the consumer. And we’re going to be
the consumer of this. There’s this thing called an application
programming interface or API. And if you’re going to work and
talk to this application, and you want to talk to it, we’re going to write our own
application that’s going to make use of the services of another application,
we want to know what the API is for that. The API is the specification for what
the URL patterns are, what the syntax of the data we’re supposed to send, what the
syntax of data we can expect to get back. And so the implementation is who
we’re going to communicate with and we’re going to be the consumer. So I’d like to think of them
as the producer of the API is the one that has the data and
exports the API and defines the API. And then the consumer is the one that
reads the documentation of the API, writes some code, and
then complies with the rules of the API. And so here is, and it turns out
it’s more common to be the consumer of the API than to be the provider of
the API, or the producer of the API. So here’s an API that you can use, and
we will use this in a couple of little assignments, called
the Google Geocoding API. And so you’re like oh, I’ve got a
bunch of addresses. And so a long time ago we did a survey
where we took the addresses of all the people in the class and
we just let them type that in, and those addresses are imperfect data and
we could have hand-coded it. But it turns out that Google,
because Google has Google Maps, they have an API that will
take a rough approximation of an address like The Jerk Pit,
Ann Arbor, Michigan. And it will clean that up and give you
back details of what the formal name of that address, and what the exact GPS
coordinates of that address are, etc., etc., etc. And
that’s called geocoding. So Google exports that to us and there’s
lots of data, and lots of computing, and lots of research, and lots of all kinds
of things that make this geocoding API make our lives really simple because we
just have to hit it and give it some data. And so the way this works if you
were to read that documentation, is that you hit a URL,
mapsgoogleapis.com and all the stuff is in the URL
is in the documentation, and then you give it a parameter after this
question mark, address equals, and then there’s a way that you
encode these parameters. The plus is a space, Ann Arbor,
and then the %2C is a comma, and that’s another space. So this is as if you typed this into a
Google search box, Ann space Arbor, comma Michigan, that would be in the search box
and then you hit the search button, right? But we’re going to do that
search by talking to an API. And so we hit this URL and you can
actually take this particular URL and you can type it into a browser. And when you type it into a browser
you will get back some JSON, and if you look at the documentation
it will tell you what the JSON is. And we’ve got a bit of code
that you can play with, geojson.py, that has this in it. And then you have to do all the stuff,
you’ve gotta do json.loads and all the things that we’ve been talking
about in order to talk about this. And so here is a bit of code that does that. And so now we’re going to see, for
the first time, the entire application. So we’re going to use urllib and
we’re going to use JSON. And so it has this url as a string and
then it’s going to read and ask for a location with an input. And then the address is
what we type right here, so that goes into the string address. We make it so
if we hit Enter by itself it breaks, then we’re going to make a URL
by concatenating this URL. This urlencode address equals address, that’s the thing that turns this into
the plus and this into the %2C, and this into the comma, right? And so this
bit right here, that’s a concatenation. This bit right here, you don’t have
to know what those rules are for putting those parameters on,
but that’s what happens. And so this ends up being the URL,
and then we print that out so that says retrieving the URL. We do a urlopen and
then we get a handle back, remember the handle doesn’t
actually pull the data down. But if I call the read method, uh.read,
that does pull the entire document, all that text from that previous page,
curly braces, square braces, etc. And remember that we have to decode it,
because it’s probably UTF-8 out there. So we probably are going to hit Google and
they’re going to give us UTF-8 data back. And so that’s like a string that’s coming
from the outside world to the inside world, and now inside data we
have a Python Unicode string. And now we just have a string,
so it’s how much data. Now here we now have this loads, json.loads, which parses that data
that we got back from Google. It came into this variable data,
we’re going to parse it, and we’re going to get back,
well let’s take a look. We’re getting back an object. So the outer thing, that’s what I was
looking at, the outer thing is an object with curly braces, so that means it’s a
Python dictionary that we end up getting. So that means that this thing that we get,
js, is a dictionary. Now we have to have a, so there might
be a failure or there’s other things, there might be a thing, so
we’ve got to check a few things. If we’re looking for,
if the js is a false or if the status key is not in that,
that’s not in is Python, status is not one of the keys that is in the dictionary,
or the status is not equal to OK. And so one of the things you’ll
notice if you look at the specs, that status OK is part of
the response if it worked. And so that’s a way for you to know
that you didn’t get any good data, so the rest of the code
actually doesn’t blow up. So that just is sort of I’m going to
skip and print out a nasty message and print this data out. And then what I’m going to do is I’m
going to walk my way down the tree. And so it’s got a dictionary,
js sub results, and then it’s got a list, sub 0, which is the first result, and
then that is a dictionary. And then I want to go geometry,
location, and then latitude. So that’s sort of walking down the tree,
and if you look at the data it’s the results. Geometry, results sub 0 which,
because this is a list, sub 0, geometry, location, latitude. And so we basically extracted
that little number, 422008, we extracted that right here. And again, it takes you a while to
get these expressions right, and you test this code for a while,
and I don’t write these all out. Maybe you can, but I tend to write
this much and print that out, and then add another thing, and then add another thing, and say oh I got
my 42 point whatever, and I’m happy. But, and I’m not giving you this one
in XML, this would be a lot harder in XML because you’re doing gets, and
gets, and finds, and gets, and finds. And now we’ve got a dictionary
that contains a list, a list of dictionaries, and dictionary
within dictionary within dictionary. And so that’s what this is,
the outer bit is a dictionary. And within one key there’s a list,
and within that the first item has another dictionary that has then
another dictionary inside of it, right? So a dictionary that contains a list,
that contains dictionaries, and each of those contains a dictionary. And so that’s,
I mean, we didn’t make up this format, I mean, Google told us what the rules
were and so we’re like oh thank you Google. And you can complain about that,
but hey, Google just says you want to use our thing,
learn this syntax, write this code. And so you can sort of cruise through
there and print all that stuff out. So up next we’re going to
talk about a Twitter API, which is a little bit more
complex than the Google API.

Video: Worked Example: GeoJSON API (Chapter 13)

Summary of the video about using Google Maps API with Python:

Introduction:

  • The video discusses using the Google Maps API with Python for geocoding (converting addresses to coordinates).
  • The API used is free and allows 2,500 requests per day.
  • Different parts of the API require API keys and OAuth, but this specific address service remains free.

Key points:

  • The API requires URL encoding the address (spaces as +, commas as encoded characters).
  • The response is JSON containing details like formatted address, geometry, latitude, and longitude.
  • The code demonstrates fetching data, handling errors, and extracting relevant information from the JSON response.

Demonstration:

  • The video shows Python code fetching data for “Ann Arbor, Michigan” and extracting latitude/longitude and formatted address.
  • It highlights the importance of debugging and iteratively constructing the data extraction logic.

Conclusion:

  • The video provides a basic example of using Google Maps API with Python for geocoding.
  • It emphasizes the need for careful data handling and parsing the JSON response.

Additional notes:

  • The video mentions a later chapter covering API keys and OAuth for other parts of the API.
  • It’s important to be aware of API usage limits and terms of service.

Unleashing the Power of Location: A Python Tutorial for Google Maps API

Harness the potential of Python and the Google Maps API to bring geographical data and visualization to your projects. This tutorial equips you with the essential tools to utilize location-based information, navigate the API, and extract valuable insights.

Prerequisites:

Let’s Dive In:

1. Setting Up Your Environment:

  • Start by installing the required libraries: pip install requests json
  • Obtain your Google Maps API key and store it securely (environment variables recommended).

2. Exploring the API:

  • Refer to the official Google Maps API documentation (https://developers.google.com/maps/documentation/) to understand various services and functionalities.
  • Familiarize yourself with key terms like geocoding (address to coordinates), reverse geocoding (coordinates to address), and directions API.

3. Basic Geocoding Example:

Python

import requests

# Your API key
api_key = "YOUR_API_KEY"

# User-provided address
address = "1600 Pennsylvania Ave NW, Washington, DC"

# Construct the URL with API key and address
url = f"https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={api_key}"

# Send the request and retrieve JSON response
response = requests.get(url)
data = response.json()

# Extract desired information from the response, e.g., latitude and longitude
latitude = data["results"][0]["geometry"]["location"]["lat"]
longitude = data["results"][0]["geometry"]["location"]["lng"]

# Print the coordinates
print(f"Latitude: {latitude}, Longitude: {longitude}")

4. Going Beyond: Explore More Features:

  • Utilize reverse geocoding to find addresses based on coordinates.
  • Leverage the directions API to calculate routes and travel times between locations.
  • Visualize data on a map using libraries like folium or matplotlib.

5. Remember:

  • Always respect usage limits and terms of service of the Google Maps API.
  • Consider error handling and data validation in your code for robust solutions.
  • Explore advanced features and services based on your specific project needs.

Bonus:

  • Check out community resources and tutorials for deeper exploration and inspiration.
  • Experiment with different data sources and visualization techniques to unlock hidden patterns.

By following these steps and practicing along, you’ll be well on your way to mastering the Google Maps API with Python. Remember, the journey of location-based exploration is vast, so keep learning and creating!

import urllib.request, urllib.parse, urllib.error
import json

# Note that Google is increasingly requiring keys
# for this API
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'

while True:
    address = input('Enter location: ')
    if len(address) < 1: break

    url = serviceurl + urllib.parse.urlencode(
        {'address': address})

    print('Retrieving', url)
    uh = urllib.request.urlopen(url)
    data = uh.read().decode()
    print('Retrieved', len(data), 'characters')

    try:
        js = json.loads(data)
    except:
        js = None

    if not js or 'status' not in js or js['status'] != 'OK':
        print('==== Failure To Retrieve ====')
        print(data)
        continue

    print(json.dumps(js, indent=4))

    lat = js["results"][0]["geometry"]["location"]["lat"]
    lng = js["results"][0]["geometry"]["location"]["lng"]
    print('lat', lat, 'lng', lng)
    location = js['results'][0]['formatted_address']
    print(location)

# Code: http://www.py4e.com/code3/geojson.py
# Or select Download from this trinket's left-hand menu

Hello and welcome to Python for everybody. We are doing some code samples here. If you want to follow along you can download the sample code or it’s in the big zip file. I’ve got it. We are going to be working with the Google Maps API. In the old days, this Maps API was free and did 2,500 requests per day. But now, they’ve made it so that parts of it are behind API keys and you start over using OAuth and stuff but they haven’t put it all behind this one address service that we’ve been using, that continues to work. The basic idea of an API as you go read the documentation, you find a URL and this is going to Google servers and you pass in the address. We have to pass in the address using what’s called URL encoding. So, spaces are pluses. That’s a comma and then that’s a space. So, we have to pass this in a certain way. But if we do it right, we hit this we’re going to get ourselves some JSON back, and that’s really cool. So, deep inside here we get the real address. A good address. We get a geometry. We have the location. We got the latitude and longitude. We can extract stuff out of here. So, we’re talking and this one here is still rate limited to 2,500, but it’s one of the few parts of the Google Maps API that is not hidden behind an API key. In a later chapter, we’ll show you how to actually talk with the API key in the geodata code. The geoload, shows you how to use an API key if you want to jump ahead and take a look at that. But for now, we’re just going to take a look at GeoJSON which is going to retrieve one page and tear it apart. So, let’s take a look. So, we’re going to grab the urllib stuff and import JSON. So now, we’re going to use JSON but we’re going to actually pull the data out of the Internet. So, I just take that service URL for Google Maps API. I found that somewhere in documentation. Then I’m going to have a loop that’s going to run forever. I’m going to add for the add the location. Then if I hit enter that’s what this is saying, get out of the loop. Then what I’m going do is, I’m going to concatenate the the service URL which is this. This URL parse URL encode gives a dictionary of address equals. This bit right here gives me the string that leads to putting this address equals, but they’re encoding these spaces the right way. So, if you type a space that bit of code turns it into the plus. So, that’s important and I’ve got the question mark sitting here at the end of that. Then what we’re going to do is, we’re just going to do a urlopen to get a handle. We’re going to read the whole document and because it’s UTF-8 coming from the outside world and we want it turned into unicode inside our application, we say.decode. We can ask how many characters we got and we put our JSON loads. Now, up till now we’ve been just doing loads’s from internal strings. But this is now a string that came from the outside world. We’ll put a try except in and we’ll set js to be non and that’ll be our little trigger. They give us, we take a look at the output. They give us this okay and that status can be a problem and it can complain about things. So, we have to check to see if we got a good status. So, at this point, if you look at the outer bit of this, the outer bit that we get is a curly brace so it’s an dictionary. Then there is within that dictionary and key results which is a list. But then the second thing in the outer dictionary is status. So, we can ask if we got a fall. So, forgotten nothing that will quit. If we don’t have a status key in that job and that object or that dictionary or it’s not equal to okay, any number of those things. If this, or this, or this, are all either of those are true, we’re going to quit for failure to retrieve and print the data out. When you start to restuff on the net, you often have to put debugging in here like this, something quit I got to figure out and so debugging. Next thing we’re going to do is called JSON dumps which is the opposite of loads which takes this array that dictionary and includes arrays and we’re going to pretty print it with an indent of four and then we’re going to print that out. So, if you look at my code we’ll see that the first thing we do once we parsed it as we print it back out so we can see it. Then we’re going to dig into it. So, let’s go ahead and run this code. Python geojson.py. One of these days I will always type Python3 Ann Arbor Michigan, okay. So it ran, and so you see that it retrieve this URL. This URL was constructed and retrieved 1736 characters and it’s JSON pretty printed with an indent of four. This is that JSON dumps all the way down to here. So, that’s just JSON dumps. Then it starts extracting. So, it’s going to pull things out. Now, when you write this code is really easy to look at this and say, “Oh, great it’s easy.” I tend to have to print this stuff out over, and over, and over, and as I construct this expression. But if we look at it, the outer dictionary sub results leads to this array. If you go look at this array careful, you find there is only one thing in it. So, that the results is an array sub zero gets us this dictionary. I keep on I saying object because that’s what it’s called and that goes all the way down to here. So, that’s what we get there. Then within that, we now have an object and we look for geometry within that object, where is geometry? Right there, geometry. Geometry goes from there to there. There’s geometry in there. Got here used to it that’s why it’s nice to have this stuff indented. Geometry sub low. Oops come back, come back and then we go to location within that, so jump location within geometry. Then well then let’s job location we have lat and long. So, this is pulling out this 42 and 83. Then, so we print that out. Take a look, and that prints that out. Pulls that right out of the JSON. These are tricky to write but after awhile you win and you get it right and it’s just fine, okay. So, we do the same thing results sub zero formatted address gets us this. So, that’s how we print the location out. So, that’s a real quick look at how we would do that with the JSON talking to the Google Maps API. Okay, hope this helps.

Video: 13.8 – Securing API Requests

This lecture explains how to access and use APIs like Google Maps and Twitter. Here are the key points:

APIs:

  • Offer free resources like data and functionality.
  • Usage might be limited (e.g., Google Maps requests per day).
  • Some APIs require authorization (e.g., Twitter).

Twitter API example:

  • Requires authorized keys and secrets.
  • Uses urllib library to access data.
  • Parses JSON response to extract user information.
  • Requires signing URLs for security (OAuth).

Key takeaways:

  • Understand API usage limits and authorization requirements.
  • Learn basic techniques to access and process API data.
  • Be aware of security measures like URL signing.

Additional notes:

  • The lecture mentions code examples that will be covered in separate videos.
  • The Twitter API requires setting up keys and secrets in a hidden file.
  • OAuth protocol is used for secure authorization and URL signing.

import urllib.request, urllib.parse, urllib.error
import twurl
import json
import ssl

# https://apps.twitter.com/
# Create App and get the four strings, put them in hidden.py

TWITTER_URL = 'https://api.twitter.com/1.1/friends/list.json'

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

while True:
    print('')
    acct = input('Enter Twitter Account:')
    if (len(acct) < 1): break
    url = twurl.augment(TWITTER_URL,
                        {'screen_name': acct, 'count': '5'})
    print('Retrieving', url)
    connection = urllib.request.urlopen(url, context=ctx)
    data = connection.read().decode()

    js = json.loads(data)
    print(json.dumps(js, indent=2))

    headers = dict(connection.getheaders())
    print('Remaining', headers['x-rate-limit-remaining'])

    for u in js['users']:
        print(u['screen_name'])
        if 'status' not in u:
            print('   * No status found')
            continue
        s = u['status']['text']
        print('  ', s[:50])

So one of the things about that
Google API that we just got done talking about is that is an amazing
amount of free resource for us. Google has servers and buildings,
and staff, and researchers, they wrote the code in the first place,
they accumulated all the data, they did all the searching. They tested the data. They cleaned up the data and we just sit
there and write 40 lines of JSON and we’re taking advantage of it. There’s no such thing as a free lunch. The data behind these APIs
is often very valuable. So there is ways that these organizations
either want to monetize this, or limit their exposure. Sometimes Google is trying to do this for
the greater good to make it so there are more location-aware
applications. But frankly if you are going to build
a location-aware application that has a billion users they probably
want a cut off your deal, right? And so they tend to build these things and
the rules kind of change. If you look at Google this may or
may not the current but that particular API we were doing
limits you to 2,500 requests per day. And later I will show you some
code that I wrote if you have to do geocoding on more than 2,500 things you
got to break it over a couple of days. And so I’ve shown in a later application
geojson, there’s a whole zip file in the code section, that shows you
how to start this and stop this so you can get like 2,000 and then wait 24 hours and
get 2,000 more and wait 24 hours. So if you’ve got 10000 to do, well,
after a week, you’ll have it. It automatically starts itself off and
stops itself. And it uses a database to
keep track of where it’s at. Or you can pay Google if you want, and
they’ve got rates for that and on and on and on. So it’s not as simple as wow this is free. In this case as long as you don’t
use it too much this is free, that’s why it’s a good
starting point to show you. Now another API that is an API
that you might find interesting. Facebook has APIs. Twitter has APIs. I’ll talk a little bit
about the Twitter API. The thing about Twitter API
that’s different is the Google API said from your
computer you can only get 2,500 requests. But for the Twitter API, you get zero
requests unless you are authorized. And so this is a situation where
there is a required authorization. And it’s kind of like what you do
on your browser or on your phone, where you log in first and then you can
use this application as much as you want. In this case, you can log in and
then use this API. Not necessarily always as much as
you want, but authorization and authentication is part of some APIs. And so later I’ll give you a much more
detailed walk through on how to do this. But for now I just want to
give you a high-level overview of what we’re going to do here. And so there is a URL that you can go
to for Twitter that basically creates keys and secrets, kind of like
IDs and passwords except for applications. And so once you know how to authorize
your URLs you have a documentation for the API on Twitter that you can use. And so here is like the rules for
the Twitter, the Tweets API. How you can go find the API. And in this it’s telling you even though
you might not be able to read it. You have to hit this URL and
you can do this and you get a timeline, a user timeline, a favorites timeline,
a retweet timeline setter. So there are URL patterns that you
can hit to get different kinds of data from the Twitter API. So here’s a bit of code and I’m not going
to go through it in tremendous detail. I’ll just call out some of the highlights,
because I’m going to have a separate video where I just go through this and
actually run the code. But let me just give you
sort of a summary of this. So we are going to be using urllib. Twitter URL is a bit of code that I
wrote that augments these URLs and deals with that authorization problem. Of course we need JSON, and so we read what the Twitter URL was
from the Twitter documentation. It says that’s where you
go to get a friends list. And so again, we’re going to have
a while loop that’s going to ask for a Twitter account. Again if you hit enter
we’re going to skip out. And this augmenting of the Twitter url
this is basically doing a whole bunch of stuff that is both putting on some
things like screen name equals and count equals and doing all the url encoding. But it’s also putting on the security
stuff and we’ll touch on that in a second. So this is I wrote this. That’s twurl.py, which we’ll
show you in a second. And then you hit this URL. It includes login information in this URL. Then we do our urlopen. Then that gives us just
sort of the connection. But then we do connection.read and as always because it’s coming out from
the outside world, it’s probably UTF-8. We have to do a decode to bring it in. But now inside data we’ve got a string
which represents the curly braces and the square braces, and the commas and
all that stuff, right? It’s not the JSON. It is the string
representation of the JSON. And so that’s what that does, and
this next line is kind of interesting. Remember when I told you that urllib,
this is a long time ago. I told you that urllib eats the headers,
you don’t get the headers, but on all these request/response
cycles that we’ve been doing, we’ve been getting headers all along. So it turns out, that this is the line
of code that you use to get the headers. And so the data comes in. You do
connection.read, that gets you the body, and then if you say connection.getheaders,
that is a method inside connection. It had the headers all along and
it kept them. And you said give me these headers and
give them back to me as a dictionary. Now if you look back at those headers, they’re a bunch easily
represented as key-value pairs. And what’s going to happen here is Twitter
is going to communicate to you how many more of these requests they are going to
allow you to do before they shut you down, because it’s a thing per day. And the name of the header
is x-rate-limit-remaining. And that gives you a number
that’s like 12, 11, 10, 2, 1, 0 and if it’s like 0, you better stop, right? because they are basically
saying they’ll tell you when you retrieve this how many
more of these you can do and this particular one you only got like
15 a day or something like that. And so what we can do is we can print
this out see so how many you’ve got, so that just prints out
the number that’s left. Then we’re going to parse this and
we’re going to also dump it. This dumps takes this JSON structure and
then prints that out in a pretty way, with an indent, with curly braces and
stuff like that. So that’s nice. And then, if we look at what the JSON
looks like, which is down here. So it says we’ve got 14 of these requests
remaining, it’s an object that has users, which is an array. And then each user is itself an object. And so, the outer one sub users,
js sub users, is an array. So we’re going to have u that’s going to
loop through each one of the users in this output. So there’s the output, right? So u is going to iterate through this one,
then this one, then this one, then this one, on and on and on. And in there will be the screen name and
their real name, etc, etc, etc., right? So we’re going to go through each user,
we’re going to print out the screen name. And then we’re going to print
within the status their text and the only first 50
characters of the status. And so if we run this program
which I’ll do in another video. I’ll actually run this for you. You see the person’s screen name. You see their last status. You see the next person’s screen name. So this loop is going through all
the loop users and printing that out, and so it’s not that hard. This little bit here with
the Twitter URL is a little tricky. We’ll show you that in a sec. But just reading it isn’t all that crazy. It’s not all that crazy to
read this data and parse it. So that’s what that one is. So when you are going to set up these
keys, you’ve got to go into this web site. You’ve got to get these values. You get this token. You got this key. You got this secret. You got this token secret. And they’re just these
big long crazy strings. And there is this file called
hidden.py on the sample code. And you have got to cut and paste the four values that you get from the
Twitter web site after you’ve logged in. It will give you these values and you’ll have to put them in, so
hidden.py is part of twurl that makes this thing. So, you have to get these right, and if you don’t get these right,
your Twitter code’s not going to work. And I’ll show you how to do
that in a recorded video. It uses a protocol called OAuth, and
basically what we’re doing is we’re signing URLs, and so it’s not just a URL.
It’s a URL plus the signature on the URL. And it’s signed in a way that only
you as long as you know the keys and the secrets can sign that stuff. And so this is the Twitter URL
code that I’ve written. hidden.py is what you have to write. You put all those four strings in there,
and that’s the consumer_key, the consumer_secret,
the token_key and the token_secret. You set all that, and
then I call this oauth library, that’s not your code, that’s actually
part of Python that does this oauth. And when it’s all said and done,
we do all these things, make a request and then we sign this request and
then we convert it to a URL. When it’s all said and done,
the url looks like this. These are the, this is the data we’re
interested in. We’re looking for count of 2 and the screen_name of drchuck and
then all of this stuff that says oauth_version equals oauth_token,
oauth_timestamp, oauth_signature. All of these stuff is the magic stuff
that is part of that signed URL, okay? Now you don’t have to worry about that,
because as long as you get hidden.py and you called twurl,
you’ll get back this big long URL. And you hit that with urlopen, and
Twitter will read these things and say good numbers, good numbers. Okay, here’s your data. And if you don’t know what this data,
if you don’t have these secrets right, then you can’t see it. And it also is a way
for Twitter to know if it’s you requesting the data or me requesting the data
because somewhere in here is who I am. Not screen name equals Dr. Chuck,
that’s the data we’re actually asking for. But the key, and the consumer key,
and the consumer secret are the trick where it says, hey this is Chuck
having logged in asking for API data. And so that is how Twitter basically
knows how many you’ve done and how many I’ve done so
you get 15 and I get 15. But once I run out of 15,
I can’t use anymore. So we’ve gone a long way. We had we started with oh, urllib can get
data to serialization and de-serialization. To formats like XML and JSON,
to the notion of applications that begin to be distributed running on
different computers connected via network. The service oriented architecture,
cutpoints where we agree on APIs, contracts, etc, etc, etc. And so this is sort of just enough to
get you started on Web services and service oriented architectures.

Video: Worked Example: Twitter API (Chapter 13)

Summary of Python for Everybody Tutorial on Twitter API:

This tutorial covers using the Twitter API in Python to access user data and statuses. Here are the key points:

Authentication:

  • Requires API keys (consumer key, consumer secret, token key, token secret) stored securely in hidden.py.
  • These keys are used to sign each request and ensure authorization.
  • The tutorial emphasizes not revealing actual secret keys for security reasons.

Rate Limits:

  • Twitter API enforces rate limits on how many requests can be made within a time period.
  • The tutorial demonstrates checking and printing remaining calls to monitor usage.

Sample Code:

  • twtest.py: Retrieves user timeline data using twURL function for OAuth signing.
  • twitter2.py: Retrieves friends list and latest statuses for each friend.
  • Both scripts handle SSL certificate errors and decode JSON responses.
  • They demonstrate parsing the JSON structure to extract specific data like screen names and status text.

Key Learnings:

  • Importance of proper OAuth authentication and handling rate limits.
  • Techniques for parsing and manipulating JSON data in Python.
  • Debugging and fixing errors encountered during development.

Note:

  • The provided code might contain typos or errors that are fixed in the downloadable version.

twtest.py

import urllib.request, urllib.parse, urllib.error
from twurl import augment
import ssl

# https://apps.twitter.com/
# Create App and get the four strings, put them in hidden.py

print('* Calling Twitter...')
url = augment('https://api.twitter.com/1.1/statuses/user_timeline.json',
              {'screen_name': 'drchuck', 'count': '2'})
print(url)

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

connection = urllib.request.urlopen(url, context=ctx)
data = connection.read()
print(data)

print ('======================================')
headers = dict(connection.getheaders())
print(headers)

Welcome to Python for Everybody. We’re doing some sample code, playing through with some sample code samples and you can get this by downloading it. I’ve got this whole thing downloaded and I’ve got all the files here, and these are the files we’re going to play with today. Today, what we’re going to do is talk about the Twitter API. The one thing we got to learn about the Twitter API is we have to authorize ourselves. So, we have to make sure that we have a Twitter account and then we get some keys. So, in this particular application if you want to duplicate what I’m doing, you have to go to apps.twitter.com, click this create new application button, and then get some codes, okay, and the codes show up as soon as you hit this button and then one more button, which I’m not going to do on screen. So, what happens is there are four codes that you got to put in this file hidden.py, consumer key, the consumer secret, token key, and token secret. These are just messed up, so I’ll show you how this works and blows up first and then I’ll put my keys in here without showing you. But basically, this is a little file you got to edit or these Twitter ones don’t work, you’ll see what happens. So, the first one I’m going to do is do the simplest one of all and that is, I call this thing Twitter test and it just is going to go ask for the user timeline and we can take a look at this and we’re going to take the URL and we’re going to augment the URL. This is the base, we found this looking at the Twitter API documentation. We’re going to pass a parameter of screen name Dr. Chuck and account of two, so this is just a Python dictionary. An augment comes from this little bit of called code called twURL. This uses a bit of code called OAuth, which is built into Python as well, right? Yeah. That’s built into Python as well and it augments the URL. It takes the the key, the secret, the token key and does a thing and signs it, and then makes this big, long, ugly URL which you will soon see and does this, it’s a signature of the URL. So, we pass this data back and forth to Twitter with a signature and then they recheck the signature and it’s a digital signature that knows that this URL came from a program that knows the key secret and token and token secret. So, this augment basically, is something that I wrote, twURL augment is something I wrote to make it easier to add all these OAuth parameters. You feed this code by putting your data into hidden.py. Lots of people get this to work so, don’t worry it’s cool when you finally get it to work. So, let’s take a look at what it does just know that this makes an awesome URL that does all the security, and we’ll see one of those URLs. So, ignore the certificate errors. This has to do with the fact that we’re using HTTPS and Python doesn’t have enough certificates put into it by default for a lot of reasons, but our quick and dirty ways they turn them off. Thank you Python for reducing security by teaching us, so that this is the best way to do it, that’s a grumpy up moment from on my part. So, what we’re going to do is we’re going to do a URL open. This bit here is to shut off the security checking for the SSL certificate, and then we’re going to read all the data, and then we’re going to want to print it out. We’re also going to ask the connection this url. Remember, I told you a long time ago that URL lib eats the headers but you can get them back. Now, we’re going to ask to get a dictionary of the headers back, and so we’ll print those out, okay. So, this is really just testing the body and the headers and printing them out in as raw a way we can do. So, let’s go run this. Now, this is going to fail the first time we do it because we haven’t put the hidden variables in there. So, if I say python3twtest.py, it’s going to run and blow up and it’s going to give you this 401 authorization required. That’s a good sign because that means that you haven’t yet updated your values in hidden.py. So, this is that augmented URL and you can see the consumer key and consumer secret and the OAuth token and whatever, okay. So, these tokens are wrong. These aren’t Control-C, they aren’t real. But you’ll notice it doesn’t end the key and the secret of the token key of the token secret and the secret. That’s all actually encoded in the signature. It turns out that you need to have the key and the secret and the token secret to generate the signature and where is the signature? There’s the signature, right there’s the signature. So, this signature combined with the notes, that you can only do this signature as a time and includes all kinds of things. So, even if you type this in, well, you’ll see these go by and it’s not really breaking my security too much when you see these afterwards. So, don’t get all excited when you say, “Oh, you revealed your token and your key.” Now, I can reveal my token and key but I’m not going to reveal the secret. So, this adds all this OAuth stuff, OAuth nonce, OAuth timestamp. These timestamps and nonces are made it so that you can’t replay my URL even if you see the exact URL. Once I hit it, then you can’t hit it again and so, that’s what the nonce does. So, I’m going to close hidden.py here. I’m going to update hidden.py in another window, okay. So, I just in another window, I updated hidden.py. I’m not going to show you that. But now, I’m going to run python3twtest.py. So, twURL is going to read hidden and now, these keys and secrets are my real ones that I haven’t shown you. So, this should work, fingers crossed. It worked, okay. So, it worked. So, I’m calling Twitter. Here’s the URL. Now, don’t worry, the token and the consumer key are not enough to break into my account and neither is the signature because you can’t replay this. In about five minutes, you can’t replay this anymore, okay. So, you can’t generate the signature. I’ve done one, the signature includes the time and date, so you can’t trust me. Go read up on OAuth, don’t worry, I haven’t really revealed anything. But, so, the first thing we see is this. So, we see, and we should put it like the line and dashes here. This is the Json. It ain’t very pretty, it’s not very pretty. Okay, and so that’s the Json, from there to there. It’s just what most APIs give us back, it’s really dense Json, right. So, this is a byte array. Remember, how you have to do a.decode, I didn’t do a.decode here. So, this is telling, Python is telling us this is a byte array which it’s a raw set of bytes that came from the Internet, which probably are UTF-8. If I input a decode here, then it would decode. If say.data.decode there, then it would be fine. But we don’t care, this was just a dump, do we get anything? So, then, here let’s do this, print. I’ll just make this code different. Put some equal signs here, a lot equal signs. So, we can easily see where the thing starts and stops. So, we’ll run that again if you look at those URLs. So, that was all of that stuff and then this is the headers, and so the headers again are not pretty. You get the headers it’s a dictionary, you got cache-control, no cash comma. This is the string key value, you got to find your commas key value, but the one that’s really interesting here is which one is it? X-rate-limit-remaining, right there, x-rate-limit-remaining remaining. So, that means that for this particular API in this header tells me that I’ve got 898 calls left, and this is when I will get more calls and so watch. I’m going to do this again and you will see that I can only do this 897 more times now. Run it. I can only do this 897. So, I am being tracked at this point. I’m being tracked by Twitter. Twitter knows that it’s Dr. Chuck that’s doing this and Dr. Chuck is done 900, he’s done 899, 897, and if I keep running this, eventually Twitter will tell me, “You got to wait for a while”, and that’s because Twitter doesn’t want me under my Dr. Chuck account pulling out lots and lots of stuff out of Twitter and making my own website. I do actually have my own twitter website using some cool software www.doctorchuck.com/Twitter and this I have to run in rate limits and causes all kinds of you know, whatever. So, okay the rate limit. So, I’ll save that. So that’s tweet, this is just a test, okay? Because I want to do something interesting, so we’re not parsing the Json that comes back, we’re not doing anything tricky with this and away we go. So, let’s take a look at some more code. I think I don’t need this anymore. So, now I am going to parse this. So, most of this looks the same. I’ve got that same user timeline Json, I’m going to ignore the SSL certificate someone write a loop. So, I’m going to ask the Twitter I’m going to print, I’m going to get a Twitter account and quit if it’s a blank line or phase to entering. I want to use the Twitter URL augment the same way that’s going to do all the signing using from hidden.py and retrieve it and I’m going to retrieve it ignoring the SSL errors and then I’m going to decode. This time I’m going to decode it so that I get a real Unicode string, and I’ll print the first 250 characters of it, I’m going to grab the headers, and I’m going to print the remaining, the rate limits. This is sort of a very simple version of this same thing. It really is decoding the data and only printing the first 250 characters. So, let’s run that, Dr. Chuck. Boom, and it’s got 896. So that’s just a little simpler version that with a little less brutal debugging. Okay, so now let’s do something even more fun. Let’s go to twitter2.py and tear it apart. So again, we’re going to look at my friends list or someone else, any of his buddies’ friends list. We’re going to ask for the friends and ask for the screen name, ask for the first five friends, and then look at their statuses, open it, decode it, get the headers, print the rate remaining. All this stuff is the same as in twitter1 but now we’re going to parse the JavaScript. I’m not even putting this in a try and except because, hey, I’m talking to twitter. I’m going to guess that twitter is going to give me the right stuff. You probably want to put a try and except here. Then I’m going to do a debug print. I’m going to do a Json pretty print. Let’s make that to be two, so it looks a little better. And then, I’m going to run it and then you’re going to see how we have to parse this. We’re going to see that it’s a list. So, we’re done with that and now we’re running twitter2.py. So, I’m going to go to Dr. Chuck and this is going to ask the question, who Dr. Chuck’s friends are? Okay, let’s go to the top. So it hit this API and it has the screen name Dr. Chuck, count equals five and all this oauth stuff. Again, this is not a security breach by showing you all of this because the signature, the secrets aren’t there. Okay, so if we look at it, it’s an outer object or dictionary and then the outer has a users which is a list. Then, each user has some stuff in it. So this one’s Stephanie Teasley. It’s got her screen name, it’s got some descriptions. Keep on going and it’s got her status, her latest status, for my friend her status. Her source, where she’s at. Man she’s got a lot of stuff here. Okay. There we go, that was the first one and then the next one that I’m following is LiveEdu etc. So you’ll see that this is an array. So that outer thing is an array of users. Now, JS here is a dictionary. So I can say for you in JS sub-users. Well JS sub-users is a list. So the first U is going to be this Stephanie Teaseley U and the second U is going to be LiveEdu. So, that’s all it took to get through all that stuff and figure that out. And then I’m going to say, get me the screen name of my person. So let’s go in here. So that’s going to pull Stephanie Teasley as stephteaseley out then. Then I’m going to go find her status. Let’s find her somewhere in here. U sub status, sub text. Come on. Okay, there’s sub status, sub status is all this stuff. More and more. Right there, that status, that U sub status is that and then U sub status, sub text is this step. So it’s going to extract this bit right here, okay. So, use that as text and I print out the first 50 characters of the screening status, and I do that for the first five because I told it I only wanted five. And then of course I get to see that the right limits. So let’s go down to the bottom. So all of this is the debug print of the Json I got back. Here is the program starting to print. Here is the screen name of my first friend and here’s the first 50 characters of her most recent status. Here is the screen name of my, and these are in reverse order, who I’ve been following. So I’ve been playing with this live coding stuff. So I’m following them. What! KeyError status. That didn’t work. Why not? Well, that’s because Livecodingtv somehow doesn’t have a status. So, most of these work. So now you’ll get to see me fix something and when you download it, it will be fixed. So, it says KeyError status. So that means that I’ve got to do a thing that says, if status not in U print no status found. Continue. Since sometimes there’s no statuses. Who would have thought. I did not know that. Okay, so let’s run this again. Did I get to see my remaining? Oh, actually let me change the order of this. Let me put this down here. It’ll be wrong from the slides but it’ll be prettier now. Let’s put the headers after the dump of the data. Okay. So let’s run it again. Did I save it? Yes. Dr. Chuck. A whole bunch of stuff. So I got 13 remaining calls on this one. So it is not the same as the other one. I don’t get to call this too many more times so hopefully I’ll get the debugging to work. I got a bad space here. No status found. And I need to put three spaces there. No status found. I’ll make an asterisk. So let’s run it again. See I got 13 remaining. So, it’s important you write code that’s aware of your remaining. That’s why I make it so obvious about that. Go retrieve all that. I got 12 remaining but I now have another space here. Hang on I got to fix that. I need yet another space. Hopefully, I can make this as pretty as I want it to work. Wait a second, I didn’t even do Dr. Chuck, I got that wrong. Typed my name wrong. Okay, so now it works. Oh well. So, now I have my five recent friends are stephteasley, Liveeduofficial, Livecodingtv, Nancy Gillby and GreggyKrueger. And so there are there are statuses and I tore all this Json apart using twitter2.py. Of course, after fixing hidden.py, which I’m not going to show you because it actually contains my real consumer key and consumer secret, you’re seeing the consumer key and the token key go by on each of these URLs but what you’re not seeing is these two things which are the thing I’m protecting, so that it’s not a problem. Okay. So I will send that up, there you go. Welcome. I hope you found this useful. The code will be fixed when you take a look at and download it here from samplecode.zip.

Assignments


Quiz: REST, JSON, and APIs

Who is credited with the REST approach to web services?

What Python library do you have to import to parse and handle JSON?

What is the method used to parse a string containing JSON data so that you can work with the data in Python?

What kind of variable will you get in Python when the following JSON is parsed:
[ “Glenn”, “Sally”, “Jen” ]

Which of the following is not true about the service-oriented approach?

If the following JSON were parsed and put into the variable x,

What library call do you make to append properly encoded parameters to the end of a URL like the following:
http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=Ann+Arbor%2C+MI

What happens when you exceed the Google geocoding API rate limit?

What protocol does Twitter use to protect its API?

What header does Twitter use to tell you how many more API requests you can make before you will be rate limited?

Graded App Item: Extracting Data from JSON

Reading

Code

Graded App Item: Using the GeoJSON API

Reading

Code

Bonus Material


Video: Bonus: Office Hours – Melbourne, AU

Dr. Chuck’s Melbourne Office Hours Summary

This video features Dr. Chuck hosting an Office Hours session in Melbourne, Australia. Participants introduce themselves and share their positive experiences with the class.

Attendees:

  • John
  • Francis
  • Brent Groves
  • Derek
  • Junie
  • Alexander Kristov
  • Belinda
  • Mike Murray
  • Fabian
  • Greg
  • Charles

Key points:

  • The session was held in Melbourne with a large turnout.
  • Participants enjoyed the class and found it valuable.
  • Dr. Chuck interacted with attendees and answered their questions.
  • Everyone received stickers.

Overall, the video showcases a positive and engaging learning experience for the participants.

[MUSIC] Hello everybody. This is Chuck, and
I’m in Melbourne, Australia. And here we have a yet
another face-to-face Office Hours. And we have a bunch of people, and I’m
gonna let them introduce themselves, so. >> Hi, my name’s John. We’ve just spent about what,
two hours here? >> Yep.

And great time. >> Hi, my name’s Francis. We’re sitting here,
having a great time by the Yarra. And hi to everyone else. >> My name is Brent Groves and anybody who’s interested in the future
should take this and we’ll all see. >> [LAUGH]
Yeah. >> I’m Derek and can’t believe I
got to meet the famous Dr. Chuck. So thank you. >> I can’t believe I met the famous Derek. >> Hi, I’m Junie and
I really enjoyed the class. And we’ll have a good time here today. >> Hello, my name is Alexander Kristov. I live in Russia. Thanks for all the juicy videos. >> [LAUGH]
Hi, I’m Belinda. I’m loving the class. I’m just getting back into education, and this has been a really
nice place to start. >> Hi there friend, Mike Murray. Thanks for everything. >> Hi, I’m Fabian and
I’m really enjoying the class. >> I’m Greg, I’m from New Zealand. I did the last MOOC and
I really enjoyed it. >> Hi there, I’m Charles. Just flown in from Christchurch,
and it’s great to be here. >> You’ve flown in from Christchurch? >> Yes. >> For this? >> No, no yesterday. I flew in. >> For this? >> No, I work here now. >> Oh okay, okay. Wow, that would be crazy. So thanks to everybody. This was great.
We had a really big turn out here at Melbourne. And everybody got stickers. >> [LAUGH]

Video: Bonus: Office Hours – Santa Monica, CA

Internet History Technology Office Hours in Santa Monica: Student Perspectives

This video features Charles Severance hosting an Internet history technology office hours session in Santa Monica, California. Three students, Anthony, Mo, and Melanie, share their experiences with Coursera and online education.

Key takeaways:

  • Anthony: Enjoys using a screen reader with Coursera and finds it accessible. Views Coursera as a way to learn more beyond formal education.
  • Mo: Has taken multiple online courses and values the flexibility and variety of topics offered. Believes online education complements traditional formats but may not replace them entirely.
  • Melanie: Non-technical professional who appreciates Coursera for making learning accessible. Sees it as a valuable supplement to traditional education and a potential threat to education as a business.

Overall:

The video highlights the diverse perspectives of students and their positive experiences with online education platforms like Coursera. The discussion touches on accessibility, cost, flexibility, and the potential impact on traditional education structures.

Additional notes:

  • The session took place shortly after the Rose Bowl football game.
  • Charles encourages viewers to explore Coursera and see for themselves.

Hello, Charles Severance here, again with another Internet
history technology office hours. This time, January 3rd or
4th, 2014 in Santa Monica, California and I’d like to introduce
you to some of the students. >> Hi, my name is Anthony from Carson,
California. I already enjoyed learning about
history of the Internet because, yeah I’ve been using it for
years and years and years. And I really had no idea
how this all got together. I use a screen reader called JAWS. And because it’s pretty much all HTML,
the screen reader reads everything fine. Every link is tagged correctly, pictures, and haven’t actually had to encounter
any images or anything like that. But links are tagged correctly. When I take the quizzes, the different
choices are tagged correctly. I haven’t had any problems with it,
and I’m actually very happy for that. >> How about the lectures? How do the lectures work you? >> I download the videos. I just click on the download link. Open up my Windows Media Player and
just listen away. >> So tell me a little bit about how Coursera fits into your
like education plan? In this for fun? Do you have any goals? What kind of education do you have and
where does Coursera fit in? >> Yeah. Oh, I see. I have two bachelors from Long Beach
State, a Master’s from Stanford. So for me, Coursera is just more to
learn more about the world around me, and to listen to professors from
other great universities teach. For the first time really you
can feel as if you’re sitting in a classroom at Michigan or,
at the university. Like Hong Kong University of Science and
Technology or whatever. And it’s really cool, because there
are other professors who are there and because of the Internet,
it’s compressing time and space. And you can be there. So, for me,
I think I would be taking more. I’ve taken four courses. Five courses. And I plan to take more this year. This is only my,
I started in April of last year, so we’ll see how many more I take. >> Okay.
Thanks, Anthony. >> Yeah, thank you. >> Well done. >> Wow, good energy. >> Okay. So, Mo, tell us a little
bit about your background. >> I studied computer science, graduated
in 1983 from Cal State Northbridge. I’ve been working since then. Developed CAD cam software, computer-aided
design software for about 26 years and then we were off-shored to India. And from that,
I started working at a medical record company developing EMR packages for physical therapy, physical therapists. >> So tell me how Coursera sort
of fits into your education. How many classes have you taken, what
are your plans, where is it going for you? >> Got you, I’ve taken a lot of
online classes through Coursera. I think this Internet History class is
one of the only that I’ve completed. I’ve registered for many, but
taken other online courses, the gamification course is fantastic. There’s just so many opportunities to
learn almost anything in the world. It’s just fantastic. You can take them at your own leisure. Whenever I’m sitting at home bored,
I’ll just browse through what’s out there. And I love it. >> So do you think it has any chance
of replacing education as we know it? >> Probably not. >> Why?

I think there is a huge benefit to meeting in a classroom
environment where you can be face to face with fellow students and
instructors. There’s a lot of courses that it makes
sense to take on your own at home. But there are a lot of courses
that would be very difficult to achieve a high level of understanding. >> Is there anything that worries
you about this kind of movement, putting online education. No it’s definitely the way we should go. I love free online education, and
I hope Coursera keeps it free. I love it. >> Okay, let’s switch to Melanie. So hello Melanie,
tell us a little bit about yourself. >> Hi, I’m Melanie Abbot,
I live here in Los Angeles. I am a meeting and event planner. And so a nontechnical person, but
I’ve always been interested in technology, which is what drew me to Dr.
Chuck’s course, which is excellent. And it was my first Coursera course. >> So
are you planning on taking other classes? >> Oh, yeah. Like Mo, I’ve signed up for many. Have only completed this one so far, but
[LAUGH] I am a huge fan of free education. I think it’s brought learning to
the world, and it’s not going away. >> How do you think it would
fit into your education, if you were in college right this minute? >> If I was in college right now, I think it’s a great supplement to
whatever you’re already taking. I mean, it’s just a way to deepen
your knowledge on any topic. So do you think that it’s
a threat in any way of any kind of educational structures
that they’re building right now? >> Maybe monetarily. To the institution of education as a
business which I’m not really in favor of. That education has kind
of become a business. And I think that this is a way
to bring it back to what it really should be, learning. Education is the key to solve
all the world’s problems. >> All right, is there anything about the way it’s
being done that worries you at all? >> No, not worried. But I would like to see more of what
you’re doing, is like making it real. I think that making it feel like
it’s not just through the Internet, that this is real people in the world that
you can interact with and learn from. >> Do you, have you been listening
to the media talking about MOOCs? >> There was a special on NPR a couple
weeks ago and I did listen to it. And I was surprised how uninteresting it
was considering how fantastic Coursera and MOOCs are. I was really surprised that it didn’t do
a better job of pushing it onto people. >> Anthony,
do you have any closing thoughts? >> I encourage people
to check out Coursera. And I don’t, there’s this maybe talk
that it’s gonna replace education and it can solve all the problems with
remedial education and universities. I don’t think it can, but
it’s an excellent supplement. And if I were in college, I could imagine that I would be taking maybe one
course along with my other courses. Would I complete it? I don’t know. But I would definitely be interested. It’s just a lot of different
topics that have been discussed. Computer science,
the universe, programming. I hope that they keep on doing these
things and they keep it interesting, and they do encourage professors to
meet the people that they are teaching. >> I think there’s great
value in taking classes where the grades don’t matter, right? Because in college, you would choose. I only chose to take classes where I
knew I could get really good grades. >> [LAUGH]. >> Right, so, okay,
well let me turn this thing back at me. So, there you have it. Nice little report here from Santa Monica,
just after the Rose Bowl. So, we’ll see you on the net.

Video: Bonus: Class Reunion at Bletchley Park

Bletchley Park Office Hours: A Gathering of Python Enthusiasts

This video features Dr. Chuck Severance hosting a record-setting Office Hours session at Bletchley Park, the historical site of WWII codebreaking efforts. Students from various countries and backgrounds share their experiences with Dr. Chuck’s Python courses and their passion for programming.

Key takeaways:

  • Students come from diverse locations like France, China, Thailand, and the UK.
  • Many use Python for personal projects, teaching, and professional development.
  • Dr. Chuck emphasizes the historical significance of Bletchley Park and the contributions of women mathematicians.
  • The session fosters a sense of community and shared passion for learning.

Memorable moments:

  • A student traveled from Paris specifically for the event.
  • A wife and children are acknowledged through a video call.
  • A pensioner uses Python to complement his Raspberry Pi projects.
  • Dr. Chuck jokes about “cutting out” a competitor’s course mention.
  • The session concludes with applause and anticipation for future reunions.

Overall:

This video showcases the global reach and impact of online education platforms like Coursera. It highlights the positive learning experiences and connections fostered by Dr. Chuck’s courses and the unique setting of Bletchley Park.

Additional notes:

  • Steve, the course mentor, is acknowledged for his contributions.
  • Dr. Chuck expresses his hope to make Capstone projects more defined.
  • A student highlights the use of Python for analyzing nuclear reactor data.

[MUSIC] Well hello, we are at Bletchley Park
where computer science, Internet history, technology, and all of computation
started here at Bletchley Park. And we’ve had a very special,
record setting office hours. And I wanted you to meet
your fellow students. So here we go. So howdy. State your name and-

Hi. I’m Steve. >> Well also but you should say
that you’re somebody special. You’re not just Steve. >> Well I’m one of the mentors for
the Python course, all of them. [LAUGH]
[LAUGH] So welcome Steve. It’s an honor to have worked
with you all these years right? Steve is the one that
does all the hard work. I get all the credit and
people like Steve and other mentors they actually do
the hard work so thank you Steve. Thank you so much. >> Well thanks for inviting me down
with your help, it’s brilliant. >> Yeah, so
say hi to the rest of the class. >> Hi, I’m Benoit. I originated from France,
now I live here at London. I’m very happy to live here. And meet you Dr. Chuck. >> I’m happy to meet you. >> I’ve done the intern at history class. I really loved it. I’m really keen to do
the next one under Python. >> Cool. And don’t worry.
It’s a very wide angle. So I’m not really taking
a picture of your nose. >> [LAUGH]
Hopefully. Hi.
I’m Craig. I’m doing the [INAUDIBLE] and
web data and see you online. >> Okay.
Did you get in trouble when that guy come talk to you? >> Yeah. >> Really?
Yeah. >> You’re okay though? They’re going to let you come back? >> Yeah. [INAUDIBLE]
I’m Steve. I’ve done the first Python course and I’m
going to be doing some of the others soon, and I found it really, really useful in getting my way into
the language Rich, really helpful. >> Thanks. >> Hi, I’m Jenny, and
I’m a software developer. I use the Python for
Everybody course to teach a programming class at an international
school in ChangWei Thailand. And it’s good to be here at Park, England. >> You know that the book’s
translated into Chinese, right? Does that work in Thailand? >> Not so well. >> Oh okay.
See, I guess I’m not that cool so I- >> Different script. >> Hello. I’m Ano from France. I work in my university as a instructor
and because of the Python class, I forgot the syntax of that
was embarrassing in my job. >> You mean processing? You mean don’t use processing anymore? You use Python instead of processing? >> No, I use Arduino. I’m supposed to do the debugging
at the university but [INAUDIBLE] and Python [INAUDIBLE]. >> Well welcome,
did you travel from Paris? >> Yeah. >> You came from like, Paris to here? >> Yeah.
I- >> [LAUGH] [APPLAUSE] >> This is two hours. [INAUDIBLE] Right? >> I guess it is for Euro star. It’s not like coming from Australia. Right?
I guess it’s not. >> Oh, someone beat me, huh? >> No, no, no, I’m not saying. >> [LAUGH]
No, I’m not saying, you’re right. Euro star Paris is closer than we,
Paris is closer than we think. >> I’m Kevin, I’ve only come from London,
it’s been really good to hear how Coursera has been put together
and how you make the course and thank you. >> Well, thanks for coming. >> Hi, I’m Ji Yun. >> I’m Sapul. We are from China. >> So you’re students? >> We’re living here, yeah. >> Yeah, we are working here in the U.K.
at the moment. >> Okay. >> So nice to meet you here at Blatchley
park, and the Python crowd is great. >> Yeah. >> Cool. >> Hello, I’m Yanis. I used to work with Python for
a long time, but it’s the first time that I’m taking
a course, just to see the language proper. Thank you. >> Hello, I’m Isabel. I use your courses to refresh
my computer science knowledge. >> Yes. >> I’m learning Python with you. I’ve been come to Bletchley Park
at least five times. >> Like you, you’re a fan like me. >> Oh, I would have loved to
have worked here doing the war! >> Oh!
Must have been so exciting. >> Yeah, you know that three-quarters
of the people here were women? >> Yeah.
Yeah. Very impressive.
We don’t have the most interesting jobs out of them, but we-
Exactly, exactly. >> Yep. >> Hi, I’m Patrick,
I did the Internet history course, and it’s a very exciting day for
me today because when I did that course I said I wanted to go to
the Bletchley Park, and I’m here today. And I also said I wanted to come to
one of Doctor Chuck’s office hours, so I got to do two at once
today which is quite brilliant. >> Yeah, one of my ideas is
that this is our class reunion. >> Yeah. >> That was, I was thinking,
well we could have a class reunion. >> Hi, I’m David.
I’ve taken your Python courses and your course. Loving them. >> Welcome, glad you’re here. >> Hi, I’m Paul, and I’ve done
the Python courses and they’re great. >> Thank you. >> Okay.
Hi, I’m Hugh from Cambridge, and I have done your Python courses,
I’m currently in the web one. I’m very excited to be here with you,
and it’s great to try and teach kids. So, I’m actually learning it because
my son’s doing Python for his exam. So, hopefully I can help him. >> Great, you’re like only like
a couple of blocks away from Raspberry Pie headquarters. Right?
That’s right. >> Here in Cambridge. And they’re doing Python
stuff there too as well. >> Big time. >> Yeah.
Like to get that all connected somehow. >> [INAUDIBLE]
Yeah. >> Hi, I’m Roger. I’ve done the Python course,
and I’ve just thought of the. Really influence the game right through. >> Cool. >> Hi, I’m Dallers. I come originally from Holland,
but I live here in UK. I just started my journey with Python. I should say, thank you very much for
everything you’re doing. It’s really, really good stuff. Yes? >> So, here’s the question. Do you know the history of Bletchley Park
and the three Polish cryptographers? >> Yes.
Poles are very, very underrated. [INAUDIBLE]
Well, I bet everybody who’s historians here have done a really good
job of making that clear. >> Which brings me to the next question. When are you gonna come to the office
hours to [INAUDIBLE] a parent? I, we have a lot of friends there. >> I go many places, so
maybe we’ll figure that out. >> One more thing. >> Okay.
[LAUGH] >> Hello to my wife and two daughters. >> There you go. [LAUGH]
[LAUGH] Hi, I’m Matty Olive, just north of London. I took the first Python course so I could keep up with my little boy
who’s learning it at school, basically. >> Great.
It’s lovely to meet you. >> Lovely to meet you. >> Hello there, my name is Andrew. I’m a pensioner. Just doing the Python course for
interest and I’m hoping to combine that with little
projects I’ve got with the raspberry pie. >> Good. >> But I think the pie fund
cost is a gripping cost. >> There you go. >> Well done.
You’re very young for a pensioner. [LAUGH]
Indeed. >> [LAUGH]
To my seventys. >> Hello.
I’m Adam, I came with my family and I’ve just started to learn [INAUDIBLE]. [INAUDIBLE],
and later I’d like to teach my daughter programming, yeah. >> At some point that will become normal. Yessum? >> Just the next generation
of code breakers. >> Exactly. [LAUGH] >> It is important
to bring them to the ground zero. >> Ground zero. Ground zero of it all. >> Yeah.
And in the future it will be so much better. We break this ground and
it will stay broken. Definitely.
Yeah. >> Hi, I’m Alice and I live in Bristol and I-
Bristol, that’s far away, that’s that way. >> It’s a couple of hours southwest. >> Yeah. >> So I did the Python
course a few months ago and I’m going to do your new one,
the course three. >> Yeah, course three. Looking forward to seeing
you in course three. >> But yeah they’re really good. I tried to do the ones from [INAUDIBLE]
there wasn’t any one near as good. >> I’ll have to cut that out. No, maybe I won’t cut that part out. >> Hi.
I’m Yang. It’s nice to see you in real life finally. I’m an analyst so
I hope to be able to use your work and apply it to my work so I’ll follow
you to Capstone I’m pretty sure. >> Well great.
So keep up the good work. >> Well hopefully I’ll figure out the
Capstone by the time we get to Capstone. Hi. >> Hi.
I’m David. I’m from North of Liverpool. I work as a learning technologist and I’m interested in doing a Python course to
bring my programming skills up to date. Because they’re about 20 years old. >> I’m a learning technologist myself. >> Yeah. >> Hello. >> Hi. I’m Cynthia. >> Yeah? >> And I’m taking your Python course,
which is excellent. I highly recommend it for
anyone who wants to learn programming. And I’m on a career break right now,
raising Aiden and Tyco. >> Hello.
Hello. >> And it’s been great for
maintaining intellectual sanity. >> That is a use case that’s
very important, actually. But your career has something
to do with like nuclear reactors or-
No, no, no, no, no. >> Stealth aircraft? >> No, no, no. Used to but it’s math spectrometry. >> Oh math spectrometry. Whatever, I knew it was complex. >> So, without the computational part,
math spectometrists are useless. >> Well, math spectrometry is
a needle in the haystack problem. >> Yeah, but if it weren’t for
the programming, we couldn’t get any sense of that data and
I never knew how to do that before and I hope when I go back,
I’ll actually know what I’m doing. >> Well great, great! That’s a great way to do a career break. >> Hi, my name is Alfman, I’m from
London and I’ve done the Internet and history technology course and I’m looking
forward to doing the python course. >> Do you want to be on?
Or are you hiding? >> No, no, it’s just my wife. >> Oh okay, okay, you’re the photographer. Okay, so,
we’ll get a wide shot of you all. Give yourself a large round of applause
for your awesomeness and initiative. >> [APPLAUSE]
So there we have it, the first perhaps of many class
reunions here at Bletchley Park. So, see you on the net. [MUSIC]


Home » University of Michigan » Python for Everybody Specialization » Using Python to Access Web Data » Week 6: JSON and the REST Architecture (Chapter 13)