Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clearbit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
from clearbit.watchlist import Watchlist
from clearbit.watchlist import Entity as WatchlistEntity
from clearbit.watchlist import Individual as WatchlistIndividual
from clearbit.watchlist import Candidate as WatchlistCandidate

key = os.getenv('CLEARBIT_KEY', None)
25 changes: 20 additions & 5 deletions clearbit/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
class Resource(dict):
endpoint = ''

@classmethod
def new(cls, item):
if isinstance(item, list):
return (cls(rec) for rec in item)
else:
return cls(item)

@classmethod
def get(cls, url, **options):
params = options.get('params', {})
Expand All @@ -16,7 +23,7 @@ def get(cls, url, **options):
response = requests.get(endpoint, params=params, auth=(key, ''))

if response.status_code == 200:
return cls(response.json())
return cls.new(response.json())
if response.status_code == 202:
return cls({ 'pending': True })
elif response.status_code == requests.codes.not_found:
Expand All @@ -31,8 +38,16 @@ def post(cls, url, **options):
endpoint = cls.endpoint + url

response = requests.post(endpoint, params=params, auth=(key, ''))
response.raise_for_status()

if response.status_code == 200:
return response
else:
response.raise_for_status()
return response

@classmethod
def delete(cls, url, **options):
key = options.get('key', clearbit.key)
endpoint = cls.endpoint + url

response = requests.delete(endpoint, auth=(key, ''))
response.raise_for_status()

return response
20 changes: 20 additions & 0 deletions clearbit/watchlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,23 @@ class Entity(Watchlist):
@classmethod
def search(cls, **options):
return super(Entity, cls).search(path='/search/entities', **options)

class Candidate(Resource):
endpoint = 'https://watchlist.clearbit.com/v1'

@classmethod
def all(cls):
return cls.get('/candidates')

@classmethod
def create(cls, **params):
response = cls.post('/candidates', params=params)

return cls(response.json())

@classmethod
def find(cls, id):
return cls.get('/%s' % id)

def destroy(self):
return self.__class__.delete('/candidates/%s' % self['id'])