Skip to content

Commit

Permalink
Merge pull request #7 from level23/set-renew-option
Browse files Browse the repository at this point in the history
Added implementation of set_renew_option method
  • Loading branch information
teyeheimans authored Sep 19, 2023
2 parents 8dd42d3 + 6b44537 commit de0b459
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 1 deletion.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,28 @@ Level23\Dynadot\ResultObjects\GetContactResponse\Contact Object
[Country] => country
)
```


### Set renew option with `setRenewOption`

```php
<?php

use Level23\Dynadot\DynadotApi;

$apiKey = 'xxx YOUR API KEY xxx';

try {
$api = new DynadotApi($apiKey);
$api->setRenewOption('example.com', 'auto');
// ...
} catch (Exception $e) {
// ... handle exception
}
```
The `setRenewOption` let's you set the renewal setting for a domain. Values of for the second
argument ($renewOption) van be "donot", "auto", "reset". The method will return `true` when
the renew option is set successfully.

# FAQ

## I keep getting timeouts!
Expand Down
55 changes: 55 additions & 0 deletions src/Dynadot/DynadotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Level23\Dynadot\Exception\ApiHttpCallFailedException;
use Level23\Dynadot\ResultObjects\ListDomainInfoResponse;
use Level23\Dynadot\Exception\ApiLimitationExceededException;
use Level23\Dynadot\ResultObjects\RenewOptionResponse\SetRenewOptionHeader;
use Level23\Dynadot\ResultObjects\RenewOptionResponse\SetRenewOptionResponse;

/**
* Class DynadotApi
Expand Down Expand Up @@ -570,4 +572,57 @@ public function getContactInfo(int $contactId): GetContactResponse\Contact

return $resultData->GetContactContent->Contact;
}

/**
* @throws \Level23\Dynadot\Exception\DynadotApiException
* @throws \Sabre\Xml\ParseException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Level23\Dynadot\Exception\ApiHttpCallFailedException
*/
public function setRenewOption(string $domain, string $renewOption): bool
{
$this->log(LogLevel::INFO, 'Set auto renew for: ' . $domain . ' to: ' . $renewOption);

$requestData = [
'command' => 'set_renew_option',
'domain' => $domain,
'renew_option' => $renewOption,
];

// perform the API call
$response = $this->performRawApiCall($requestData);

$this->log(LogLevel::DEBUG, 'Start parsing result');

// start parsing XML data using Sabre
$sabreService = new Service();

// map certain values to objects
$sabreService->mapValueObject('{}SetRenewOptionResponse', SetRenewOptionResponse::class);
$sabreService->mapValueObject('{}SetRenewOptionHeader', SetRenewOptionHeader::class);
$sabreService->mapValueObject('{}Response', GeneralResponse\Response::class);
$sabreService->mapValueObject('{}ResponseHeader', GeneralResponse\ResponseHeader::class);

// parse the data
$resultData = $sabreService->parse($response->getContents());

// General error, like incorrect api key
if ($resultData instanceof GeneralResponse\Response) {
$code = $resultData->ResponseHeader->ResponseCode;
if ($code != GeneralResponse\ResponseHeader::RESPONSECODE_OK) {
throw new DynadotApiException($resultData->ResponseHeader->Error);
}
}

if (!$resultData instanceof SetRenewOptionResponse) {
throw new DynadotApiException('We failed to parse the response');
}

$code = $resultData->SetRenewOptionHeader->SuccessCode;
if ($code != SetRenewOptionHeader::SUCCESSCODE_OK) {
throw new DynadotApiException((string)$resultData->SetRenewOptionHeader->Error);
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Level23\Dynadot\ResultObjects\RenewOptionResponse;

class SetRenewOptionHeader
{
/**
* @var int
*/
public $SuccessCode;

/**
* @var string
*/
public $Status;

/**
* @var string
*/
public $Error;

const SUCCESSCODE_OK = 0;
const SUCCESSCODE_FAILURE = -1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Level23\Dynadot\ResultObjects\RenewOptionResponse;

class SetRenewOptionResponse
{
/**
* @var \Level23\Dynadot\ResultObjects\RenewOptionResponse\SetRenewOptionHeader
*/
public $SetRenewOptionHeader;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<SetRenewOptionResponse>
<SetRenewOptionHeader>
<SuccessCode>-1</SuccessCode>
<Status>failed</Status>
</SetRenewOptionHeader>
</SetRenewOptionResponse>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<SetRenewOptionResponseInvalid>
</SetRenewOptionResponseInvalid>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<SetRenewOptionResponse>
<SetRenewOptionHeader>
<SuccessCode>0</SuccessCode>
<Status>success</Status>
</SetRenewOptionHeader>
</SetRenewOptionResponse>
78 changes: 78 additions & 0 deletions tests/ApiTests/SetRenewOptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Level23\Dynadot\Tests\ApiTests;

use Level23\Dynadot\DynadotApi;
use Level23\Dynadot\Exception\DynadotApiException;

class SetRenewOptionTest extends TestCase
{
/**
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Sabre\Xml\ParseException
* @throws \Level23\Dynadot\Exception\ApiHttpCallFailedException
*/
public function testFailedResponse(): void
{
$api = new DynadotApi('_API_KEY_GOES_HERE_');

$mockHandler = $this->getMockedResponse('renewOptionFailedResponse.txt');

$api->setGuzzleOptions(['handler' => $mockHandler]);

$this->expectException(DynadotApiException::class);

$api->setRenewOption('example.com', 'auto');
}

public function testInvalidResponse(): void
{
$api = new DynadotApi('_API_KEY_GOES_HERE_');

$mockHandler = $this->getMockedResponse('renewOptionInvalidResponse.txt');

$api->setGuzzleOptions(['handler' => $mockHandler]);

$this->expectException(DynadotApiException::class);
$this->expectExceptionMessage('We failed to parse the response');

$api->setRenewOption('example.com', 'auto');
}

/**
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Sabre\Xml\ParseException
* @throws \Level23\Dynadot\Exception\ApiHttpCallFailedException
*/
public function testInvalidGeneralResponse(): void
{
$api = new DynadotApi('_API_KEY_GOES_HERE_');

$mockHandler = $this->getMockedResponse('invalidApiKeyResponse.txt');

$api->setGuzzleOptions(['handler' => $mockHandler]);

$this->expectException(DynadotApiException::class);

$api->setRenewOption('example.com', 'auto');
}

/**
* @throws \Level23\Dynadot\Exception\DynadotApiException
* @throws \Sabre\Xml\ParseException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Level23\Dynadot\Exception\ApiHttpCallFailedException
*/
public function testSuccessResponse(): void
{
$api = new DynadotApi('_API_KEY_GOES_HERE_');

$mockHandler = $this->getMockedResponse('renewOptionSuccessResponse.txt');

$api->setGuzzleOptions(['handler' => $mockHandler]);

$result = $api->setRenewOption('example.com', 'auto');

$this->assertTrue($result);
}
}

0 comments on commit de0b459

Please sign in to comment.