From d4bd192c21c1eb74d5cc0f476a56bf1116bff2fd Mon Sep 17 00:00:00 2001 From: Stefan Deelstra Date: Tue, 19 Sep 2023 14:52:59 +0200 Subject: [PATCH 1/3] Added implementation of set_renew_option method --- src/Dynadot/DynadotApi.php | 54 +++++++++++++ .../SetRenewOptionHeader.php | 24 ++++++ .../SetRenewOptionResponse.php | 11 +++ .../renewOptionFailedResponse.txt | 6 ++ .../renewOptionInvalidResponse.txt | 2 + .../renewOptionSuccessResponse.txt | 6 ++ tests/ApiTests/SetRenewOptionTest.php | 78 +++++++++++++++++++ 7 files changed, 181 insertions(+) create mode 100644 src/Dynadot/ResultObjects/RenewOptionResponse/SetRenewOptionHeader.php create mode 100644 src/Dynadot/ResultObjects/RenewOptionResponse/SetRenewOptionResponse.php create mode 100644 tests/ApiTests/MockHttpResponses/renewOptionFailedResponse.txt create mode 100644 tests/ApiTests/MockHttpResponses/renewOptionInvalidResponse.txt create mode 100644 tests/ApiTests/MockHttpResponses/renewOptionSuccessResponse.txt create mode 100644 tests/ApiTests/SetRenewOptionTest.php diff --git a/src/Dynadot/DynadotApi.php b/src/Dynadot/DynadotApi.php index a16f7e2..2b40551 100644 --- a/src/Dynadot/DynadotApi.php +++ b/src/Dynadot/DynadotApi.php @@ -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 @@ -570,4 +572,56 @@ 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 = [ + '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; + } } diff --git a/src/Dynadot/ResultObjects/RenewOptionResponse/SetRenewOptionHeader.php b/src/Dynadot/ResultObjects/RenewOptionResponse/SetRenewOptionHeader.php new file mode 100644 index 0000000..b0fd8b9 --- /dev/null +++ b/src/Dynadot/ResultObjects/RenewOptionResponse/SetRenewOptionHeader.php @@ -0,0 +1,24 @@ + + + -1 + failed + + \ No newline at end of file diff --git a/tests/ApiTests/MockHttpResponses/renewOptionInvalidResponse.txt b/tests/ApiTests/MockHttpResponses/renewOptionInvalidResponse.txt new file mode 100644 index 0000000..3cd5099 --- /dev/null +++ b/tests/ApiTests/MockHttpResponses/renewOptionInvalidResponse.txt @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/tests/ApiTests/MockHttpResponses/renewOptionSuccessResponse.txt b/tests/ApiTests/MockHttpResponses/renewOptionSuccessResponse.txt new file mode 100644 index 0000000..2350677 --- /dev/null +++ b/tests/ApiTests/MockHttpResponses/renewOptionSuccessResponse.txt @@ -0,0 +1,6 @@ + + + 0 + success + + \ No newline at end of file diff --git a/tests/ApiTests/SetRenewOptionTest.php b/tests/ApiTests/SetRenewOptionTest.php new file mode 100644 index 0000000..0851f0e --- /dev/null +++ b/tests/ApiTests/SetRenewOptionTest.php @@ -0,0 +1,78 @@ +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); + } +} \ No newline at end of file From c8f8a617a41612987de2f7f4f53f9c20af6e976c Mon Sep 17 00:00:00 2001 From: Stefan Deelstra Date: Tue, 19 Sep 2023 15:06:55 +0200 Subject: [PATCH 2/3] Updated README.md --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 648808f..8389357 100644 --- a/README.md +++ b/README.md @@ -230,7 +230,28 @@ Level23\Dynadot\ResultObjects\GetContactResponse\Contact Object [Country] => country ) ``` - + +### Set renew option with `setRenewOption` + +```php +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! From 6b44537335acc8c01685b8a665f3d795c59fcc1c Mon Sep 17 00:00:00 2001 From: Stefan Deelstra Date: Tue, 19 Sep 2023 15:41:18 +0200 Subject: [PATCH 3/3] Added command to API call --- src/Dynadot/DynadotApi.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Dynadot/DynadotApi.php b/src/Dynadot/DynadotApi.php index 2b40551..1e634ce 100644 --- a/src/Dynadot/DynadotApi.php +++ b/src/Dynadot/DynadotApi.php @@ -584,6 +584,7 @@ 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, ];