Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Start v1 refactor. Kill validators. Add mixins (Createable, Findable).
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonSanford committed Oct 6, 2014
1 parent 6c73547 commit 8745474
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 423 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ htmlcov
.mr.developer.cfg
.project
.pydevproject
go.py
41 changes: 5 additions & 36 deletions fulcrum/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import requests

from .exceptions import InvalidAPIVersionException, NotFoundException, UnauthorizedException, InvalidObjectException, InternalServerErrorException
from .validators import FormValidator, RecordValidator, WebhookValidator
from .exceptions import InvalidAPIVersionException, NotFoundException, UnauthorizedException, InternalServerErrorException
from .mixins import Findable, Deleteable, Createable, Searchable, Updateable

supported_versions = [2]

Expand All @@ -29,26 +29,6 @@ class BaseAPI(object):
def __init__(self, api_config):
self.api_config = api_config

def all(self, params=None):
api_resp = self.call('get', self.path, params=params)
return api_resp

def create(self, obj):
if hasattr(self, 'validator_class'):
self.validator = self.validator_class(obj)
if not self.validator.valid:
raise InvalidObjectException(self.validator)
api_resp = self.call('post', self.path, data=obj, extra_headers={'Content-Type': 'application/json'})
return api_resp


def delete(self, id):
self.call('delete', '{0}/{1}'.format(self.path, id))

def find(self, id):
api_resp = self.call('get', '{0}/{1}'.format(self.path, id))
return api_resp

def call(self, method, path, data=None, extra_headers=None, params=None):
full_path = self.api_config.api_root + path
headers = {'X-ApiToken': self.api_config.key}
Expand All @@ -71,25 +51,14 @@ def call(self, method, path, data=None, extra_headers=None, params=None):
if method != 'delete':
return resp.json()

def update(self, id, obj):
if hasattr(self, 'validator_class'):
self.validator = self.validator_class(obj)
if not self.validator.valid:
raise InvalidObjectException(self.validator)
api_resp = self.call('put', '{0}/{1}'.format(self.path, id), data=obj, extra_headers={'Content-Type': 'application/json'})
return api_resp


class Form(BaseAPI):
class Form(BaseAPI, Findable, Deleteable, Createable, Searchable, Updateable):
path = '/forms'
validator_class = FormValidator


class Record(BaseAPI):
class Record(BaseAPI, Findable, Deleteable, Createable, Searchable, Updateable):
path = '/records'
validator_class = RecordValidator


class Webhook(BaseAPI):
class Webhook(BaseAPI, Findable, Deleteable, Createable, Searchable, Updateable):
path = '/webhooks'
validator_class = WebhookValidator
4 changes: 0 additions & 4 deletions fulcrum/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@ class UnauthorizedException(Exception):
"""The API key supplied is not authorized to access this resource."""


class InvalidObjectException(Exception):
"""The object you attempted to create or update was invalid."""


class InternalServerErrorException(Exception):
"""There was an error while processing your request."""
27 changes: 27 additions & 0 deletions fulcrum/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Findable(object):
def find(self, id):
api_resp = self.call('get', '{0}/{1}'.format(self.path, id))
return api_resp


class Deleteable(object):
def delete(self, id):
self.call('delete', '{0}/{1}'.format(self.path, id))


class Createable(object):
def create(self, obj):
api_resp = self.call('post', self.path, data=obj, extra_headers={'Content-Type': 'application/json'})
return api_resp


class Searchable(object):
def search(self, params=None):
api_resp = self.call('get', self.path, params=params)
return api_resp


class Updateable(object):
def update(self, id, obj):
api_resp = self.call('put', '{0}/{1}'.format(self.path, id), data=obj, extra_headers={'Content-Type': 'application/json'})
return api_resp
146 changes: 0 additions & 146 deletions fulcrum/validators.py

This file was deleted.

Loading

0 comments on commit 8745474

Please sign in to comment.