2012-02-07 03:55:05 -08:00
import httplib
import urllib
import json
from connection import *
from auth import *
from errors import *
2012-02-07 19:15:28 -08:00
from models import *
2012-02-07 03:55:05 -08:00
class API ( object ) :
def __init__ ( self , host = ' api.fastly.com ' , secure = True , port = None , root = ' ' ,
2012-02-09 16:29:35 -08:00
timeout = 5.0 , key = None ) :
2012-02-07 03:55:05 -08:00
self . conn = Connection ( host , secure , port , root , timeout )
2012-02-09 16:29:35 -08:00
if key :
self . authenticate_by_key ( key )
2012-02-07 03:55:05 -08:00
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
2012-02-07 19:15:28 -08:00
def service ( self , id ) :
2012-02-09 16:29:35 -08:00
return Service . find ( self . conn , id = id )
2012-02-07 19:15:28 -08:00
def version ( self , service_id , version ) :
return Version . find ( self . conn , service_id = service_id , number = version )
def domain ( self , service_id , version , name ) :
return Domain . find ( self . conn , service_id = service_id , version = version , name = name )
def backend ( self , service_id , version , name ) :
return Backend . find ( self . conn , service_id = service_id , version = version , name = name )
2012-02-07 03:55:05 -08:00
def purge_url ( self , host , path ) :
2012-02-07 19:15:28 -08:00
resp , data = self . conn . request ( ' PURGE ' , path , headers = { ' Host ' : host } )
2012-02-07 03:55:05 -08:00
return resp . status == 200
2013-05-20 13:48:24 -07:00
def purge_service ( self , service ) :
resp , data = self . conn . request ( ' POST ' , ' /service/ %s /purge_all ' % service )
return resp . status == 200
def purge_key ( self , service , key ) :
resp , data = self . conn . request ( ' POST ' , ' /service/ %s /purge/ %s ' % ( service , key ) )
return resp . status == 200