def test_connect(mocker): mocker.patch.object(requests, "post") requests.post().json.return_value = {"response": {"token": "TOKEN"}} client = AppNexusClient() credentials = {"username": "******", "password": "******"} client.connect(**credentials) client.update_token() _, kwargs = requests.post.call_args assert kwargs["json"] == {"auth": credentials}
def connect(cls, username, password): cls.client = AppNexusClient(username, password) return cls.client
def test_model_can_have_class_and_instance_client(): Model.client = AppNexusClient('dumb', 'test') x = Campaign() x.client = AppNexusClient('dumbo', 'elephant') assert Campaign.client is not x.client
# -*- coding:utf-8-*- import pytest from appnexus.client import AppNexusClient from appnexus.cursor import Cursor from appnexus.model import Model Model.client = AppNexusClient("Test.", "dumb") class Campaign(Model): service = "campaign" @pytest.fixture def response(): return { "profile": [{ "id": 21975591, "another_field": "another_value" }], "count": 8 } @pytest.fixture def response2(): return {"member": {"id": 395858219, "field": "value"}, "count": 1}
def random_cursor(mocker, random_response_dict): client = AppNexusClient("test", "test") mocker.patch.object(client, "get") client.get.side_effect = random_response_dict return Cursor(client, "campaign", representations.raw)
def cursor(mocker, response_dict): client = AppNexusClient("test", "test") mocker.patch.object(client, 'get') client.get.return_value = response_dict return Cursor(client, "campaign", representations.raw)
def line_item(): LineItem.client = AppNexusClient("test", "test") return LineItem({ "id": 42 })
def get_report(start_date=None, end_date=None): config = configs.get("api_credentials") today = datetime.today() temp_folder = config.get('download_folder') resume_file = "{}/resume.txt".format(temp_folder) report_folder = "{}/reports".format(temp_folder) timezone = config.get('timezone') report_id = None if (end_date is None): tomorrow = today + timedelta(1) end_date = tomorrow.strftime('%Y-%m-%d') yesterday = today - timedelta(1) if (start_date is None): start_date = yesterday.strftime('%Y-%m-%d') params = { 'start_date': start_date, 'end_date': end_date, } LOGGER.info("Appnexus India API Date Range :: {}".format(params)) # Connect to the API client = AppNexusClient() client.connect(config.get('username'), config.get('password')) #Request to generate report params = { "report": { "report_type": "network_analytics", "columns": [ "hour", "advertiser_name", "line_item_name", "campaign_name", "creative_name", "size", "imps", "clicks", "post_view_convs", "post_click_convs", "cost", "advertiser_currency" ], "timezone": timezone, # "report_interval":"last_48_hours", "start_date": start_date, "end_date": end_date, "format": "csv" } } response = client.create('report', params) # print("REPORT REQUEST RETURN ::: {}".format(response)); if response.get('status') == 'OK': report_id = response.get('report_id', None) else: LOGGER.error('RESPONSE ERROR :: {}'.format(response)) raise RuntimeError print("REPORT ID ::: {}".format(report_id)) # Get report status, retry for 5 times if execution_status is not getting ready # if "execution_status": "ready" then get url for download report in "url": "report-download?id=3876d8ed34a14921f95793a1d618d3a9" valid_response = False limit = 5 count = 0 download_url = None report_object = None while not valid_response: response = client.get('report', id=report_id) # print("REPORT REQUEST RETURN ::: {}".format(response)); count += 1 if (response.get('status') == 'OK' and response.get('execution_status') == 'ready'): # report_object = response.get('report') download_url = response.get('report').get('url') valid_response = True elif count == limit: valid_response = True else: time.sleep(4) # print("REPORT REQUEST RETURN ::: {}".format(report_object)); print("REPORT DOWNLOAD URL ::: {}".format(download_url)) # Download Report url = report-download?id=07bd4e5ae85ac2200955d4bfad6a4acc # response = client.get('report-download', id=report_id, raw=True) # print("RESPONSE ::: {}".format(response)) data = None headers = dict(Authorization=client.token) url = client._prepare_uri('report-download', id=report_id) print('\n URI ::: {}'.format(url)) if 'reports' not in os.listdir(temp_folder): os.mkdir(report_folder) file_path = "{}/report_{}.csv".format( report_folder, tomorrow.strftime('%Y-%m-%d %H:%M:%S')) with closing(requests.get(url, stream=True, headers=headers)) as r: with open(file_path, 'wb') as fp: fp.write(r.content)
def client(username, password): return AppNexusClient(username, password)
def test_without_credentials(): client = AppNexusClient() with pytest.raises(RuntimeError): client.update_token()
def campaign(): Campaign.client = AppNexusClient("test", "test") return Campaign({"id": 9347201, "profile_id": 394828})
def test_base_url(): assert (AppNexusClient(None, None, test=False).base_url == AppNexusClient.url) assert (AppNexusClient(None, None, test=True).base_url == AppNexusClient.test_url)