owncloud / ocis-php-sdk

Apache License 2.0
3 stars 1 forks source link

added code for getting current oCis version #229

Closed S-Panta closed 3 months ago

S-Panta commented 3 months ago

New API endpoint could be added in a different version of ocis. oCIS will throw 404 status code , that leads to confusion, whether api request has problem or api endpoint has not implemented. This PR adds capability api endpoint to get current version of oCIS so that it would be easy to dissect the methods to use in different version of oCIS

individual-it commented 3 months ago

Why does the function need to be public? What about making it private and only using it internally when an OCIS object is initiated and setting a version in a private variable. Then when we contact a endpoint that we know is not available in that version, we can throw an exception

phil-davis commented 3 months ago

Why does the function need to be public?

IMO it does not need to be public, consumers of the SDK do not need to be able to get the ocis version, anything related to differences of the ocis version should be handled internally.

The function should be used to fetch and remember the ocis version.

@S-Panta you could also add code in this PR to do "throw an exception about API endpoint not implemented on ocis version less than 6.0.0 " for the existing code that was first introduced in PR #225 - that way the new getOcisVersion will be used.

phil-davis commented 3 months ago

https://drone.owncloud.com/owncloud/ocis-php-sdk/1578/5/3 code-style problems. Please check code-style locally before pushing to GitHub.

amrita-shrestha commented 3 months ago

maybe this wil pass unit tests

private function getGuzzleMock(?string $responseContent = null): MockObject
    {
        if ($responseContent === null) {
            $responseContent = '
                {
                    "data": {
                        "capabilities": {
                            "core": {
                                "status": {
                                    "productversion": "6.0.0+aa6041abb6"
                               }
                            }
                        }
                    }
                }';
        }

        $streamMock = $this->createMock(StreamInterface::class);
        $streamMock->method('getContents')->willReturn($responseContent);
        $responseMock = $this->createMock(ResponseInterface::class);
        $responseMock->method('getBody')->willReturn($streamMock);
        $guzzleMock = $this->createMock(Client::class);
        $guzzleMock->method('get')
            ->willReturn($responseMock);
        return $guzzleMock;
    }

    public function testSetAccessTokenPropagatesToDrives(): void
    {
        $driveMock = [];
        $driveMock[] = $this->createMock(Drive::class);
        $driveMock[] = $this->createMock(Drive::class);
        $driveCollectionMock = $this->createMock(CollectionOfDrives::class);
        $driveCollectionMock->method('getValue')
            ->willReturn($driveMock);
        $drivesGetDrivesApi = $this->createMock(DrivesGetDrivesApi::class);
        assert($drivesGetDrivesApi instanceof DrivesGetDrivesApi);
        $drivesGetDrivesApi->method('listAllDrives')
            ->willReturn($driveCollectionMock);
        $ocis = new Ocis(
            'https://localhost:9200',
            'tokenWhenCreated',
            [ 'guzzle' => $this->getGuzzleMock() ,
                'drivesGetDrivesApi' => $drivesGetDrivesApi]
        );
        $drives = $ocis->getAllDrives();
        foreach ($drives as $drive) {
            $this->assertSame('tokenWhenCreated', $drive->getAccessToken());
        }
        $ocis->setAccessToken('changedToken');
        foreach ($drives as $drive) {
            $this->assertSame('changedToken', $drive->getAccessToken());
        }
    }
sonarcloud[bot] commented 3 months ago

Quality Gate Passed Quality Gate passed

Issues
3 New issues
0 Accepted issues

Measures
0 Security Hotspots
91.7% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

phil-davis commented 3 months ago

Good - we can reuse a lot of this code when implementing the other root endpoints, where we need to know the ocis version etc.