initial
This commit is contained in:
commit
2435affbc8
8 changed files with 159 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
*.pyc
|
9
README
Normal file
9
README
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
Fastly Python Client
|
||||||
|
|
||||||
|
TODO:
|
||||||
|
Doc files
|
||||||
|
Docstrings
|
||||||
|
Models
|
||||||
|
Config file
|
||||||
|
|
||||||
|
|
11
fastly/__init__.py
Normal file
11
fastly/__init__.py
Normal file
|
@ -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'
|
22
fastly/auth.py
Normal file
22
fastly/auth.py
Normal file
|
@ -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
|
50
fastly/connection.py
Normal file
50
fastly/connection.py
Normal file
|
@ -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)
|
11
fastly/errors.py
Normal file
11
fastly/errors.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
|
||||||
|
class AuthenticationError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class InternalServerError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class BadRequestError(Exception):
|
||||||
|
pass
|
28
fastly/fastly.py
Normal file
28
fastly/fastly.py
Normal file
|
@ -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
|
||||||
|
|
27
test/api_test.py
Normal file
27
test/api_test.py
Normal file
|
@ -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()
|
Reference in a new issue