Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0fc71d1
Showing
164 changed files
with
35,469 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.swp | ||
*.db | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
.sass-cache/ | ||
progress/ |
Oops, something went wrong.