Skip to content

Commit

Permalink
update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Nov 26, 2023
1 parent 00c1dbf commit 46a8378
Show file tree
Hide file tree
Showing 39 changed files with 2,031 additions and 1,665 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/composer.lock
/vendor
/.phpunit.result.cache
/.phpunit.cache/test-results
20 changes: 10 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
},
"require": {
"php": "~8.2",
"innmind/time-continuum": "~2.0",
"innmind/time-continuum": "~3.4",
"ramsey/uuid": "^3.8|^4.0",
"innmind/http-transport": "~5.0",
"innmind/http-transport": "~7.1",
"innmind/json": "^1.1",
"innmind/filesystem": "~4.0",
"innmind/immutable": "~3.3",
"innmind/ip": "~2.0",
"innmind/url": "^3.5.1"
"innmind/filesystem": "~7.3",
"innmind/immutable": "~5.2",
"innmind/ip": "~3.2",
"innmind/url": "~4.3"
},
"autoload": {
"psr-4": {
Expand All @@ -37,10 +37,10 @@
}
},
"require-dev": {
"phpunit/phpunit": "~9.0",
"innmind/object-graph": "~2.0",
"vimeo/psalm": "~4.4",
"innmind/black-box": "^4.16",
"phpunit/phpunit": "~10.2",
"innmind/object-graph": "~3.2",
"vimeo/psalm": "~5.5",
"innmind/black-box": "~5.5",
"innmind/coding-standard": "~2.0"
}
}
21 changes: 13 additions & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" printerClass="Innmind\BlackBox\PHPUnit\ResultPrinterV9" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache">
<extensions>
<bootstrap class="Innmind\BlackBox\PHPUnit\Extension">
</bootstrap>
</extensions>
<coverage/>
<testsuites>
<testsuite name="Test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>.</directory>
</include>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</source>
</phpunit>
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
findUnusedCode="false"
findUnusedBaselineEntry="true"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
145 changes: 77 additions & 68 deletions src/Authenticated/IPs/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@
};
use Innmind\HttpTransport\Transport;
use Innmind\Http\{
Message\Request\Request,
Message\Method,
Request,
Method,
ProtocolVersion,
Headers,
Header\ContentType,
Header\Link,
Header\LinkValue,
};
use Innmind\Filesystem\File\Content;
use Innmind\Url\Url;
use Innmind\Json\Json;
use Innmind\Stream\Readable\Stream;
use Innmind\IP\{
IPv4,
IPv6,
Exception\AddressNotMatchingIPv4Format,
};
use Innmind\Immutable\Set;
use function Innmind\Immutable\first;
use Innmind\Immutable\{
Set,
Predicate\Instance,
};

final class Http implements IPs
{
Expand All @@ -51,18 +53,21 @@ public function __construct(
public function create(Organization\Id $organization): IP
{
/** @psalm-suppress InvalidArgument */
$response = ($this->fulfill)(new Request(
$response = ($this->fulfill)(Request::of(
Url::of("https://cp-{$this->region->toString()}.scaleway.com/ips"),
Method::post(),
new ProtocolVersion(2, 0),
Method::post,
ProtocolVersion::v20,
Headers::of(
new AuthToken($this->token),
AuthToken::of($this->token),
ContentType::of('application', 'json'),
),
Stream::ofContent(Json::encode([
Content::ofString(Json::encode([
'organization' => $organization->toString(),
])),
));
))->match(
static fn($success) => $success->response(),
static fn() => throw new \RuntimeException,
);

/** @var array{ip: array{address: string, id: string, organization: string, server: ?array{id: string}}} */
$body = Json::decode($response->body()->toString());
Expand All @@ -77,62 +82,59 @@ public function list(): Set
$ips = [];

do {
$response = ($this->fulfill)(new Request(
$response = ($this->fulfill)(Request::of(
$url,
Method::get(),
new ProtocolVersion(2, 0),
Method::get,
ProtocolVersion::v20,
Headers::of(
new AuthToken($this->token),
AuthToken::of($this->token),
),
));
))->match(
static fn($success) => $success->response(),
static fn() => throw new \RuntimeException,
);

/** @var array{ips: list<array{address: string, id: string, organization: string, server: ?array{id: string}}>} */
$body = Json::decode($response->body()->toString());
$ips = \array_merge($ips, $body['ips']);
$next = null;

if ($response->headers()->contains('Link')) {
/**
* @psalm-suppress ArgumentTypeCoercion
* @var Set<LinkValue>
*/
$next = $response
->headers()
->get('Link')
->values()
->filter(static function(LinkValue $link): bool {
return $link->relationship() === 'next';
});

if ($next->size() === 1) {
$next = $url
->withPath(first($next)->url()->path())
->withQuery(first($next)->url()->query());
$url = $next;
}
}
} while ($next instanceof Url);

/** @var Set<IP> */
$set = Set::of(IP::class);

foreach ($ips as $ip) {
$set = ($set)($this->decode($ip));
}
$url = $response
->headers()
->find(Link::class)
->flatMap(
static fn($header) => $header
->values()
->keep(Instance::of(LinkValue::class))
->find(static fn($link) => $link->relationship() === 'next'),
)
->map(
static fn($link) => $url
->withPath($link->url()->path())
->withQuery($link->url()->query()),
)
->match(
static fn($next) => $next,
static fn() => null,
);
} while ($url instanceof Url);

return $set;
/** @var Set<IP> */
return Set::of(...$ips)->map($this->decode(...));
}

public function get(IP\Id $id): IP
{
$response = ($this->fulfill)(new Request(
$response = ($this->fulfill)(Request::of(
Url::of("https://cp-{$this->region->toString()}.scaleway.com/ips/{$id->toString()}"),
Method::get(),
new ProtocolVersion(2, 0),
Method::get,
ProtocolVersion::v20,
Headers::of(
new AuthToken($this->token),
AuthToken::of($this->token),
),
));
))->match(
static fn($success) => $success->response(),
static fn() => throw new \RuntimeException,
);

/** @var array{ip: array{address: string, id: string, organization: string, server: ?array{id: string}}} */
$body = Json::decode($response->body()->toString());
Expand All @@ -142,31 +144,37 @@ public function get(IP\Id $id): IP

public function remove(IP\Id $id): void
{
($this->fulfill)(new Request(
($this->fulfill)(Request::of(
Url::of("https://cp-{$this->region->toString()}.scaleway.com/ips/{$id->toString()}"),
Method::delete(),
new ProtocolVersion(2, 0),
Method::delete,
ProtocolVersion::v20,
Headers::of(
new AuthToken($this->token),
AuthToken::of($this->token),
),
));
))->match(
static fn($success) => $success->response(),
static fn() => throw new \RuntimeException,
);
}

public function attach(IP\Id $id, Server\Id $server): IP
{
/** @psalm-suppress InvalidArgument */
$response = ($this->fulfill)(new Request(
$response = ($this->fulfill)(Request::of(
Url::of("https://cp-{$this->region->toString()}.scaleway.com/ips/{$id->toString()}"),
Method::patch(),
new ProtocolVersion(2, 0),
Method::patch,
ProtocolVersion::v20,
Headers::of(
new AuthToken($this->token),
AuthToken::of($this->token),
ContentType::of('application', 'json'),
),
Stream::ofContent(Json::encode([
Content::ofString(Json::encode([
'server' => $server->toString(),
])),
));
))->match(
static fn($success) => $success->response(),
static fn() => throw new \RuntimeException,
);

/** @var array{ip: array{address: string, id: string, organization: string, server: ?array{id: string}}} */
$body = Json::decode($response->body()->toString());
Expand All @@ -180,11 +188,12 @@ public function attach(IP\Id $id, Server\Id $server): IP
*/
private function decode(array $ip): IP
{
try {
$address = IPv4::of($ip['address']);
} catch (AddressNotMatchingIPv4Format $e) {
$address = IPv6::of($ip['address']);
}
$address = IPv4::maybe($ip['address'])
->otherwise(static fn() => IPv6::maybe($ip['address']))
->match(
static fn($ip) => $ip,
static fn() => throw new \RuntimeException,
);

return new IP(
new IP\Id($ip['id']),
Expand Down

0 comments on commit 46a8378

Please sign in to comment.