Skip to content

Commit

Permalink
Merge pull request #125 from deathiop/v2
Browse files Browse the repository at this point in the history
feat: allow /v1 or /v2 prefixes in path
  • Loading branch information
rbeuque74 authored Apr 7, 2023
2 parents 25cb8de + 73dab16 commit 9a29f72
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 368 deletions.
5 changes: 4 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<property name="lineLimit" value="180"/>
</properties>
</rule>
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<file>src</file>
<file>tests</file>
<arg name="encoding" value="utf-8"/>
</ruleset>
</ruleset>
23 changes: 20 additions & 3 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ public function requestCredentials(
return $response;
}

/**
* getTarget returns the URL to target given an endpoint and a path.
* If the path starts with `/v1` or `/v2`, then remove the trailing `/1.0` from the endpoint.
*
* @param string path to use prefix from
* @return string
*/
protected function getTarget($path) : string
{
$endpoint = $this->endpoint;
if (substr($endpoint, -4) == '/1.0' && (
substr($path, 0, 3) == '/v1' ||
substr($path, 0, 3) == '/v2')) {
$endpoint = substr($endpoint, 0, strlen($endpoint)-4);
}
return $endpoint . $path;
}

/**
* This is the main method of this wrapper. It will
* sign a given query and return its result.
Expand Down Expand Up @@ -238,7 +256,7 @@ protected function rawCall($method, $path, $content = null, $is_authenticated =
}
}

$url = $this->endpoint . $path;
$url = $this->getTarget($path);
$request = new Request($method, $url);
if (isset($content) && $method === 'GET') {
$query_string = $request->getUri()->getQuery();
Expand Down Expand Up @@ -280,9 +298,8 @@ protected function rawCall($method, $path, $content = null, $is_authenticated =
}
$headers['Content-Type'] = 'application/json; charset=utf-8';

$headers['X-Ovh-Application'] = $this->application_key ?? '';
if ($is_authenticated) {
$headers['X-Ovh-Application'] = $this->application_key;

if (!isset($this->time_delta)) {
$this->calculateTimeDelta();
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ApiFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ class ApiFunctionalTest extends TestCase
*/
protected function setUp() :void
{
foreach (['APP_KEY', 'APP_SECRET', 'CONSUMER', 'ENDPOINT'] as $envName) {
if (!getenv($envName)) {
$this->markTestSkipped("Skip test due to missing $envName variable");
return;
}
}

$this->application_key = getenv('APP_KEY');
$this->application_secret = getenv('APP_SECRET');
$this->consumer_key = getenv('CONSUMER');
Expand Down
Loading

0 comments on commit 9a29f72

Please sign in to comment.