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

Commit 218c311

Browse files
committed
initial commit
0 parents  commit 218c311

11 files changed

Lines changed: 234 additions & 0 deletions

File tree

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.pyc
2+
*.cache
3+
*#*
4+
*.egg-info
5+
*.egg
6+
*.iml
7+
.DS_Store
8+
.ropeproject
9+
.idea
10+
build
11+
_build
12+
dist
13+
env
14+
.venv

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0.1.0
2+
-----
3+
4+
- Initial version

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2015, Spot Media Solutions Sdn. Bhd. <daniel@atomx.com>
2+
3+
Permission to use, copy, modify, and/or distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include *.txt *.ini *.cfg *.rst
2+
recursive-include atomx

README.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
atomx api README
2+
================
3+
4+
This module let's you easlily
5+
6+
7+
Installation
8+
------------
9+
10+
To install the python atomx api, simply:
11+
12+
.. code-block:: bash
13+
14+
$ pip install atomx
15+

atomx/__init__.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import requests
4+
from .version import API_VERSION, VERSION
5+
from . import models
6+
from .exceptions import (
7+
APIError,
8+
ModelNotFoundError,
9+
InvalidCredentials,
10+
)
11+
12+
13+
__title__ = 'atomx'
14+
__version__ = VERSION
15+
__author__ = 'Spot Media Solutions Sdn. Bhd.'
16+
__copyright__ = 'Copyright 2015 Spot Media Solutions Sdn. Bhd.'
17+
18+
19+
class Atomx(object):
20+
def __init__(self, email, password, api_endpoint='http://api.atomx.com/{}/'.format(API_VERSION)):
21+
self.email = email
22+
self.password = password
23+
self.api_endpoint = api_endpoint
24+
self.session = requests.Session()
25+
self.login()
26+
27+
def login(self, email=None, password=None):
28+
if email:
29+
self.email = email
30+
if password:
31+
self.password = password
32+
33+
r = self.session.post(self.api_endpoint + 'login',
34+
json={'email': self.email, 'password': self.password})
35+
if not r.ok:
36+
if r.status_code == 401:
37+
raise InvalidCredentials
38+
raise APIError(r.json()['error'])
39+
self.auth_tk = r.json()['auth_tkt']
40+
41+
def logout(self):
42+
self.session.get(self.api_endpoint + 'logout')
43+
44+
def search(self, query):
45+
r = self.session.get(self.api_endpoint + 'search', params={'q': query})
46+
if not r.ok:
47+
raise APIError(r.json()['error'])
48+
return r.json()['search']
49+
50+
def get(self, model, **kwargs):
51+
if model not in dir(models):
52+
raise ModelNotFoundError()
53+
r = self.session.get(self.api_endpoint + model, params=kwargs)
54+
if not r.ok:
55+
raise APIError(r.json()['error'])
56+
57+
r_json = r.json()
58+
model_name = model.lower()
59+
if model_name in r_json:
60+
return getattr(models, model)(self, **r_json[model_name])
61+
return [getattr(models, model)(self, **m) for m in r_json[model_name + 's']]
62+
63+
def post(self, model, json, **kwargs):
64+
return self.session.post(self.api_endpoint + model, json=json, params=kwargs)
65+
66+
def put(self, model, id, json, **kwargs):
67+
return self.session.put(self.api_endpoint + model + '/' + str(id), json=json, params=kwargs)
68+
69+
def delete(self, model, id, json, **kwargs):
70+
return self.session.put(self.api_endpoint + model + '/' + str(id), json=json, params=kwargs)

atomx/exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class NoSessionError(Exception):
2+
pass
3+
4+
class InvalidCredentials(Exception):
5+
pass
6+
7+
class APIError(Exception):
8+
pass
9+
10+
class ModelNotFoundError(Exception):
11+
pass

atomx/models.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from .exceptions import NoSessionError
4+
5+
6+
__all__ = ['Advertiser', 'Campaign', 'Creative', 'Fallback', 'Network', 'Placement', 'Profile',
7+
'Publisher', 'Segment', 'Site', 'User']
8+
9+
10+
class AtomxModel(object):
11+
def __init__(self, session=None, **attributes):
12+
super(AtomxModel, self).__setattr__('session', session)
13+
super(AtomxModel, self).__setattr__('_attributes', attributes)
14+
super(AtomxModel, self).__setattr__('_dirty', set()) # list of changed attributes
15+
16+
def __getattr__(self, item):
17+
return self._attributes.get(item)
18+
19+
def __setattr__(self, key, value):
20+
if self._attributes[key] != value:
21+
self._attributes[key] = value
22+
self._dirty.add(key)
23+
24+
def __repr__(self):
25+
return '{}({})'.format(self.__class__.__name__, self._attributes)
26+
27+
@property
28+
def _dirty_json(self):
29+
return {k: self._attributes[k] for k in self._dirty}
30+
31+
@property
32+
def json(self):
33+
return self._attributes
34+
35+
def save(self, session=None):
36+
if not session and not self.session:
37+
raise NoSessionError
38+
return self.session.post(self.__class__.__name__, json=self._dirty_json)
39+
40+
def update(self, session=None):
41+
if not session and not self.session:
42+
raise NoSessionError
43+
return self.session.put(self.__class__.__name__, self.id, json=self._dirty_json)
44+
45+
def delete(self, session=None):
46+
if not session and not self.session:
47+
raise NoSessionError
48+
return self.session.delete(self.__class__.__name__, self.id, json=self._dirty_json)
49+
50+
51+
for m in __all__:
52+
locals()[m] = type(m, (AtomxModel,), {})

atomx/version.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VERSION = '0.1.0'
2+
API_VERSION = 'v1'

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bdist_wheel]
2+
universal = 1

0 commit comments

Comments
 (0)