Skip to content

Commit 2d626d4

Browse files
authored
Merge pull request #7 from packagist/t/package-credential-endpoints
Package & Credentials endpoints
2 parents 0a55973 + 006b34a commit 2d626d4

File tree

7 files changed

+404
-0
lines changed

7 files changed

+404
-0
lines changed

Diff for: README.md

+88
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,94 @@ $packages = $client->packages()->all($filters);
117117
```
118118
Returns an array of packages.
119119

120+
##### Show a package
121+
```php
122+
$package = $client->packages()->show('acme-website/package');
123+
```
124+
Returns the package.
125+
126+
##### Create a vcs package
127+
```php
128+
$job = $client->packages()->createVcsPackage('https://github.com/acme-website/package');
129+
```
130+
Returns a new job.
131+
132+
##### Create a vcs package with credentials
133+
```php
134+
$credentialId = 42;
135+
$job = $client->packages()->createVcsPackage('https://github.com/acme-website/package', $credentialId);
136+
```
137+
Returns a new job.
138+
139+
##### Create a custom package
140+
```php
141+
$packageDefinition = '{...}'
142+
$job = $client->packages()->createCustomPackage($packageDefinition);
143+
```
144+
Returns a new job.
145+
146+
##### Create a custom package with credentials
147+
```php
148+
$packageDefinition = '{...}'
149+
$credentialId = 42;
150+
$job = $client->packages()->createCustomPackage($packageDefinition, $credentialId);
151+
```
152+
Returns a new job.
153+
154+
##### Update a vcs package
155+
```php
156+
$job = $client->packages()->updateVcsPackage('acme-website/package', 'https://github.com/acme-website/package');
157+
```
158+
Returns a new job.
159+
160+
##### Update a custom package
161+
```php
162+
$packageDefinition = '{...}'
163+
$job = $client->packages()->updateCustomPackage('acme-website/package', $packageDefinition);
164+
```
165+
Returns a new job.
166+
167+
##### Delete a package
168+
```php
169+
$client->packages()->remove('acme-website/package');
170+
```
171+
172+
#### Credential
173+
174+
##### List an organization's credentials
175+
```php
176+
$packages = $client->credentials()->all();
177+
```
178+
Returns an array of credentials.
179+
180+
##### Show a credential
181+
```php
182+
$credentialId = 42;
183+
$credential = $client->credentials()->show($credentialId);
184+
```
185+
Returns the credential.
186+
187+
##### Create a credential
188+
```php
189+
$type = \PrivatePackagist\ApiClient\Api\Credentials::TYPE_HTTP_BASIC;
190+
$credential = $client->credentials()->create('ACME http auth', $type, 'acme-website.com', 'username', 'password');
191+
```
192+
Returns the new credential.
193+
194+
##### Update a credential
195+
```php
196+
$credentialId = 42;
197+
$type = \PrivatePackagist\ApiClient\Api\Credentials::TYPE_HTTP_BASIC;
198+
$credential = $client->credentials()->create($credentialId, $type, 'username', 'password');
199+
```
200+
Returns the updated credential.
201+
202+
##### Delete a credential
203+
```php
204+
$credentialId = 42;
205+
$client->credentials()->remove($credentialId);
206+
```
207+
120208
#### Job
121209

122210
##### Show a job

Diff for: src/Api/AbstractApi.php

+20
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ protected function post($path, array $parameters = [], array $headers = [])
6060
return $this->responseMediator->getContent($response);
6161
}
6262

63+
/**
64+
* @param string $path
65+
* @param array $parameters
66+
* @param array $headers
67+
* @return array|string
68+
*/
69+
protected function put($path, array $parameters = [], array $headers = [])
70+
{
71+
$response = $this->client->getHttpClient()->put(
72+
$path,
73+
array_merge($headers, [
74+
'Accept' => 'application/json',
75+
'Content-Type' => 'application/json',
76+
]),
77+
$this->createJsonBody($parameters)
78+
);
79+
80+
return $this->responseMediator->getContent($response);
81+
}
82+
6383
/**
6484
* @param string $path
6585
* @param array $parameters

Diff for: src/Api/Credentials.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Api;
4+
5+
class Credentials extends AbstractApi
6+
{
7+
const TYPE_BITBUCKET_APP_PW = 'bitbucket-app-pw';
8+
const TYPE_GITHUB_OAUTH = 'github-oauth';
9+
const TYPE_GITLAB_TOKEN = 'gitlab-token';
10+
const TYPE_HTTP_BASIC = 'http-basic';
11+
const TYPE_MAGENTO = 'magento';
12+
const TYPE_HTTP_HEADER = 'http-header';
13+
14+
public function all()
15+
{
16+
return $this->get('/credentials/');
17+
}
18+
19+
public function show($credentialId)
20+
{
21+
return $this->get(sprintf('/credentials/%s/', $credentialId));
22+
}
23+
24+
public function create($description, $type, $domain, $username, $credential)
25+
{
26+
return $this->post('/credentials/', [
27+
'description' => $description,
28+
'type' => $type,
29+
'domain' => $domain,
30+
'username' => $username,
31+
'credential' => $credential,
32+
]);
33+
}
34+
35+
public function update($credentialId, $type, $username, $credential)
36+
{
37+
return $this->put(sprintf('/credentials/%s/', $credentialId), [
38+
'username' => $username,
39+
'credential' => $credential,
40+
'type' => $type,
41+
]);
42+
}
43+
44+
public function remove($credentialId)
45+
{
46+
return $this->delete(sprintf('/credentials/%s/', $credentialId));
47+
}
48+
}

Diff for: src/Api/Packages.php

+30
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,34 @@ public function all(array $filters = [])
3030

3131
return $this->get('/packages/', $filters);
3232
}
33+
34+
public function show($packageName)
35+
{
36+
return $this->get(sprintf('/packages/%s/', $packageName));
37+
}
38+
39+
public function createVcsPackage($url, $credentials = null)
40+
{
41+
return $this->post('/packages/', ['repoType' => 'vcs', 'repoUrl' => $url, 'credentials' => $credentials]);
42+
}
43+
44+
public function createCustomPackage($customJson, $credentials = null)
45+
{
46+
return $this->post('/packages/', ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentials]);
47+
}
48+
49+
public function updateVcsPackage($packageName, $url, $credentials = null)
50+
{
51+
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'vcs', 'repoUrl' => $url, 'credentials' => $credentials]);
52+
}
53+
54+
public function updateCustomPackage($packageName, $customJson, $credentials = null)
55+
{
56+
return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentials]);
57+
}
58+
59+
public function remove($packageName)
60+
{
61+
return $this->delete(sprintf('/packages/%s/', $packageName));
62+
}
3363
}

Diff for: src/Client.php

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function authenticate($token, $secret)
4343
$this->httpClientBuilder->addPlugin(new RequestSignature($token, $secret));
4444
}
4545

46+
public function credentials()
47+
{
48+
return new Api\Credentials($this, $this->responseMediator);
49+
}
50+
4651
public function customers()
4752
{
4853
return new Api\Customers($this, $this->responseMediator);

Diff for: tests/Api/CredentialsTest.php

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Api;
4+
5+
class CredentialsTest extends ApiTestCase
6+
{
7+
public function testAll()
8+
{
9+
$expected = [
10+
[
11+
'id' => 1,
12+
'description' => 'My secret credential',
13+
'domain' => 'localhost',
14+
'username' => 'username',
15+
'credential' => 'password',
16+
'type' => 'http-basic',
17+
],
18+
];
19+
20+
/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
21+
$api = $this->getApiMock();
22+
$api->expects($this->once())
23+
->method('get')
24+
->with($this->equalTo('/credentials/'))
25+
->will($this->returnValue($expected));
26+
27+
$this->assertSame($expected, $api->all());
28+
}
29+
30+
public function testShow()
31+
{
32+
$expected = [
33+
'id' => 1,
34+
'description' => 'My secret credential',
35+
'domain' => 'localhost',
36+
'username' => 'username',
37+
'credential' => 'password',
38+
'type' => 'http-basic',
39+
];
40+
41+
/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
42+
$api = $this->getApiMock();
43+
$api->expects($this->once())
44+
->method('get')
45+
->with($this->equalTo('/credentials/1/'))
46+
->will($this->returnValue($expected));
47+
48+
$this->assertSame($expected, $api->show(1));
49+
}
50+
51+
public function testCreate()
52+
{
53+
$expected = [
54+
'id' => 1,
55+
'description' => 'My secret credential',
56+
'domain' => 'localhost',
57+
'username' => 'username',
58+
'credential' => 'password',
59+
'type' => 'http-basic',
60+
];
61+
62+
/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
63+
$api = $this->getApiMock();
64+
$api->expects($this->once())
65+
->method('post')
66+
->with($this->equalTo('/credentials/'), $this->equalTo(['domain' => 'localhost', 'description' => 'My secret credential', 'type' => 'http-basic', 'username' => 'username', 'credential' => 'password']))
67+
->will($this->returnValue($expected));
68+
69+
$this->assertSame($expected, $api->create('My secret credential', 'http-basic', 'localhost', 'username', 'password'));
70+
}
71+
72+
public function testUpdate()
73+
{
74+
$expected = [
75+
'id' => 1,
76+
'description' => 'My secret credential',
77+
'domain' => 'localhost',
78+
'username' => 'username',
79+
'credential' => 'password',
80+
'type' => 'http-basic',
81+
];
82+
83+
/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
84+
$api = $this->getApiMock();
85+
$api->expects($this->once())
86+
->method('put')
87+
->with($this->equalTo('/credentials/1/'), $this->equalTo(['type' => 'http-basic', 'username' => 'username', 'credential' => 'password']))
88+
->will($this->returnValue($expected));
89+
90+
$this->assertSame($expected, $api->update(1, 'http-basic', 'username', 'password'));
91+
}
92+
93+
public function testRemove()
94+
{
95+
$expected = [];
96+
97+
/** @var Customers&\PHPUnit_Framework_MockObject_MockObject $api */
98+
$api = $this->getApiMock();
99+
$api->expects($this->once())
100+
->method('delete')
101+
->with($this->equalTo('/credentials/1/'))
102+
->will($this->returnValue($expected));
103+
104+
$this->assertSame($expected, $api->remove(1));
105+
}
106+
107+
/**
108+
* @return string
109+
*/
110+
protected function getApiClass()
111+
{
112+
return Credentials::class;
113+
}
114+
}

0 commit comments

Comments
 (0)