Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit 85dcc40

Browse files
committed
fix save, update
better example add first tests
1 parent cfa7542 commit 85dcc40

4 files changed

Lines changed: 95 additions & 18 deletions

File tree

README.rst

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,44 @@ Example Usage:
1010
1111
from atomx import Atomx
1212
13-
atomx = Atomx('daniel@atomx.com', 'password')
14-
creatives = atomx.get('Creatives', limit=10)
13+
# create atomx session
14+
atomx = Atomx('user@example.com', 'password')
1515
16+
# get 10 creatives
17+
creatives = atomx.get('Creatives', limit=10)
18+
# the result is a list of `atomx.models.Creative` models
19+
# that you can easily inspect, manipulate and update
1620
for creative in creatives:
1721
print('Creative ID: {c.id}, state: {c.state}, '
1822
'name: {c.name}, title: {c.title}'.format(c=creative))
1923
24+
# update title for the first creative in list
2025
creative = creatives[0]
2126
creative.title = 'shiny new title'
27+
# the session is inherited from `atomx` that made the get request
2228
creative.update()
2329
2430
31+
# create a new profile
32+
from atomx.models import Profile
33+
profile = Profile(advertiser_id=23, name='test profile')
34+
# Note that you have to pass it a valid `Atomx` session for save
35+
# or use `atomx.save(profile)`
36+
profile.save(atomx)
37+
38+
# now you could alter and update it like the creative above
39+
profile.name = 'changed name'
40+
profile.update()
41+
42+
43+
# you can also get attributes
44+
profiles = atomx.get('advertiser/88/profiles')
45+
# profiles is now a list of `atomx.models.Profile` that you can
46+
# read, update, etc again.
47+
profiles[0].click_frequency_cap_per = 86400
48+
profiles[0].update()
49+
50+
2551
Installation
2652
------------
2753

atomx/__init__.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
__author__ = 'Spot Media Solutions Sdn. Bhd.'
1717
__copyright__ = 'Copyright 2015 Spot Media Solutions Sdn. Bhd.'
1818

19+
API_ENDPOINT = 'http://api.atomx.com/{}/'.format(API_VERSION)
20+
1921

2022
class Atomx(object):
21-
def __init__(self, email, password, api_endpoint='http://api.atomx.com/{}/'.format(API_VERSION)):
23+
def __init__(self, email, password, api_endpoint=API_ENDPOINT):
2224
self.email = email
2325
self.password = password
2426
self.api_endpoint = api_endpoint
@@ -48,25 +50,40 @@ def search(self, query):
4850
raise APIError(r.json()['error'])
4951
return r.json()['search']
5052

51-
def get(self, model, **kwargs):
52-
model = get_model_name(model)
53-
if not model:
54-
raise ModelNotFoundError()
55-
r = self.session.get(self.api_endpoint + model, params=kwargs)
53+
def get(self, resource, **kwargs):
54+
r = self.session.get(self.api_endpoint + resource, params=kwargs)
5655
if not r.ok:
5756
raise APIError(r.json()['error'])
5857

5958
r_json = r.json()
60-
model_name = model.lower()
61-
if model_name in r_json:
62-
return getattr(models, model)(self, **r_json[model_name])
63-
return [getattr(models, model)(self, **m) for m in r_json[model_name + 's']]
59+
model_name = r_json['resource']
60+
res = r_json[model_name]
61+
model = get_model_name(model_name)
62+
if model:
63+
if isinstance(res, list):
64+
return [getattr(models, model)(self, **m) for m in res]
65+
return getattr(models, model)(self, **res)
66+
return res
6467

6568
def post(self, model, json, **kwargs):
66-
return self.session.post(self.api_endpoint + model, json=json, params=kwargs)
69+
r = self.session.post(self.api_endpoint + model, json=json, params=kwargs)
70+
r_json = r.json()
71+
if not r.ok:
72+
raise APIError(r_json['error'])
73+
return r_json[r_json['resource']]
6774

6875
def put(self, model, id, json, **kwargs):
69-
return self.session.put(self.api_endpoint + model + '/' + str(id), json=json, params=kwargs)
76+
r = self.session.put(self.api_endpoint + model + '/' + str(id), json=json, params=kwargs)
77+
r_json = r.json()
78+
if not r.ok:
79+
raise APIError(r_json['error'])
80+
return r_json[r_json['resource']]
7081

7182
def delete(self, model, id, json, **kwargs):
7283
return self.session.put(self.api_endpoint + model + '/' + str(id), json=json, params=kwargs)
84+
85+
def save(self, model):
86+
return model.save(self)
87+
88+
def update(self, model):
89+
return model.update(self)

atomx/models.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,20 @@ def json(self):
3333
return self._attributes
3434

3535
def save(self, session=None):
36-
if not session and not self.session:
36+
session = session or self.session
37+
if not session:
3738
raise NoSessionError
38-
return self.session.post(self.__class__.__name__, json=self._dirty_json)
39+
res = session.post(self.__class__.__name__, json=self.json)
40+
self.__init__(session=session, **res)
41+
return self
3942

4043
def update(self, session=None):
41-
if not session and not self.session:
44+
session = session or self.session
45+
if not session:
4246
raise NoSessionError
43-
return self.session.put(self.__class__.__name__, self.id, json=self._dirty_json)
47+
res = self.session.put(self.__class__.__name__, self.id, json=self._dirty_json)
48+
self.__init__(session=session, **res)
49+
return self
4450

4551
def delete(self, session=None):
4652
if not session and not self.session:

tests.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
4+
@pytest.fixture(scope="session")
5+
def atomx():
6+
from .atomx import Atomx
7+
return Atomx('daniel@atomx.com', 'password')
8+
9+
10+
def test_limit(atomx):
11+
creatives = atomx.get('creatives', limit=5)
12+
assert len(creatives) == 5
13+
14+
def test_update(atomx):
15+
NEW_TITLE = 'test title'
16+
creative = atomx.get('creatives', id=5)
17+
creative.title = NEW_TITLE
18+
creative.update()
19+
creative = atomx.get('creatives', id=5)
20+
assert creative.title == NEW_TITLE
21+
22+
23+
def test_save(atomx):
24+
from .atomx.models import Profile
25+
profile = Profile(advertiser_id=23, name='test advertiser')
26+
profile.save()
27+
profile_new = atomx.get(Profile, id=profile.id)
28+
assert profile.name == profile_new.name

0 commit comments

Comments
 (0)