Skip to content

Commit 54fa132

Browse files
committed
ORM draft
1 parent 411dd47 commit 54fa132

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ dist
66
*.pyo
77
.coverage
88
coverage
9+
run_it.py

syncano/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
__copyright__ = 'Copyright 2015 Syncano'
99

1010
VERSION = __version__
11-
API_ROOT = 'http://127.0.0.1:8000/api/'
11+
API_ROOT = os.getenv('SYNCANO_API_ROOT', 'https://api.syncano.com/')
1212

1313
env_loglevel = os.getenv('SYNCANO_LOGLEVEL', 'INFO')
1414
loglevel = getattr(logging, env_loglevel.upper(), None)

syncano/connection.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import syncano
77
from syncano.exceptions import (
88
SyncanoValueError, SyncanoRequestError,
9+
SyncanoValidationError
910
)
1011
from syncano.resultset import ResultSet
12+
from syncano.models import Registry
1113

1214

1315
class 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

syncano/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ def __repr__(self):
2929

3030
def __str__(self):
3131
return '{0} {1}'.format(self.status_code, self.reason)
32+
33+
34+
class SyncanoValidationError(SyncanoValueError):
35+
pass

0 commit comments

Comments
 (0)