Skip to content

Commit 9dd7291

Browse files
committed
Make v3 of the api the default version
Merge branch 'v3'
2 parents 3c09037 + 6688578 commit 9dd7291

File tree

4 files changed

+34
-201
lines changed

4 files changed

+34
-201
lines changed

Atomx/ApiClient.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ private function getOptions($options)
8989
{
9090
$defaultOptions = $this->getDefaultOptions();
9191

92-
// TODO: Use a merge that overrides already set values
93-
$options = array_merge_recursive($defaultOptions, $options);
92+
$options = array_replace_recursive($defaultOptions, $options);
9493

9594
return $options;
9695
}

Atomx/AtomxClient.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function getDefaultOptions()
5757
$options = parent::getDefaultOptions();
5858

5959
if ($this->shouldSendToken)
60-
$options['cookies'] = ['auth_tkt' => $this->getToken()];
60+
$options['headers'] = ['Authorization' => $this->getToken()];
6161

6262
return $options;
6363
}
@@ -67,9 +67,11 @@ public function login()
6767
$this->shouldSendToken = false;
6868

6969
try {
70-
$response = $this->getUrl('login', [
71-
'email' => $this->accountStore->getUsername(),
72-
'password' => $this->accountStore->getPassword()
70+
$response = $this->postUrl('login', [
71+
'json' => [
72+
'email' => $this->accountStore->getUsername(),
73+
'password' => $this->accountStore->getPassword()
74+
]
7375
]);
7476
} catch (ApiException $e) {
7577
throw new ApiException('Unable to login to API!');
@@ -84,7 +86,8 @@ public function login()
8486
if ($response['success'] !== true)
8587
throw new ApiException('Unable to login to API!');
8688

87-
$token = $response['auth_tkt'] . '!userid_type:int';
89+
90+
$token = 'Bearer ' . $response['auth_token'];
8891

8992
$this->accountStore->storeToken($token);
9093

Atomx/Resources/Report.php

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,90 +9,21 @@
99
class Report extends AtomxClient {
1010
private $returnStream = false;
1111

12-
// Run report
13-
public function run($json)
12+
public function run($json, $timeout = 600)
1413
{
15-
return $this->postUrl('report', compact('json'));
16-
}
17-
18-
public function status($reportId)
19-
{
20-
return $this->getUrl('report/' . $reportId, ['status' => true]);
21-
}
22-
23-
public static function getReportId($report)
24-
{
25-
if (isset($report['report']['id'])) {
26-
return $report['report']['id'];
27-
}
28-
29-
return false;
30-
}
31-
32-
public static function isReady($report)
33-
{
34-
if (isset($report['report']['is_ready'])) {
35-
return $report['report']['is_ready'];
36-
}
37-
38-
return false;
39-
}
40-
41-
public static function numberOfRows($report)
42-
{
43-
if (isset($report['report']['lines']))
44-
return $report['report']['lines'];
45-
46-
return false;
47-
}
48-
49-
public static function getColumns($report)
50-
{
51-
if (!is_null($report) && isset($report['query'])) {
52-
$sumsOrMetrics = (isset($report['query']['sums']) ? $report['query']['sums'] : $report['query']['metrics']);
53-
54-
return array_merge($report['query']['groups'], $sumsOrMetrics);
55-
}
56-
57-
return false;
58-
}
59-
60-
public function download($report)
61-
{
62-
$reportId = $report['report']['id'];
63-
6414
$this->returnStream = true;
6515

66-
$stream = $this->getUrl('report/' . $reportId, [], [
67-
// 'timeout' => 0,
68-
// 'connect_timeout' => 0
16+
$stream = $this->postUrl('report?download', [
17+
'json' => $json,
18+
'timeout' => $timeout,
19+
'connect_timeout' => 20
6920
]);
7021

7122
$this->returnStream = false;
7223

7324
return new ReportStreamer($stream);
7425
}
7526

76-
public function runAndDownload($json, $timeout = 120, $returnReportId = false)
77-
{
78-
$reportData = $this->run($json);
79-
80-
$secondsWaiting = 0;
81-
82-
while (!Report::isReady($this->status(Report::getReportId($reportData)))) {
83-
sleep(1);
84-
85-
if (++$secondsWaiting >= $timeout) {
86-
return false;
87-
}
88-
}
89-
90-
if ($returnReportId)
91-
return [$this->download($reportData), Report::getReportId($reportData)];
92-
else
93-
return $this->download($reportData);
94-
}
95-
9627
protected function handleResponse(Response $response)
9728
{
9829
if ($response->getStatusCode() == 200) {

tests/ReportTest.php

Lines changed: 20 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -10,128 +10,28 @@ public function testCreateAndDownloadReport()
1010
$to = date('Y-m-d 00:00:00', time());
1111

1212
$report = new Report(new AtomxAccountStore());
13-
$options = array(
14-
'scope' => 'advertiser',
15-
'groups' => ['campaign_id', 'domain_id', 'day_timestamp'],
16-
'sums' => ['impressions', 'clicks', 'conversions', 'campaign_cost'],
13+
$options = [
14+
'scope' => 'network_buy',
15+
'groups' => ['day', 'campaign_id', 'creative_id', 'country_id'],
16+
'metrics' => ['impressions', 'clicks', 'conversions', 'campaign_cost', 'advertiser_network_profit'],
1717
'where' => [['advertiser_network_id', '==', '1']],
1818
'from' => $from,
1919
'to' => $to,
2020
'timezone' => 'UTC'
21-
);
22-
23-
//$report = $report->run($options);
24-
25-
//var_export($report);
26-
}
27-
28-
public function testReportIsDone()
29-
{
30-
$report = new Report(new AtomxAccountStore());
31-
$rData = $report->status(Report::getReportId($this->getReportData()));
32-
33-
var_dump($rData);
34-
35-
$this->assertEquals(true, Report::isReady($rData));
36-
}
37-
38-
public function testDownloadReport()
39-
{
40-
$report = new Report(new AtomxAccountStore());
41-
$rData = $this->getReportData();
42-
43-
$streamer = $report->download($rData);
44-
45-
var_dump(Report::numberOfRows($rData));
46-
var_dump(Report::getColumns($rData));
47-
48-
var_dump($streamer->readLine());
49-
50-
$imps = 0;
51-
$revenue = 0;
52-
53-
while (($row = $streamer->readLine()) !== false) {
54-
$imps += $row[3];
55-
$revenue += $row[6];
56-
}
57-
58-
var_dump($imps, $revenue);
59-
}
60-
61-
public function testRunAndDownloadReport()
62-
{
63-
$from = date('Y-m-d 00:00:00', time() - 24 * 60 * 60);
64-
$to = date('Y-m-d 00:00:00', time());
65-
66-
$report = new Report(new AtomxAccountStore());
67-
$options = array(
68-
'scope' => 'advertiser',
69-
'groups' => ['campaign_id', 'domain_id', 'day_timestamp'],
70-
'sums' => ['impressions', 'clicks', 'conversions', 'campaign_cost'],
71-
'where' => [['advertiser_network_id', '==', '1']],
72-
'from' => $from,
73-
'to' => $to,
74-
'timezone' => 'UTC'
75-
);
76-
77-
$streamer = $report->runAndDownload($options);
78-
79-
$this->assertNotEquals(false, $streamer);
80-
81-
var_dump($streamer->readLine());
82-
}
83-
84-
/**
85-
* @return array
86-
*/
87-
private function getReportData()
88-
{
89-
// TODO: Mock the response
90-
$rData = [
91-
'success' => true,
92-
'timestamp' => '2015-09-23T15:30:38.563796',
93-
'report' =>
94-
[
95-
'link' => '/v1/report/89130c7cd76c7637195224529bbae9c7',
96-
'duration' => NULL,
97-
'started' => 1443022238,
98-
'lines' => 0,
99-
'error' => NULL,
100-
'finished' => NULL,
101-
'fast' => true,
102-
'is_ready' => true,
103-
'id' => '89130c7cd76c7637195224529bbae9c7',
104-
],
105-
'query' =>
106-
[
107-
'from' => '2015-09-22 00:00:00',
108-
'timezone' => 'UTC',
109-
'sums' =>
110-
array (
111-
0 => 'impressions',
112-
1 => 'clicks',
113-
2 => 'conversions',
114-
3 => 'campaign_cost',
115-
),
116-
'groups' =>
117-
array (
118-
0 => 'campaign_id',
119-
1 => 'domain_id',
120-
2 => 'day_timestamp',
121-
),
122-
'to' => '2015-09-23 00:00:00',
123-
'where' =>
124-
array (
125-
0 =>
126-
array (
127-
0 => 'advertiser_network_id',
128-
1 => '==',
129-
2 => '1',
130-
),
131-
),
132-
'scope' => 'advertiser'
133-
]
134-
];
135-
return $rData;
21+
];
22+
23+
$report = $report->run($options);
24+
25+
$this->assertEquals([
26+
'day',
27+
'campaign_id',
28+
'creative_id',
29+
'country_id',
30+
'impressions',
31+
'clicks',
32+
'conversions',
33+
'campaign_cost',
34+
'advertiser_network_profit'
35+
], $report->readLine());
13636
}
137-
}
37+
}

0 commit comments

Comments
 (0)