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

Commit 21324e8

Browse files
committed
add simple example
better handling for model strings
1 parent 218c311 commit 21324e8

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

README.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
atomx api README
22
================
33

4-
This module let's you easlily
4+
Simple interface for the atomx rest api.
5+
6+
Example Usage:
7+
--------------
8+
9+
.. code-block:: python
10+
11+
from atomx import Atomx
12+
13+
api = Atomx('daniel@atomx.com', 'password')
14+
creatives = api.get('Creatives', limit=10)
15+
16+
for creative in creatives:
17+
print('Creative ID: {c.id}, state: {c.state},'
18+
'name: {c.name}, title: {c.title}'.format(c=creative))
19+
20+
creative = creatives[0]
21+
creative.title = 'shiny new title'
22+
creative.save()
523
624
725
Installation

atomx/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import requests
44
from .version import API_VERSION, VERSION
55
from . import models
6+
from .utils import get_model_name
67
from .exceptions import (
78
APIError,
89
ModelNotFoundError,
@@ -48,7 +49,8 @@ def search(self, query):
4849
return r.json()['search']
4950

5051
def get(self, model, **kwargs):
51-
if model not in dir(models):
52+
model = get_model_name(model)
53+
if not model:
5254
raise ModelNotFoundError()
5355
r = self.session.get(self.api_endpoint + model, params=kwargs)
5456
if not r.ok:

atomx/utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from . import models
2+
3+
def get_model_name(name):
4+
"""Checks that :param:`name` is a valid model.
5+
Converts plural `name` to capitalized singular form and '-' or '_' separation to camelCase.
6+
Returns the name of the model if found, False otherwise.
7+
E.g.
8+
9+
>>> check_model_name("countries")
10+
"Country"
11+
>>> check_model_name("ADVERTISERS")
12+
"Advertiser"
13+
>>> check_model_name("conversion-pixels")
14+
"ConversionPixel"
15+
>>> check_model_name("operating_system")
16+
"OperatingSystem"
17+
>>> check_model_name("InvalidModel")
18+
False
19+
20+
:param str name: model name to convert
21+
:return: name of the model or False
22+
"""
23+
if '-' in name:
24+
model_name = ''.join(m.capitalize() for m in name.split('-'))
25+
elif '_' in name:
26+
model_name = ''.join(m.capitalize() for m in name.split('_'))
27+
else:
28+
model_name = name.capitalize()
29+
30+
if model_name.endswith('ies'): # change e.g. Countries to Countr*y*
31+
model_name = model_name[:-3]+'y'
32+
else:
33+
model_name = model_name.rstrip('s')
34+
if model_name in dir(models):
35+
return model_name
36+
return False

0 commit comments

Comments
 (0)