Skip to content

Latest commit

 

History

History
80 lines (65 loc) · 4.81 KB

Class2.md

File metadata and controls

80 lines (65 loc) · 4.81 KB

Git

What is it? https://www.nobledesktop.com/learn/git/what-is-git

GitHub

Online host for git repositories

The web

HTTP - Hypertext Transfer Protocol

Used to communicate with browsers but in today's world, it's used for so much more

HTTPS - Secure HTTP

data is ecrypted between the web server and the browser

URL

To get to things on the web, we use URLs (Uniform Resource Locators). When we want to get to something using HTTP, the URL starts with http: or https: The format of a url is protocol://server-name/path["?" query]

Request Methods

HTTP specifies "request methods". Right now we're only concerned with "GET" and "POST". Normally, when a browser makes a request to a web server, it's making a "GET" request. This request method should only get data and have no other side effects. When you submit a form on a web page, it most often gets sent to the web server as a "POST" request. A POST request contains data coming from the client (web-browser) that gets sent to the web server. The web server then takes that information and does something with it. For example, you might "POST" a message which has some text in it, and the server takes that and stores the text in a database.

Web Server

A program/application that runs on a computer that responds to web requests. For our purposes, the application responds to HTTP requests. We're using a library called Flask that allows us to define how our web server will actually work. It also provides a way to run the web server so you can hit it with your browser or another tool.

APIs - Application Programming Interface

https://en.wikipedia.org/wiki/API An API is a contract that lists a series of queries or actions that a user of the API can take. For example - "get messages" (query) or "post a message" (action).

In our case, we're building an API using the HTTP protocol and use "endpoints" or, in HTTP speak, "paths" to do different things. For example http://localhost:8080/get-messages/<user-id> is an endpoint that says "make an http request to a server called localhost (on port 8080, but we can get into that later)" The server sees the path get-messages/<user-id> and reads that as "get messages for user id 'user-id'"

The endpoint in this case is get-messages (and incidentally the request method is "GET")

Back to Python

In our app.py file, we're defining what paths the web server should respond to and how. The way we do that with Flask is to "decorate" methods with path information using the @app.route decorator. That decorator provides the http path to respond to, and when we're running our web server, Flask "routes" the incoming requests to the appropriate function.

Dictionaries

When the server receives an incoming message, it needs a way to save that message for later retrieval. I chose to use a really simple structure to do this called a dictionary. A dictionary in Python is just a key / value object or type of variable. You put things into a dictionary by specifying a key (in our case, the user name) and a value (in our case, a list of messages). You retreive things from a dictionary by asking for them by key.

# initialize the dictionary
my_dict = {}
# put 'value' into the dictionary using the key 'key'
my_dict['key'] = 'value'
# print the value stored in the dictionary under they key 'key'
print(my_dict['key'])

When you try to retrieve a key from a dictionary that doesn't exist, python throws an error. Luckily there's a helper function on dictionaries that allows you to specify a default if the key doesn't exist - get().

# this will throw an error
print(my_dict['not_key'])
# using get, we can avoid the error and have a default value
print(my_dict.get('not_key', 'default value'))

Lists

A list in python is a collection of things that have an "index" or numeric location value.

# create a list that has a couple of values
my_list = ['hi', 'there']
# to get at a value, we use its numeric location (starting with 0)
# this will print 'hi'
print(my_list[0])

Defaultdict

In order to make my life easier for storing messages, I used a utility class called defaultdict to store my messages. A default dict will automatically create an entry in a dictionary for a key if it doesn't exist.

# default dict isn't part of the main python language, so I have to import it
from collections import defaultdict

# here I'm creating a default dict with a type of list. If I reference a key that doesn't exist, it automatically creates an empty list for that key
messages = defaultdict(list)
# foo will be an empty list
foo = messages['hi']
# I can just add things directly to that list
# This will create an empty list, add the value 'there' to it, and store it in the messages dictionary under the key 'hi'.
messages['hi'].append('there')
# messages looks like this when we're done
# { 'hi': [ 'there' ] }