Skip to content

Commit d617f72

Browse files
committed
Add options class for table update, fix put in client
1 parent 2dda027 commit d617f72

4 files changed

Lines changed: 66 additions & 62 deletions

File tree

src/Keboola/StorageApi/Client.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Keboola\StorageApi\Options\ListFilesOptions;
1313
use Keboola\StorageApi\Options\SearchTablesOptions;
1414
use Keboola\StorageApi\Options\StatsOptions;
15+
use Keboola\StorageApi\Options\TableUpdateOptions;
1516
use Keboola\StorageApi\Options\TokenCreateOptions;
1617
use Keboola\StorageApi\Options\TokenUpdateOptions;
1718
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
@@ -691,24 +692,14 @@ public function createTableSnapshot($tableId, $snapshotDescription = null)
691692
}
692693

693694
/**
694-
* @param string $tableId
695-
* @param array $options
695+
* @param TableUpdateOptions $options
696696
* @return string
697697
*/
698-
public function updateTable($tableId, $options)
698+
public function updateTable(TableUpdateOptions $options)
699699
{
700-
$url = "storage/tables/" . $tableId;
701-
702-
$allowedOptions = [
703-
'displayName',
704-
];
705-
706-
$filteredOptions = array_intersect_key($options, array_flip($allowedOptions));
707-
708-
$url .= '?' . http_build_query($filteredOptions);
700+
$result = $this->apiPut('storage/tables/' . $options->getTableId(), $options->toParamsArray());
709701

710-
$result = $this->apiPut($url);
711-
$this->log("Table {$tableId} updated");
702+
$this->log("Table {$options->getTableId()} updated");
712703
return $result['id'];
713704
}
714705

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Keboola\StorageApi\Options;
4+
5+
class TableUpdateOptions
6+
{
7+
/** @var string $tableId */
8+
private $tableId;
9+
10+
/** @var string $displayName */
11+
private $displayName;
12+
13+
public function __construct($tableId, $displayName)
14+
{
15+
$this->tableId = (string) $tableId;
16+
$this->displayName = (string) $displayName;
17+
}
18+
19+
/** @return string */
20+
public function getDisplayName()
21+
{
22+
return $this->displayName;
23+
}
24+
25+
/** @return string */
26+
public function getTableId()
27+
{
28+
return $this->tableId;
29+
}
30+
31+
/** @return array */
32+
public function toParamsArray()
33+
{
34+
$params = [];
35+
36+
if ($this->getDisplayName()) {
37+
$params['displayName'] = $this->getDisplayName();
38+
}
39+
40+
return $params;
41+
}
42+
}

tests/Backend/CommonPart1/CreateTableTest.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Keboola\StorageApi\ClientException;
1313
use Keboola\StorageApi\Options\FileUploadOptions;
14+
use Keboola\StorageApi\Options\TableUpdateOptions;
1415
use Keboola\Test\StorageApiTestCase;
1516
use Keboola\Csv\CsvFile;
1617

@@ -61,32 +62,21 @@ public function testTableCreate($tableName, $createFile, $expectationFile, $asyn
6162
);
6263

6364
$displayName = 'Romanov-display-name';
64-
$tableId = $this->_client->updateTable(
65-
$tableId,
66-
[
67-
'displayName' => $displayName,
68-
]
69-
);
65+
$tableUpdateOptions = new TableUpdateOptions($tableId, $displayName);
66+
$tableId = $this->_client->updateTable($tableUpdateOptions);
7067

7168
$table = $this->_client->getTable($tableId);
7269

7370
$this->assertEquals($displayName, $table['displayName']);
7471

7572
// rename table to same name it already has should succeed
76-
$this->_client->updateTable(
77-
$tableId,
78-
[
79-
'displayName' => $displayName,
80-
]
81-
);
73+
74+
$tableUpdateOptions = new TableUpdateOptions($tableId, $displayName);
75+
$this->_client->updateTable($tableUpdateOptions);
8276

8377
try {
84-
$this->_client->updateTable(
85-
$tableId,
86-
[
87-
'displayName' => '_wrong-display-name',
88-
]
89-
);
78+
$tableUpdateOptions = new TableUpdateOptions($tableId, '_wrong-display-name');
79+
$this->_client->updateTable($tableUpdateOptions);
9080
} catch (\Keboola\StorageApi\ClientException $e) {
9181
$this->assertEquals(
9282
'Invalid data - displayName: Cannot start with underscore.',
@@ -104,12 +94,8 @@ public function testTableCreate($tableName, $createFile, $expectationFile, $asyn
10494
$options
10595
);
10696
try {
107-
$this->_client->updateTable(
108-
$anotherTableId,
109-
[
110-
'displayName' => $displayName,
111-
]
112-
);
97+
$tableUpdateOptions = new TableUpdateOptions($anotherTableId, $displayName);
98+
$this->_client->updateTable($tableUpdateOptions);
11399
$this->fail('Renaming another table to existing displayname should fail');
114100
} catch (\Keboola\StorageApi\ClientException $e) {
115101
$this->assertEquals(

tests/Backend/Synapse/CreateTableTest.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Keboola\StorageApi\Client;
99
use Keboola\StorageApi\ClientException;
1010
use Keboola\StorageApi\Exception;
11+
use Keboola\StorageApi\Options\TableUpdateOptions;
1112
use Keboola\StorageApi\Workspaces;
1213
use Keboola\Test\Backend\Workspaces\Backend\WorkspaceBackendFactory;
1314
use Keboola\Test\Backend\Workspaces\WorkspacesTestCase;
@@ -53,32 +54,20 @@ public function testTableCreate($tableName, $createFile, $expectationFile, $asyn
5354
// );
5455

5556
$displayName = 'Romanov-display-name';
56-
$tableId = $this->_client->updateTable(
57-
$tableId,
58-
[
59-
'displayName' => $displayName,
60-
]
61-
);
57+
$tableUpdateOptions = new TableUpdateOptions($tableId, $displayName);
58+
$tableId = $this->_client->updateTable($tableUpdateOptions);
6259

6360
$table = $this->_client->getTable($tableId);
6461

6562
$this->assertEquals($displayName, $table['displayName']);
6663

6764
// rename table to same name it already has should succeed
68-
$this->_client->updateTable(
69-
$tableId,
70-
[
71-
'displayName' => $displayName,
72-
]
73-
);
65+
$tableUpdateOptions = new TableUpdateOptions($tableId, $displayName);
66+
$this->_client->updateTable($tableUpdateOptions);
7467

7568
try {
76-
$this->_client->updateTable(
77-
$tableId,
78-
[
79-
'displayName' => '_wrong-display-name',
80-
]
81-
);
69+
$tableUpdateOptions = new TableUpdateOptions($tableId, '_wrong-display-name');
70+
$this->_client->updateTable($tableUpdateOptions);
8271
} catch (\Keboola\StorageApi\ClientException $e) {
8372
$this->assertEquals(
8473
'Invalid data - displayName: Cannot start with underscore.',
@@ -96,12 +85,8 @@ public function testTableCreate($tableName, $createFile, $expectationFile, $asyn
9685
$options
9786
);
9887
try {
99-
$this->_client->updateTable(
100-
$anotherTableId,
101-
[
102-
'displayName' => $displayName,
103-
]
104-
);
88+
$tableUpdateOptions = new TableUpdateOptions($anotherTableId, $displayName);
89+
$this->_client->updateTable($tableUpdateOptions);
10590
$this->fail('Renaming another table to existing displayname should fail');
10691
} catch (\Keboola\StorageApi\ClientException $e) {
10792
$this->assertEquals(

0 commit comments

Comments
 (0)