diff --git a/CHANGELOG.md b/CHANGELOG.md index 261e75b..6a9e008 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,3 +43,12 @@ This release brings support for the `swarm/peers` command. - Added the `IPFSClient::getPeers` method, which returns a list of all the node's connected peers. - Added `Peer`, `PeerIdentity`, `PeerStream` models and corresponding transformers - Added corresponding tests for the new transformers and methods. + +## v1.3.0 + +This release brings support for the `resolve` command. + +#### Additions + +- Added the `IPFSClient::resolve` method, which returns the path to a given IPFS name. + - Added corresponding tests for the new method. diff --git a/src/Client/IPFSClient.php b/src/Client/IPFSClient.php index 91b850b..7420834 100644 --- a/src/Client/IPFSClient.php +++ b/src/Client/IPFSClient.php @@ -219,4 +219,22 @@ public function getPeers(bool $verbose = false): array ); return $peerTransformer->transformList($parsedResponse['Peers'] ?? []); } + + public function resolve(string $name, bool $recursive = true): string + { + $response = $this->httpClient->request('POST', '/api/v0/resolve', [ + 'query' => [ + 'arg' => $name, + 'recursive' => $recursive, + ], + ]); + + $parsedResponse = json_decode($response, true); + + if (isset($parsedResponse['Path']) === false) { + throw new \UnexpectedValueException('Unable to resolve path.'); + } + + return (string) $parsedResponse['Path']; + } } diff --git a/tests/Client/IPFSClientTest.php b/tests/Client/IPFSClientTest.php index ffea1ae..0ccc959 100644 --- a/tests/Client/IPFSClientTest.php +++ b/tests/Client/IPFSClientTest.php @@ -440,4 +440,29 @@ private function assertPeerResponse(array $data, Peer $peer): void $this->assertSame($data['Identify']['Addresses'], $identity->addresses); $this->assertSame($data['Identify']['Protocols'], $identity->protocols); } + + /** + * @covers ::resolve + */ + public function testResolve(): void + { + $mockReturn = [ + 'Path' => '/ipfs/QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D', + ]; + + $this->httpClient + ->expects($this->once()) + ->method('request') + ->with('POST', '/api/v0/resolve', [ + 'query' => [ + 'arg' => 'QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D', + 'recursive' => true, + ], + ]) + ->willReturn(json_encode($mockReturn)); + + $result = $this->client->resolve('QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D'); + + $this->assertSame($mockReturn['Path'], $result); + } }