<s> '''<STR_LIT>''' <EOL> from corepost import Response , NotFoundException , AlreadyExistsException <EOL> from corepost . web import RESTResource , route , Http <EOL> from twisted . cred . portal import IRealm , Portal <EOL> from twisted . cred . checkers import FilePasswordDB <EOL> from twisted . web . static import File <EOL> from twisted . web . resource import IResource <EOL> from twisted . web . guard import HTTPAuthSessionWrapper , BasicCredentialFactory <EOL> from zope . interface import implements <EOL> class DB ( ) : <EOL> """<STR_LIT>""" <EOL> customers = { } <EOL> @ classmethod <EOL> def getAllCustomers ( cls ) : <EOL> return DB . customers . values ( ) <EOL> @ classmethod <EOL> def getCustomer ( cls , customerId ) : <EOL> if customerId in DB . customers : <EOL> return DB . customers [ customerId ] <EOL> else : <EOL> raise NotFoundException ( "<STR_LIT>" , customerId ) <EOL> @ classmethod <EOL> def saveCustomer ( cls , customer ) : <EOL> if customer . customerId in DB . customers : <EOL> raise AlreadyExistsException ( "<STR_LIT>" , customer . customerId ) <EOL> else : <EOL> DB . customers [ customer . customerId ] = customer <EOL> @ classmethod <EOL> def deleteCustomer ( cls , customerId ) : <EOL> if customerId in DB . customers : <EOL> del ( DB . customers [ customerId ] ) <EOL> else : <EOL> raise NotFoundException ( "<STR_LIT>" , customerId ) <EOL> @ classmethod <EOL> def deleteAllCustomers ( cls ) : <EOL> DB . customers . clear ( ) <EOL> @ classmethod <EOL> def getCustomerAddress ( cls , customerId , addressId ) : <EOL> c = DB . getCustomer ( customerId ) <EOL> if addressId in c . addresses : <EOL> return c . addresses [ addressId ] <EOL> else : <EOL> raise NotFoundException ( "<STR_LIT>" , addressId ) <EOL> class Customer : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , customerId , firstName , lastName ) : <EOL> ( self . customerId , self . firstName , self . lastName ) = ( customerId , firstName , lastName ) <EOL> self . addresses = { } <EOL> class CustomerAddress : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , streetNumber , streetName , stateCode , countryCode ) : <EOL> ( self . streetNumber , self . streetName , self . stateCode , self . countryCode ) = ( streetNumber , streetName , stateCode , countryCode ) <EOL> class CustomerRESTService ( ) : <EOL> path = "<STR_LIT>" <EOL> @ route ( "<STR_LIT:/>" ) <EOL> def getAll ( self , request ) : <EOL> return DB . getAllCustomers ( ) <EOL> @ route ( "<STR_LIT>" ) <EOL> def get ( self , request , customerId ) : <EOL> return DB . getCustomer ( customerId ) <EOL> @ route ( "<STR_LIT:/>" , Http . POST ) <EOL> def post ( self , request , customerId , firstName , lastName ) : <EOL> customer = Customer ( customerId , firstName , lastName ) <EOL> DB . saveCustomer ( customer ) <EOL> return Response ( <NUM_LIT> ) <EOL> @ route ( "<STR_LIT>" , Http . PUT ) <EOL> def put ( self , request , customerId , firstName , lastName ) : <EOL> c = DB . getCustomer ( customerId ) <EOL> ( c . firstName , c . lastName ) = ( firstName , lastName ) <EOL> return Response ( <NUM_LIT:200> ) <EOL> @ route ( "<STR_LIT>" , Http . DELETE ) <EOL> def delete ( self , request , customerId ) : <EOL> DB . deleteCustomer ( customerId ) <EOL> return Response ( <NUM_LIT:200> ) <EOL> @ route ( "<STR_LIT:/>" , Http . DELETE ) <EOL> def deleteAll ( self , request ) : <EOL> DB . deleteAllCustomers ( ) <EOL> return Response ( <NUM_LIT:200> ) <EOL> class CustomerAddressRESTService ( ) : <EOL> path = "<STR_LIT>" <EOL> @ route ( "<STR_LIT:/>" ) <EOL> def getAll ( self , request , customerId ) : <EOL> return DB . getCustomer ( customerId ) . addresses <EOL> @ route ( "<STR_LIT>" ) <EOL> def get ( self , request , customerId , addressId ) : <EOL> return DB . getCustomerAddress ( customerId , addressId ) <EOL> @ route ( "<STR_LIT:/>" , Http . POST ) <EOL> def post ( self , request , customerId , addressId , streetNumber , streetName , stateCode , countryCode ) : <EOL> c = DB . getCustomer ( customerId ) <EOL> address = CustomerAddress ( streetNumber , streetName , stateCode , countryCode ) <EOL> c . addresses [ addressId ] = address <EOL> return Response ( <NUM_LIT> ) <EOL> @ route ( "<STR_LIT>" , Http . PUT ) <EOL> def put ( self , request , customerId , addressId , streetNumber , streetName , stateCode , countryCode ) : <EOL> address = DB . getCustomerAddress ( customerId , addressId ) <EOL> ( address . streetNumber , address . streetName , address . stateCode , address . countryCode ) = ( streetNumber , streetName , stateCode , countryCode ) <EOL> return Response ( <NUM_LIT:200> ) <EOL> @ route ( "<STR_LIT>" , Http . DELETE ) <EOL> def delete ( self , request , customerId , addressId ) : <EOL> DB . getCustomerAddress ( customerId , addressId ) <EOL> del ( DB . getCustomer ( customerId ) . addresses [ addressId ] ) <EOL> return Response ( <NUM_LIT:200> ) <EOL> @ route ( "<STR_LIT:/>" , Http . DELETE ) <EOL> def deleteAll ( self , request , customerId ) : <EOL> c = DB . getCustomer ( customerId ) <EOL> c . addresses = { } <EOL> return Response ( <NUM_LIT:200> ) <EOL> def run_rest_app ( ) : <EOL> app = RESTResource ( ( CustomerRESTService ( ) , CustomerAddressRESTService ( ) ) ) <EOL> app . run ( <NUM_LIT> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_rest_app ( ) </s>
