commit 2435affbc8cb56f17ffc5e5937f536bc58db66bc
Author: Tyler McMullen <tbmcmullen@gmail.com>
Date:   Tue Feb 7 03:55:05 2012 -0800

    initial

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7e99e36
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.pyc
\ No newline at end of file
diff --git a/README b/README
new file mode 100644
index 0000000..29f1f5b
--- /dev/null
+++ b/README
@@ -0,0 +1,9 @@
+Fastly Python Client
+
+TODO:
+Doc files
+Docstrings
+Models
+Config file
+
+
diff --git a/fastly/__init__.py b/fastly/__init__.py
new file mode 100644
index 0000000..244583d
--- /dev/null
+++ b/fastly/__init__.py
@@ -0,0 +1,11 @@
+"""
+Durp.
+"""
+
+from fastly import *
+
+__author__ = 'Tyler McMullen <tbmcmullen@gmail.com>'
+__copyright__ = 'Copyright (c) 2012 Fastly Inc'
+__license__ = 'BSD'
+__version__ = '0.0.1'
+__url__ = 'http://www.fastly.com/docs/fastly-py'
diff --git a/fastly/auth.py b/fastly/auth.py
new file mode 100644
index 0000000..ce994e7
--- /dev/null
+++ b/fastly/auth.py
@@ -0,0 +1,22 @@
+"""
+"""
+
+from errors import *
+
+import urllib
+
+class KeyAuthenticator(object):
+    def __init__(self, key):
+        self.key = key
+
+    def add_auth(self, headers):
+        headers['X-Fastly-Key'] = self.key
+
+class SessionAuthenticator(object):
+    def __init__(self, conn, login, password):
+        body = urllib.urlencode({ 'user': login, 'password': password })
+        resp, data = conn.request('POST', '/login', body)
+        self.session_key = resp.getheader('Set-Cookie')
+
+    def add_auth(self, headers):
+        headers['Cookie'] = self.session_key
diff --git a/fastly/connection.py b/fastly/connection.py
new file mode 100644
index 0000000..7a94031
--- /dev/null
+++ b/fastly/connection.py
@@ -0,0 +1,50 @@
+"""
+"""
+
+import httplib
+import json
+
+from errors import *
+
+class Connection(object):
+    def __init__(self, host='api.fastly.com', secure=True, port=None, root='',
+                 timeout=10.0):
+        self.host = host
+        self.secure = secure
+        self.port = port
+        self.root = root
+        self.timeout = timeout
+
+        self.authenticator = None
+        self.http_conn = None
+    
+    def request(self, method, path, body=None, headers={}):
+        if not self.port:
+            self.port = 443 if self.secure else 80
+
+        if self.secure:
+            self.http_conn = httplib.HTTPSConnection(self.host, self.port,
+                                           timeout=self.timeout)
+        else:
+            self.http_conn = httplib.HTTPConnection(self.host, self.port,
+                                          timeout=self.timeout)
+
+        if self.authenticator:
+            self.authenticator.add_auth(headers)
+        
+        self.http_conn.request(method, self.root + path, body, headers=headers)
+        response = self.http_conn.getresponse()
+        body = response.read()
+        try:
+            data = json.loads(body)
+        except ValueError:
+            data = body
+
+        if response.status == 403:
+            raise AuthenticationError()
+        elif response.status == 500:
+            raise InternalServerError()
+        elif response.status == 400:
+            raise BadRequestError()
+
+        return (response, data)
diff --git a/fastly/errors.py b/fastly/errors.py
new file mode 100644
index 0000000..bf9c5a2
--- /dev/null
+++ b/fastly/errors.py
@@ -0,0 +1,11 @@
+"""
+"""
+
+class AuthenticationError(Exception):
+    pass
+
+class InternalServerError(Exception):
+    pass
+
+class BadRequestError(Exception):
+    pass
diff --git a/fastly/fastly.py b/fastly/fastly.py
new file mode 100644
index 0000000..03b9745
--- /dev/null
+++ b/fastly/fastly.py
@@ -0,0 +1,28 @@
+import httplib
+import urllib
+import json
+
+from connection import *
+from auth import *
+from errors import *
+
+class API(object):
+    def __init__(self, host='api.fastly.com', secure=True, port=None, root='',
+                 timeout=10.0):
+        self.conn = Connection(host, secure, port, root, timeout)
+
+
+    def authenticate_by_key(self, key):
+        self.conn.authenticator = KeyAuthenticator(key)
+
+    def authenticate_by_password(self, login, password):
+        self.conn.authenticator = SessionAuthenticator(self.conn, login, password)
+
+    def deauthenticate(self):
+        self.conn.authenticator = None
+
+
+    def purge_url(self, host, path):
+        resp, data = self.conn.request('PURGE', path, headers={ 'Host': host })
+        return resp.status == 200
+
diff --git a/test/api_test.py b/test/api_test.py
new file mode 100644
index 0000000..6abfce6
--- /dev/null
+++ b/test/api_test.py
@@ -0,0 +1,27 @@
+import unittest
+import fastly
+
+class APITest(unittest.TestCase):
+    def setUp(self):
+        self.api = fastly.API()#host='127.0.0.1', port=5500, secure=False)
+
+    def test_purge(self):
+        self.assertTrue(self.api.purge_url('test.com', '/'))
+
+    def test_auth_error(self):
+        self.api.deauthenticate()
+        with self.assertRaises(fastly.AuthenticationError):
+            self.api.conn.request('GET', '/current_customer')
+
+    def test_auth_key_success(self):
+        self.api.deauthenticate()
+        self.api.authenticate_by_key('TESTAPIKEY')
+        self.api.conn.request('GET', '/current_customer')
+
+    def test_auth_session_success(self):
+        self.api.deauthenticate()
+        self.api.authenticate_by_password('foo@test.com', 'password')
+        self.api.conn.request('GET', '/current_customer')
+
+if __name__ == '__main__':
+    unittest.main()