Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
rfk committed Aug 15, 2012
0 parents commit 0fc71d1
Show file tree
Hide file tree
Showing 164 changed files with 35,469 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
*.swp
*.db
*.pyc
15 changes: 15 additions & 0 deletions README.rst
@@ -0,0 +1,15 @@

The Lazy Dev's Guide to Testing Your Web API
============================================

This is the source material, slides, code samples etc for my PyCon Au 2012
presentation title "The Lazy Dev's Guide to Testing Your Web API".

The content of the talk is available under the Creative Commons Attribution-
ShareAlike 3.0 licence:

http://creativecommons.org/licenses/by-sa/3.0/

The slide presentation is constructed from a variety of software that has a
variety of open-source licenses applied. The most restrictive such license is
the GPL v2. See individual code directories and files for details.
63 changes: 63 additions & 0 deletions code/syncserver.py
@@ -0,0 +1,63 @@

import os
import time
import sqlite3

from bottle import Bottle, request, response, run


application = Bottle()


def connect_to_database(event):
raise ValueError("HERE")
db = sqlite3.connect(path.join(path.dirname(__file__), "sync.db"))
db.execute("CREATE TABLE IF NOT EXISTS items ("\
" username STRING NOT NULL, "\
" collection STRING NOT NULL, "\
" item STRING NOT NULL, "\
" payload STRING NOT NULL, "\
" modified INTEGER NOT NULL, "\
"PRIMARY KEY (username, collection, item))")


def connect_to_database(event):
request = event.request
request.db = sqlite3.connect(path.join(path.dirname(__file__), "sync.db"))
request.add_finished_callback(disconnect_from_database)


def disconnect_from_database(request):
request.db.close()


@application.get("/<username>/<collection>")
def get_collection(username, collection):
query = "SELECT * FROM items WHERE username=:username "\
"AND collection=:collection and modified > :newer"
params = {
"username": username,
"collection": collection,
"newer": int(request.query.newer or 0)
}
items = [request.db.execute(query, params)]
return {
"items": [{"id": r["item"], "payload": r["payload"]} for r in rows]
}


@application.post("/<username>/<collection>")
def post_collection(request):
query = "INSERT INTO items VALUES "\
"(:username, :collection, :item, :payload, :modified) "
params = request.matchdict.copy()
params["modified"] = int(time.time())
for item in json.loads(request.body):
params["item"] = item["id"]
params["payload"] = item["payload"]
request.db.execute(query, params)
response.set_header("X-Last-Modified", str(params["modified"]))
return None

if __name__ == "__main__":
run(host="localhost", port=8080)
72 changes: 72 additions & 0 deletions code/syncserver.pyramid.py
@@ -0,0 +1,72 @@

import time
import sqlite3

from os import path

from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.events import subscriber, ApplicationCreated, NewRequest


@subscriber(ApplicationCreated)
def connect_to_database(event):
raise ValueError("HERE")
db = sqlite3.connect(path.join(path.dirname(__file__), "sync.db"))
db.execute("CREATE TABLE IF NOT EXISTS items ("\
" username STRING NOT NULL, "\
" collection STRING NOT NULL, "\
" item STRING NOT NULL, "\
" payload STRING NOT NULL, "\
" modified INTEGER NOT NULL, "\
"PRIMARY KEY (username, collection, item))")


@subscriber(NewRequest)
def connect_to_database(event):
request = event.request
request.db = sqlite3.connect(path.join(path.dirname(__file__), "sync.db"))
request.add_finished_callback(disconnect_from_database)


def disconnect_from_database(request):
request.db.close()


@view_config(route_name="collection", request_method="GET")
def get_collection(request):
query = "SELECT * FROM items WHERE username=:username "\
"AND collection=:collection and modified > :newer"
params = request.matchdict.copy()
params["newer"] = int(request.GET.get("newer", "0"))
items = [request.db.execute(query, params)]
return {
"items": [{"id": r["item"], "payload": r["payload"]} for r in rows]
}


@view_config(route_name="collection", request_method="POST")
def post_collection(request):
query = "INSERT INTO items VALUES "\
"(:username, :collection, :item, :payload, :modified) "
params = request.matchdict.copy()
params["modified"] = int(time.time())
for item in json.loads(request.body):
params["item"] = item["id"]
params["payload"] = item["payload"]
request.db.execute(query, params)


def make_wsgi_app():
config = Configurator()
config.add_route("collection", "{userid}/{collection}")
config.scan("syncserver")
return config.make_wsgi_app()


if __name__ == '__main__':
from wsgiref.simple_server import make_server
app = make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
3 changes: 3 additions & 0 deletions resources/deck-js/.gitignore
@@ -0,0 +1,3 @@
.DS_Store
.sass-cache/
progress/

0 comments on commit 0fc71d1

Please sign in to comment.