Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rambobinator committed Jul 31, 2019
1 parent d6a8134 commit 91be6d5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 23 deletions.
21 changes: 13 additions & 8 deletions appnexus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ def __init__(self, username=None, password=None, test=False,

self._generate_services()

def _prepare_uri(self, service_name, **parameters):
def _prepare_uri(self, service_name, uri_parameters_template=None,
**parameters):
"""Prepare the URI for a request
:param service_name: The target service
:type service_name: str
:param kwargs: query parameters
:return: The uri of the request
"""
url = self.base_url + service_name
if uri_parameters_template is not None:
url += uri_parameters_template.format(**parameters)

query_parameters = []
for key, value in parameters.items():
if isinstance(value, (list, tuple)):
Expand All @@ -53,11 +58,9 @@ def _prepare_uri(self, service_name, **parameters):
query_parameters.append("{}={}".format(key, value))

if query_parameters:
uri = "{}{}?{}".format(self.base_url, service_name,
"&".join(query_parameters))
else:
uri = "{}{}".format(self.base_url, service_name)
return uri
url = "{}?{}".format(url, "&".join(query_parameters))
print(url)
return url

# shiro: Coverage is disabled for this function because it's mocked and it
# doesn't need testing (for the moment) since it's a simple instruction
Expand All @@ -77,10 +80,12 @@ def _send(self, send_method, service_name, data=None, **kwargs):
"""
valid_response = False
raw = kwargs.pop("raw", False)
uri_parameters_template = kwargs.pop("uri_parameters_template", None)

while not valid_response:
headers = dict(Authorization=self.token)
uri = self._prepare_uri(service_name, **kwargs)
uri = self._prepare_uri(service_name, uri_parameters_template,
**kwargs)
logger.debug(' '.join(map(str, (headers, uri, data))))

response = send_method(uri, headers=headers, json=data)
Expand Down Expand Up @@ -220,7 +225,7 @@ def base_url(self):


services_list = ["AccountRecovery", "AdProfile", "AdQualityRule", "Adserver",
"Advertiser", "BatchSegment", "Brand", "Broker", "Browser",
"Advertiser", "AugmentedLineItem", "BatchSegment", "Brand", "Broker", "Browser",
"BudgetSplitter", "Campaign", "Carrier", "Category",
"ChangeLog", "ChangeLogDetail", "City", "ContentCategory",
"Country", "Creative", "CreativeFormat", "Currency",
Expand Down
67 changes: 52 additions & 15 deletions appnexus/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,51 @@ class LineItemModel(AlphaModel):
pass


class LineItem(Model):

@classmethod
def find(cls, **kwargs):
kwargs["line_item_type"] = "standard_v1"
return super().find(**kwargs)


class BudgetSplitter(Model):

@classmethod
def find(cls, **kwargs):
raise NotImplementedError("Can't get multiple objects on '{}' service"
.format(cls.service_name))

@classmethod
def find_one(cls, id, **kwargs):
representation = (kwargs.pop("representation", None)
or cls.client.representation
or cls.constructor)
kwargs.update({
"uri_parameters_template": "/{}/splits".format(id)
})
response = cls.client.get(cls.service_name, **kwargs)
if representation:
return representation(cls.client, cls.service_name, response)
return response


class AugmentedLineItem(Model):

@classmethod
def find(cls, **kwargs):
kwargs["line_item_type"] = "standard_v2"
return super().find(**kwargs)

@classproperty
def service_name(cls):
return "line-item"

@property
def splits(self):
return BudgetSplitter.find_one(id=self.id) # noqa: F821


class Report(Model):

def download(self, retry_count=3, **kwargs):
Expand All @@ -139,13 +184,6 @@ def is_ready(self):
return (status == "ready")


class BudgetSplitterMixin():

@property
def budget_splitter(self):
return BudgetSplitter.find_one(id=self.id) # noqa: F821


class ChangeLogMixin():

@property
Expand All @@ -161,21 +199,20 @@ def profile(self):
return Profile.find_one(id=self.profile_id) # noqa: F821


def create_models(services_list):
def create_or_patch_models(services_list):
for service_name in services_list:
ancestors = [Model]
if service_name in ("LineItem"):
ancestors.append(BudgetSplitterMixin)
ancestors = [globals().get(service_name, Model)]
if service_name in ("Campaign", "InsertionOrder", "LineItem",
"Profile"):
"AugmentedLineItem", "Profile"):
ancestors.append(ChangeLogMixin)
if service_name in ("AdQualityRule", "Advertiser", "Campaign",
"Creative", "LineItem", "PaymentRule"):
"Creative", "LineItem", "AugmentedLineItem",
"PaymentRule"):
ancestors.append(ProfileMixin)
model = type(service_name, tuple(ancestors), {})
globals().setdefault(service_name, model)
globals()[service_name] = model


create_models(services_list)
create_or_patch_models(services_list)

__all__ = ["Model", "services_list"] + services_list

0 comments on commit 91be6d5

Please sign in to comment.