Skip to content

Commit f9961f6

Browse files
author
Artem Stepin
committed
added token auth method
1 parent a9560a8 commit f9961f6

File tree

8 files changed

+129
-56
lines changed

8 files changed

+129
-56
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use CloudPlayDev\ConfluenceClient\Entity\ConfluencePage;
2525

2626
//Create and configure a curl web client
2727
$curl = new Curl('confluence_host_url','username','password');
28+
// $curl = new CurlTokenAuth('confluence_host_url','NjU9OTXA4NDI2MRY5OkBznOUO8YjaUF7KoOruZRXhILJ9');
2829

2930
//Create the Confluence Client
3031
$client = new Client($curl);
@@ -39,7 +40,7 @@ $page->setSpace('testSpaceKey')->setTitle('Test')->setContent('<p>test page</p>'
3940
$client->createPage($page);
4041

4142
//Get the page we created
42-
echo $client->selectPageBy([
43+
$client->selectPageBy([
4344
'spaceKey' => 'testSpaceKey',
4445
'title' => 'Test'
4546
]);

src/Client.php

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
use CloudPlayDev\ConfluenceClient\Entity\ConfluencePage;
1313
use InvalidArgumentException;
14+
use CloudPlayDev\ConfluenceClient\Exception\Exception;
15+
use function count;
1416
use function in_array;
1517
use function is_array;
18+
use function is_string;
1619

1720
class Client
1821
{
@@ -52,7 +55,7 @@ public function createPage(ConfluencePage $page)
5255
],
5356
];
5457

55-
return $this->request('GET', $this->curl->getHost() . '/content', $data);
58+
return $this->request('GET', $this->curl->getHost() . '/rest/api/content', $data);
5659
}
5760

5861

@@ -76,10 +79,10 @@ public function updatePage(ConfluencePage $page)
7679
'representation' => 'storage',
7780
],
7881
],
79-
'version' => ['number' => $page->getVersion()]
82+
'version' => ['number' => $page->getVersion()+1]
8083
];
8184

82-
return $this->request('POST', $this->curl->getHost() . "/content/{$page->getId()}", $data);
85+
return $this->request('PUT', $this->curl->getHost() . "/rest/api/content/{$page->getId()}", $data);
8386
}
8487

8588
/**
@@ -91,18 +94,18 @@ public function updatePage(ConfluencePage $page)
9194
*/
9295
public function deletePage(string $id)
9396
{
94-
return $this->request('DELETE', $this->curl->getHost() . "/content/$id");
97+
return $this->request('DELETE', $this->curl->getHost() . "/rest/api/content/$id");
9598
}
9699

97100
/**
98101
* Search page by title, space key, type or id
99102
* @param array<string, string> $parameters
100-
* @return mixed
103+
* @return ConfluencePage|null
101104
* @throws Exception
102105
*/
103-
public function selectPageBy(array $parameters = [])
106+
public function selectPageBy(array $parameters = []): ?ConfluencePage
104107
{
105-
$url = $this->curl->getHost() . '/content?';
108+
$url = $this->curl->getHost() . '/rest/api/content?';
106109
if (isset($parameters['title'])) {
107110
$url .= "title={$parameters['title']}&";
108111
}
@@ -113,13 +116,46 @@ public function selectPageBy(array $parameters = [])
113116
$url .= "type={$parameters['type']}&";
114117
}
115118
if (isset($parameters['id'])) {
116-
$url = $this->curl->getHost() . '/content/' . $parameters['id'] . '?';
119+
return $this->getPageById((int)$parameters['id']);
117120
}
118121
if (isset($parameters['expand'])) {
119122
$url .= 'expand=' . $parameters['expand'];
120123
}
121124

122-
return $this->request('GET', $url);
125+
$searchResponse = $this->request('GET', $url);
126+
127+
if (is_array($searchResponse) && isset($searchResponse['results'], $searchResponse['size']) && is_array($searchResponse['results']) && $searchResponse['size'] >= 1 && count($searchResponse['results']) >= 1) {
128+
$firstPage = (array)reset($searchResponse['results']);
129+
if (isset($firstPage['id'])) {
130+
return $this->getPageById((int)$firstPage['id']);
131+
}
132+
}
133+
return null;
134+
}
135+
136+
public function getPageById(int $pageId): ?ConfluencePage
137+
{
138+
$url = $this->curl->getHost() . '/rest/api/content/'.$pageId;
139+
$firstPage = $this->request('GET', $url);
140+
141+
if (!isset($firstPage['id'],
142+
$firstPage['type'],
143+
$firstPage['title'],
144+
$firstPage['_links']['self'],
145+
$firstPage['space']['key'],
146+
$firstPage['version']['number'])
147+
) {
148+
return null;
149+
}
150+
151+
$page = new ConfluencePage();
152+
$page->setId((int)$firstPage['id']);
153+
$page->setType((string)$firstPage['type']);
154+
$page->setTitle((string)$firstPage['title']);
155+
$page->setUrl((string)$firstPage['_links']['self']);
156+
$page->setSpace(str_replace('/rest/api/space/', '', (string)$firstPage['space']['key']));
157+
$page->setVersion((int)$firstPage['version']['number']);
158+
return $page;
123159
}
124160

125161
/**
@@ -140,7 +176,7 @@ public function uploadAttachment(string $path, string $parentPageId)
140176
];
141177
return $this->request(
142178
'POST',
143-
$this->curl->getHost() . "/content/$parentPageId/child/attachment",
179+
$this->curl->getHost() . "/rest/api/content/$parentPageId/child/attachment",
144180
$data,
145181
$headers
146182
);
@@ -155,7 +191,7 @@ public function uploadAttachment(string $path, string $parentPageId)
155191
*/
156192
public function selectAttachments(string $pageId)
157193
{
158-
return $this->request('GET', $this->curl->getHost() . "/content/$pageId/child/attachment");
194+
return $this->request('GET', $this->curl->getHost() . "/rest/api/content/$pageId/child/attachment");
159195
}
160196

161197
/**
@@ -166,7 +202,7 @@ public function selectAttachments(string $pageId)
166202
*/
167203
public function addLabel(string $pageId, array $labels)
168204
{
169-
return $this->request('POST', $this->curl->getHost() . "/content/$pageId/label", $labels);
205+
return $this->request('POST', $this->curl->getHost() . "/rest/api/content/$pageId/label", $labels);
170206
}
171207

172208
/**
@@ -177,11 +213,11 @@ public function addLabel(string $pageId, array $labels)
177213
* @param mixed[] $data
178214
* @param array<string, string> $headers
179215
*
180-
* @return string|false
216+
* @return mixed[]|null
181217
*
182218
* @throws Exception
183219
*/
184-
public function request(string $method, string $url, array $data = [], array $headers = ['Content-Type' => 'application/json'])
220+
public function request(string $method, string $url, array $data = [], array $headers = ['Content-Type' => 'application/json']): ?array
185221
{
186222
//Detect invalid method
187223
$method = strtoupper($method);
@@ -196,16 +232,25 @@ public function request(string $method, string $url, array $data = [], array $he
196232
])->setHeaders($headers);
197233

198234
if ($data !== []) {
199-
$this->curl->setOption(CURLOPT_POSTFIELDS, $data);
235+
$this->curl->setOption(CURLOPT_POSTFIELDS, json_encode($data, JSON_THROW_ON_ERROR));
200236
}
201237

202238
$serverOutput = $this->curl->execute();
203-
$this->curl->close();
204239

205-
if (!is_scalar($serverOutput) && !is_array($serverOutput)) {
240+
if (!is_string($serverOutput)) {
206241
throw new InvalidArgumentException('Unexpected return value');
207242
}
208243

209-
return json_encode($serverOutput, JSON_THROW_ON_ERROR);
244+
if(empty($serverOutput)) {
245+
return null;
246+
}
247+
248+
$decodedData = json_decode($serverOutput, true, 512, JSON_THROW_ON_ERROR);
249+
250+
if (!is_array($decodedData)) {
251+
throw new InvalidArgumentException('Return value could not be decoded.');
252+
}
253+
254+
return $decodedData;
210255
}
211256
}

src/Curl.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace CloudPlayDev\ConfluenceClient;
1111

12+
use CloudPlayDev\ConfluenceClient\Exception\Exception;
1213
use function is_resource;
1314

1415
/**
@@ -38,11 +39,12 @@ public function __construct(string $host, string $username, string $password)
3839
}
3940

4041
$this->curl = $ch;
41-
4242
$this->hostUrl = $host;
43-
curl_setopt_array($this->curl, [
43+
44+
$this->setOptions([
4445
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
45-
CURLOPT_USERPWD => $username . ':' . $password
46+
CURLOPT_USERPWD => $username . ':' . $password,
47+
CURLINFO_HEADER_OUT => true,
4648
]);
4749
}
4850

@@ -98,6 +100,12 @@ public function execute()
98100
throw new Exception(curl_error($this->curl) . curl_errno($this->curl));
99101
}
100102

103+
$responseCode = (int)curl_getinfo($this->curl, CURLINFO_RESPONSE_CODE);
104+
105+
if($responseCode !== 200) {
106+
throw new Exception('Response code: ' . $responseCode . ' ( ' . $result . ' )');
107+
}
108+
101109
return $result;
102110
}
103111

@@ -133,7 +141,7 @@ public function getInfo(int $name)
133141
*
134142
* @return $this
135143
*/
136-
public function close(): Curl
144+
private function close(): Curl
137145
{
138146
curl_close($this->curl);
139147
return $this;

src/CurlTokenAuth.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace CloudPlayDev\ConfluenceClient;
5+
6+
7+
class CurlTokenAuth extends Curl
8+
{
9+
10+
public function __construct(string $host, string $token)
11+
{
12+
parent::__construct($host, '', '');
13+
14+
$this->setOptions([
15+
CURLOPT_HTTPAUTH => CURLAUTH_BEARER,
16+
CURLOPT_XOAUTH2_BEARER => $token,
17+
CURLOPT_USERPWD => null
18+
]);
19+
}
20+
}

src/Entity/ConfluencePage.php

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

1212
class ConfluencePage
1313
{
14-
private ?string $id = null;
14+
private ?int $id = null;
1515
private ?string $title = null;
1616
private ?string $space = null;
1717

@@ -22,7 +22,7 @@ class ConfluencePage
2222

2323
private ?string $content = null;
2424

25-
private ?int $version = null;
25+
private int $version = 1;
2626
/**
2727
* @var array<string, string> $children
2828
*/
@@ -50,18 +50,18 @@ public function setType(string $type): ConfluencePage
5050
}
5151

5252
/**
53-
* @return mixed
53+
* @return int|null
5454
*/
55-
public function getId()
55+
public function getId(): ?int
5656
{
5757
return $this->id;
5858
}
5959

6060
/**
61-
* @param string $id
61+
* @param int $id
6262
* @return ConfluencePage
6363
*/
64-
public function setId(string $id): ConfluencePage
64+
public function setId(int $id): ConfluencePage
6565
{
6666
$this->id = $id;
6767
return $this;
@@ -137,9 +137,9 @@ public function setContent(string $content): ConfluencePage
137137
}
138138

139139
/**
140-
* @return int|null
140+
* @return int
141141
*/
142-
public function getVersion(): ?int
142+
public function getVersion(): int
143143
{
144144
return $this->version;
145145
}

src/Exception.php renamed to src/Exception/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace CloudPlayDev\ConfluenceClient;
4+
namespace CloudPlayDev\ConfluenceClient\Exception;
55

66
use Exception as BaseException;
77

0 commit comments

Comments
 (0)