Skip to content

Commit 2ab8546

Browse files
committed
feat: #30 api create folder
1 parent 476cf8c commit 2ab8546

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

app/Coding.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,33 @@ public function updateWikiTitle(string $token, string $projectName, int $id, str
201201
$result = json_decode($response->getBody(), true);
202202
return $result['Response']['Data']['Title'] == $title;
203203
}
204+
205+
/**
206+
* 创建网盘目录,不可重名,如已存在,仍然正常返回 id
207+
*
208+
* @param string $token
209+
* @param string $projectName
210+
* @param string $folderName
211+
* @param int $parentId
212+
* @return int
213+
* @throws \GuzzleHttp\Exception\GuzzleException
214+
*/
215+
public function createFolder(string $token, string $projectName, string $folderName, int $parentId): int
216+
{
217+
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
218+
'headers' => [
219+
'Accept' => 'application/json',
220+
'Authorization' => "token ${token}",
221+
'Content-Type' => 'application/json'
222+
],
223+
'json' => [
224+
'Action' => 'CreateFolder',
225+
'ProjectName' => $projectName,
226+
'FolderName' => $folderName,
227+
'ParentId' => $parentId,
228+
],
229+
]);
230+
$result = json_decode($response->getBody(), true);
231+
return $result['Response']['Data']['Id'];
232+
}
204233
}

tests/Unit/CodingTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,41 @@ public function testUpdateWikiTitle()
276276
$result = $coding->updateWikiTitle($codingToken, $codingProjectUri, $id, $title);
277277
$this->assertTrue($result);
278278
}
279+
280+
public function testCreateFolder()
281+
{
282+
$responseBody = file_get_contents($this->dataDir . 'coding/CreateFolderResponse.json');
283+
$codingToken = $this->faker->md5;
284+
$codingProjectUri = $this->faker->slug;
285+
$folderName = 'foo';
286+
$parentId = $this->faker->randomNumber();
287+
288+
$clientMock = $this->getMockBuilder(Client::class)->getMock();
289+
$clientMock->expects($this->exactly(2))
290+
->method('request')
291+
->with(
292+
'POST',
293+
'https://e.coding.net/open-api',
294+
[
295+
'headers' => [
296+
'Accept' => 'application/json',
297+
'Authorization' => "token ${codingToken}",
298+
'Content-Type' => 'application/json'
299+
],
300+
'json' => [
301+
'Action' => 'CreateFolder',
302+
'ProjectName' => $codingProjectUri,
303+
'FolderName' => $folderName,
304+
'ParentId' => $parentId,
305+
],
306+
]
307+
)
308+
->willReturn(new Response(200, [], $responseBody));
309+
$coding = new Coding($clientMock);
310+
$result = $coding->createFolder($codingToken, $codingProjectUri, $folderName, $parentId);
311+
$this->assertTrue(is_numeric($result));
312+
313+
$result = $coding->createFolder($codingToken, $codingProjectUri, $folderName, $parentId);
314+
$this->assertTrue(is_numeric($result));
315+
}
279316
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Response" : {
3+
"Data" : {
4+
"FolderName" : "foo",
5+
"Id" : 24515861
6+
},
7+
"RequestId" : "b43bdea4-0602-4266-e451-dfcc671c91f7"
8+
}
9+
}

0 commit comments

Comments
 (0)