<s> from atc_api . atcd_client import atcdClient <EOL> from atc_api . serializers import SettingSerializer , DeviceSerializer <EOL> from atc_api . settings import atc_api_settings <EOL> from atc_api . utils import get_client_ip <EOL> from atc_thrift . ttypes import TrafficControlException , TrafficControl <EOL> from atc_thrift . ttypes import TrafficControlledDevice , AccessToken <EOL> from functools import wraps <EOL> from rest_framework . exceptions import APIException <EOL> from rest_framework . exceptions import ParseError <EOL> from rest_framework . response import Response <EOL> from rest_framework . views import APIView <EOL> from rest_framework import status <EOL> class BadGateway ( APIException ) : <EOL> status_code = <NUM_LIT> <EOL> default_detail = '<STR_LIT>' <EOL> def serviced ( method ) : <EOL> '''<STR_LIT>''' <EOL> @ wraps ( method ) <EOL> def decorator ( cls , request , * args , ** kwargs ) : <EOL> service = atcdClient ( ) <EOL> if service is None : <EOL> raise BadGateway ( ) <EOL> return method ( cls , request , service , * args , ** kwargs ) <EOL> return decorator <EOL> class AtcApi ( APIView ) : <EOL> '''<STR_LIT>''' <EOL> @ serviced <EOL> def get ( self , request , service , address = None , format = None ) : <EOL> '''<STR_LIT>''' <EOL> device_serializer = DeviceSerializer ( <EOL> data = request . data , <EOL> context = { '<STR_LIT>' : request , '<STR_LIT:address>' : address } , <EOL> ) <EOL> if not device_serializer . is_valid ( ) : <EOL> raise ParseError ( detail = device_serializer . errors ) <EOL> dev = device_serializer . save ( ) <EOL> try : <EOL> tc = service . getCurrentShaping ( dev ) <EOL> except TrafficControlException as e : <EOL> return Response ( <EOL> { '<STR_LIT>' : e . message } , <EOL> status = status . HTTP_404_NOT_FOUND , <EOL> ) <EOL> serializer = SettingSerializer ( tc . settings ) <EOL> return Response ( <EOL> serializer . data , <EOL> status = status . HTTP_200_OK <EOL> ) <EOL> @ serviced <EOL> def post ( self , request , service , address = None , format = None ) : <EOL> '''<STR_LIT>''' <EOL> setting_serializer = SettingSerializer ( data = request . data ) <EOL> device_serializer = DeviceSerializer ( <EOL> data = request . data , <EOL> context = { '<STR_LIT>' : request , '<STR_LIT:address>' : address } , <EOL> ) <EOL> if not setting_serializer . is_valid ( ) : <EOL> raise ParseError ( detail = setting_serializer . errors ) <EOL> if not device_serializer . is_valid ( ) : <EOL> raise ParseError ( detail = device_serializer . errors ) <EOL> setting = setting_serializer . save ( ) <EOL> device = device_serializer . save ( ) <EOL> tc = TrafficControl ( <EOL> device = device , <EOL> settings = setting , <EOL> timeout = atc_api_settings . DEFAULT_TC_TIMEOUT , <EOL> ) <EOL> try : <EOL> tcrc = service . startShaping ( tc ) <EOL> except TrafficControlException as e : <EOL> return Response ( e . message , status = status . HTTP_401_UNAUTHORIZED ) <EOL> result = { '<STR_LIT:result>' : tcrc . code , '<STR_LIT:message>' : tcrc . message } <EOL> if tcrc . code : <EOL> raise ParseError ( detail = result ) <EOL> return Response ( <EOL> setting_serializer . data , <EOL> status = status . HTTP_201_CREATED <EOL> ) <EOL> @ serviced <EOL> def delete ( self , request , service , address = None , format = None ) : <EOL> '''<STR_LIT>''' <EOL> device_serializer = DeviceSerializer ( <EOL> data = request . data , <EOL> context = { '<STR_LIT>' : request , '<STR_LIT:address>' : address } , <EOL> ) <EOL> if not device_serializer . is_valid ( ) : <EOL> return Response ( <EOL> device_serializer . errors , <EOL> status = status . HTTP_400_BAD_REQUEST , <EOL> ) <EOL> device = device_serializer . save ( ) <EOL> try : <EOL> tcrc = service . stopShaping ( device ) <EOL> except TrafficControlException as e : <EOL> return Response ( e . message , status = status . HTTP_401_UNAUTHORIZED ) <EOL> result = { '<STR_LIT:result>' : tcrc . code , '<STR_LIT:message>' : tcrc . message } <EOL> if tcrc . code : <EOL> raise ParseError ( detail = result ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) <EOL> class AuthApi ( APIView ) : <EOL> @ serviced <EOL> def get ( self , request , service , address = None ) : <EOL> '''<STR_LIT>''' <EOL> if address is None : <EOL> address = get_client_ip ( request ) <EOL> controlled_ips = [ ] <EOL> for addr in service . getDevicesControlledBy ( address ) : <EOL> if addr is None : <EOL> break <EOL> controlled_ips . append ( { <EOL> '<STR_LIT>' : addr . device . controlledIP , <EOL> '<STR_LIT>' : addr . timeout , <EOL> } ) <EOL> data = { <EOL> '<STR_LIT:address>' : address , <EOL> '<STR_LIT>' : controlled_ips , <EOL> } <EOL> return Response ( data , status = status . HTTP_200_OK ) <EOL> @ serviced <EOL> def post ( self , request , service , address = None ) : <EOL> '''<STR_LIT>''' <EOL> if address is None : <EOL> return Response ( <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> controlled_ip = address <EOL> controlling_ip = get_client_ip ( request ) <EOL> if '<STR_LIT>' not in request . data : <EOL> token = None <EOL> else : <EOL> token = AccessToken ( token = request . data [ '<STR_LIT>' ] ) <EOL> dev = TrafficControlledDevice ( <EOL> controlledIP = controlled_ip , <EOL> controllingIP = controlling_ip <EOL> ) <EOL> worked = service . requestRemoteControl ( dev , token ) <EOL> if not worked : <EOL> return Response ( <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> status = status . HTTP_401_UNAUTHORIZED , <EOL> ) <EOL> print '<STR_LIT>' , worked <EOL> data = { <EOL> '<STR_LIT>' : controlling_ip , <EOL> '<STR_LIT>' : controlled_ip , <EOL> } <EOL> return Response ( data , status = status . HTTP_200_OK ) <EOL> class TokenApi ( APIView ) : <EOL> @ serviced <EOL> def get ( self , request , service ) : <EOL> '''<STR_LIT>''' <EOL> duration = <NUM_LIT:3> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> if '<STR_LIT>' in request . query_params : <EOL> duration = int ( request . query_params [ '<STR_LIT>' ] ) <EOL> address = get_client_ip ( request ) <EOL> stuff = service . requestToken ( address , duration ) <EOL> data = { <EOL> '<STR_LIT>' : stuff . token , <EOL> '<STR_LIT>' : stuff . interval , <EOL> '<STR_LIT>' : stuff . valid_until , <EOL> '<STR_LIT:address>' : address , <EOL> } <EOL> return Response ( data , status = status . HTTP_200_OK ) </s>
