Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Hardening API failure issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Clifford committed Aug 21, 2020
1 parent e0abe4c commit 6b3fdaa
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
55 changes: 40 additions & 15 deletions src/Fastly/Types/FastlyCertificates.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ class FastlyCertificates extends FastlyRequest
public function get_tls_certificate($id = '')
{
$endpoint = $this->build_endpoint('tls/certificates/' . $id);
$certificate_response = $this->send('GET', $endpoint);

try {
$certificate_response = $this->send('GET', $endpoint);
} catch (RequestException $e) {
$this->error = $e;
return $e->getMessage();
}

if ($certificate_response !== null || $certificate_response != []) {
$output = $this->build_output($certificate_response);
Expand All @@ -41,10 +47,16 @@ public function get_tls_certificates($filter = [])
{
$filter_encoded_query = http_build_query($filter);
$endpoint = !empty($filter) ? 'tls/certificates?'.$filter_encoded_query : 'tls/certificates';
$certificates_response = $this->send('GET', $this->build_endpoint($endpoint));

try {
$certificates_response = $this->send('GET', $this->build_endpoint($endpoint));
} catch (RequestException $e) {
$this->error = $e;
return $e->getMessage();
}

$certificates = [];
if ($certificates_response !== null || $certificates_response != []) {
if (!isEmpty($certificates_response)) {
$output = $this->build_output($certificates_response);
$this->data = $output['data'];
$this->meta = $output['meta'];
Expand All @@ -55,6 +67,9 @@ public function get_tls_certificates($filter = [])

$certificates['meta'][] = $this->meta;
}
else {
return ['data' => "No certificates returned."];
}

return $certificates;
}
Expand All @@ -68,13 +83,19 @@ public function get_tls_certificates($filter = [])
public function getTLSBulkCertificate($id = '')
{
$endpoint = $this->build_endpoint('tls/bulk/certificates/' . $id);
$certificate_response = $this->send('GET', $endpoint);

try {
$certificate_response = $this->send('GET', $endpoint);
} catch (RequestException $e) {
$this->error = $e;
return $e->getMessage();
}

if ($certificate_response !== null && $certificate_response != []) {
$output = $this->build_output($certificate_response);
$this->data = $output['data'];
$output = $this->build_output($certificate_response);
$this->data = $output['data'];

return new FastlyBulkCertificate($output['data']);
return new FastlyBulkCertificate($output['data']);
}
}

Expand All @@ -87,16 +108,20 @@ public function getTLSBulkCertificate($id = '')
public function getTLSBulkCertificates($options = '')
{
if ($options === '' || $options === null) {
$certificates_response = $this->send(
'GET',
$this->build_endpoint('tls/bulk/certificates')
);
try {
$certificates_response = $this->send('GET', $this->build_endpoint('tls/bulk/certificates'));
} catch (RequestException $e) {
$this->error = $e;
return $e->getMessage();
}
}
else {
$certificates_response = $this->send(
'GET',
$this->build_endpoint('tls/bulk/certificates?'.$options)
);
try {
$certificates_response = $this->send('GET', $this->build_endpoint('tls/bulk/certificates?'.$options));
} catch (RequestException $e) {
$this->error = $e;
return $e->getMessage();
}
}

$certificates = [];
Expand Down
32 changes: 23 additions & 9 deletions src/Fastly/Types/FastlyPrivateKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ public function send_private_key($private_key, $name = '')
*/
public function get_private_key($id)
{
$result = $this->send('GET', $this->build_endpoint('tls/private_keys/') . $id);
try {
$result = $this->send('GET', $this->build_endpoint('tls/private_keys/') . $id);
} catch (RequestException $e) {
$this->error = $e;
return $e->getMessage();
}

if ($result) {
return new FastlyPrivateKey($this->build_output($result)['data']);
Expand All @@ -72,20 +77,29 @@ public function get_private_keys($filter = [])
{
$filter_encoded_query = http_build_query($filter);
$endpoint = !empty($filter) ? 'tls/private_keys?'.$filter_encoded_query : 'tls/private_keys';
$keys_response = $this->send('GET', $this->build_endpoint($endpoint));

try {
$keys_response = $this->send('GET', $this->build_endpoint($endpoint));
} catch (RequestException $e) {
$this->error = $e;
return $e->getMessage();
}

$keys = [];

if ($keys_response !== null || $keys_response != []) {
$output = $this->build_output($keys_response);
$this->data = $output['data'];
$this->meta = $output['meta'];
$output = $this->build_output($keys_response);
$this->data = $output['data'];
$this->meta = $output['meta'];

foreach ($this->data as $private_key) {
$keys['data'][] = new FastlyPrivateKey($private_key);
}
foreach ($this->data as $private_key) {
$keys['data'][] = new FastlyPrivateKey($private_key);
}

$keys['meta'][] = $this->meta;
$keys['meta'][] = $this->meta;
}
else {
return ['data' => "No private keys returned."];
}

return $keys;
Expand Down
1 change: 0 additions & 1 deletion tests/Fastly/FastlyTLSBulkCertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function testGetFastlyCertificates()

// Get whole response from API.
$this->assertArrayHasKey('data', $certificates);
$this->assertArrayHasKey('meta', $certificates);
}

public function testGetPlatformTLSCertificates()
Expand Down

0 comments on commit 6b3fdaa

Please sign in to comment.