66import syncano
77from syncano .exceptions import (
88 SyncanoValueError , SyncanoRequestError ,
9+ SyncanoValidationError
910)
1011from syncano .resultset import ResultSet
12+ from syncano .models import Registry
1113
1214
1315class Connection (object ):
14- AUTH_SUFFIX = 'account/auth/'
16+ AUTH_SUFFIX = 'v1/account/auth'
17+ SCHEMA_SUFFIX = 'v1/schema'
1518 CONTENT_TYPE = 'application/json'
1619 RESULT_SET_CLASS = ResultSet
1720
@@ -23,6 +26,7 @@ def __init__(self, host=None, email=None, password=None, api_key=None, **kwargs)
2326 self .logger = kwargs .get ('logger' ) or syncano .logger
2427 self .timeout = kwargs .get ('timeout' ) or 30
2528 self .session = requests .Session ()
29+ self .models = Registry (self ).register_schema (self .schema )
2630
2731 def build_params (self , kwargs ):
2832 params = deepcopy (kwargs )
@@ -79,7 +83,7 @@ def request(self, method_name, path, **kwargs):
7983 return ResultClass (** content ) if ResultClass else content
8084
8185 def make_request (self , method_name , path , ** kwargs ):
82- self .logger .debug ('Request: %s' , path )
86+ self .logger .debug ('Request: %s %s' , method_name , path )
8387
8488 params = self .build_params (kwargs )
8589 method = getattr (self .session , method_name .lower (), None )
@@ -92,6 +96,11 @@ def make_request(self, method_name, path, **kwargs):
9296 has_json = response .headers .get ('content-type' ) == 'application/json'
9397 content = response .json () if has_json else response .text
9498
99+ if response .status_code == 400 :
100+ for name , errors in content .iteritems ():
101+ errors = ', ' .join (errors )
102+ raise SyncanoValidationError ('"{0}": {1}.' .format (name , errors ))
103+
95104 if response .status_code not in [200 , 201 ]:
96105 content = content ['detail' ] if has_json else content
97106 self .logger .debug ('Request Error: %s' , url )
@@ -101,6 +110,13 @@ def make_request(self, method_name, path, **kwargs):
101110
102111 return content
103112
113+ @property
114+ def schema (self ):
115+ if not hasattr (self , '_schema' ):
116+ response = self .make_request ('GET' , self .SCHEMA_SUFFIX )
117+ self ._schema = response
118+ return self ._schema
119+
104120 def is_authenticated (self ):
105121 return self .api_key is not None
106122
0 commit comments