Skip to content

Commit 6fb667c

Browse files
committed
feat: #30 api create file
1 parent 2ab8546 commit 6fb667c

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

app/Coding.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,29 @@ public function createFolder(string $token, string $projectName, string $folderN
230230
$result = json_decode($response->getBody(), true);
231231
return $result['Response']['Data']['Id'];
232232
}
233+
234+
/**
235+
* @param string $token
236+
* @param string $projectName
237+
* @param array $data
238+
* @return int
239+
* @throws \GuzzleHttp\Exception\GuzzleException
240+
* @todo data 数组无法强类型校验内部字段,考虑用对象
241+
*/
242+
public function createFile(string $token, string $projectName, array $data): array
243+
{
244+
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
245+
'headers' => [
246+
'Accept' => 'application/json',
247+
'Authorization' => "token ${token}",
248+
'Content-Type' => 'application/json'
249+
],
250+
'json' => array_merge([
251+
'Action' => 'CreateFile',
252+
'ProjectName' => $projectName,
253+
], $data),
254+
]);
255+
$result = json_decode($response->getBody(), true);
256+
return $result['Response']['Data'];
257+
}
233258
}

tests/Unit/CodingTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,43 @@ public function testCreateFolder()
313313
$result = $coding->createFolder($codingToken, $codingProjectUri, $folderName, $parentId);
314314
$this->assertTrue(is_numeric($result));
315315
}
316+
317+
public function testCreateFile()
318+
{
319+
$responseBody = file_get_contents($this->dataDir . 'coding/CreateFileResponse.json');
320+
$codingToken = $this->faker->md5;
321+
$codingProjectUri = $this->faker->slug;
322+
$data = [
323+
"OriginalFileName" => "foo.pdf",
324+
"MimeType" => "application/pdf",
325+
"FileSize" => 123456,
326+
"StorageKey" => "b5d0d8e0-3aca-11eb-8673-a9b6d94ca755.pdf",
327+
"Time" => 1625579588693,
328+
"AuthToken" => "65e5968b5e17d5aaa3f5d33200aca2d1911fe2ad2948b47d899d46e6da1e4",
329+
"FolderId" => 24515861,
330+
];
331+
332+
$clientMock = $this->getMockBuilder(Client::class)->getMock();
333+
$clientMock->expects($this->once())
334+
->method('request')
335+
->with(
336+
'POST',
337+
'https://e.coding.net/open-api',
338+
[
339+
'headers' => [
340+
'Accept' => 'application/json',
341+
'Authorization' => "token ${codingToken}",
342+
'Content-Type' => 'application/json'
343+
],
344+
'json' => array_merge([
345+
'Action' => 'CreateFile',
346+
'ProjectName' => $codingProjectUri,
347+
], $data)
348+
]
349+
)
350+
->willReturn(new Response(200, [], $responseBody));
351+
$coding = new Coding($clientMock);
352+
$result = $coding->createFile($codingToken, $codingProjectUri, $data);
353+
$this->assertArrayHasKey('FileId', $result);
354+
}
316355
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Response" : {
3+
"Data" : {
4+
"FileId": 123,
5+
"ResourceCode": 456,
6+
"FileName": "foo.pdf"
7+
},
8+
"RequestId" : "b43bdea4-0602-4266-e451-dfcc671c91f9"
9+
}
10+
}

0 commit comments

Comments
 (0)