Code cleanup / Quality of Life updates

Adapter::get, post, put, patch and delete all had non-optional parameters for $headers and $query or $body. These have been made optional. I also re-ordered the parameters as $headers was never used while $query/$body were heavily used. I also condensed and removed some duplicate calls so that every call to Adapter::get/post sends that call to Adapter::request. It could use a magic method to do this but it might make it more difficult to test.
This commit is contained in:
Darin Randal
2018-04-12 23:11:04 -04:00
parent 35ce8eadf1
commit cca073c809
31 changed files with 169 additions and 263 deletions

View File

@@ -31,46 +31,46 @@ interface Adapter
* RFCs, it is never useful).
*
* @param string $uri
* @param array $query
* @param array $data
* @param array $headers
*
* @return mixed
*/
public function get(string $uri, array $query, array $headers): ResponseInterface;
public function get(string $uri, array $data = [], array $headers = []): ResponseInterface;
/**
* @param string $uri
* @param array $data
* @param array $headers
* @param array $body
*
* @return mixed
*/
public function post(string $uri, array $headers, array $body): ResponseInterface;
public function post(string $uri, array $data = [], array $headers = []): ResponseInterface;
/**
* @param string $uri
* @param array $data
* @param array $headers
* @param array $body
*
* @return mixed
*/
public function put(string $uri, array $headers, array $body): ResponseInterface;
public function put(string $uri, array $data = [], array $headers = []): ResponseInterface;
/**
* @param string $uri
* @param array $data
* @param array $headers
* @param array $body
*
* @return mixed
*/
public function patch(string $uri, array $headers, array $body): ResponseInterface;
public function patch(string $uri, array $data = [], array $headers = []): ResponseInterface;
/**
* @param string $uri
* @param array $data
* @param array $headers
* @param array $body
*
* @return mixed
*/
public function delete(string $uri, array $headers, array $body): ResponseInterface;
public function delete(string $uri, array $data = [], array $headers = []): ResponseInterface;
}

View File

@@ -37,79 +37,56 @@ class Guzzle implements Adapter
/**
* @inheritDoc
*/
public function get(string $uri, array $query = [], array $headers = []): ResponseInterface
public function get(string $uri, array $data = [], array $headers = []): ResponseInterface
{
$response = $this->client->get($uri, ['query' => $query, 'headers' => $headers]);
$this->checkError($response);
return $response;
return $this->request('get', $uri, $data, $headers);
}
/**
* @inheritDoc
*/
public function post(string $uri, array $headers = [], array $body = []): ResponseInterface
public function post(string $uri, array $data = [], array $headers = []): ResponseInterface
{
$response = $this->client->post(
$uri,
[
'headers' => $headers,
'json' => $body
]
);
$this->checkError($response);
return $response;
return $this->request('post', $uri, $data, $headers);
}
/**
* @inheritDoc
*/
public function put(string $uri, array $headers = [], array $body = []): ResponseInterface
public function put(string $uri, array $data = [], array $headers = []): ResponseInterface
{
$response = $this->client->put(
$uri,
[
'headers' => $headers,
'json' => $body
]
);
$this->checkError($response);
return $response;
return $this->request('put', $uri, $data, $headers);
}
/**
* @inheritDoc
*/
public function patch(string $uri, array $headers = [], array $body = []): ResponseInterface
public function patch(string $uri, array $data = [], array $headers = []): ResponseInterface
{
$response = $this->client->patch(
$uri,
[
'headers' => $headers,
'json' => $body
]
);
$this->checkError($response);
return $response;
return $this->request('patch', $uri, $data, $headers);
}
/**
* @inheritDoc
*/
public function delete(string $uri, array $headers = [], array $body = []): ResponseInterface
public function delete(string $uri, array $data = [], array $headers = []): ResponseInterface
{
$response = $this->client->delete(
$uri,
[
'headers' => $headers,
'json' => $body
]
);
return $this->request('delete', $uri, $data, $headers);
}
public function request(string $method, string $uri, array $data = [], array $headers = [])
{
if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete');
}
$response = $this->client->$method($uri, [
'headers' => $headers,
($method === 'get' ? 'query' : 'json') => $data,
]);
$this->checkError($response);
return $response;
}

View File

@@ -15,9 +15,9 @@ class PageRulesTargets implements Configurations
public function __construct(string $queryUrl)
{
$this->targets = [
(object)[
[
'target' => 'url',
'constraint' => (object)[
'constraint' => [
'operator' => 'matches',
'value' => $queryUrl
]

View File

@@ -14,7 +14,7 @@ class UARules implements Configurations
public function addUA(string $value)
{
$this->configs[] = (object)['target' => 'ua', 'value' => $value];
$this->configs[] = ['target' => 'ua', 'value' => $value];
}
public function getArray(): array

View File

@@ -14,12 +14,12 @@ class ZoneLockdown implements Configurations
public function addIP(string $value)
{
$this->configs[] = (object)['target' => 'ip', 'value' => $value];
$this->configs[] = ['target' => 'ip', 'value' => $value];
}
public function addIPRange(string $value)
{
$this->configs[] = (object)['target' => 'ip_range', 'value' => $value];
$this->configs[] = ['target' => 'ip_range', 'value' => $value];
}
public function getArray(): array

View File

@@ -77,7 +77,7 @@ class AccessRules implements API
$query['notes'] = $notes;
}
$data = $this->adapter->get('zones/' . $zoneID . '/firewall/access_rules/rules', $query, []);
$data = $this->adapter->get('zones/' . $zoneID . '/firewall/access_rules/rules', $query);
$body = json_decode($data->getBody());
return (object)['result' => $body->result, 'result_info' => $body->result_info];
@@ -91,14 +91,14 @@ class AccessRules implements API
): bool {
$options = [
'mode' => $mode,
'configuration' => (object) $configuration->getArray()
'configuration' => $configuration->getArray()
];
if ($notes !== null) {
$options['notes'] = $notes;
}
$query = $this->adapter->post('zones/' . $zoneID . '/firewall/access_rules/rules', [], $options);
$query = $this->adapter->post('zones/' . $zoneID . '/firewall/access_rules/rules', $options);
$body = json_decode($query->getBody());
@@ -123,7 +123,7 @@ class AccessRules implements API
$options['notes'] = $notes;
}
$query = $this->adapter->patch('zones/' . $zoneID . '/firewall/access_rules/rules/' . $ruleID, [], $options);
$query = $this->adapter->patch('zones/' . $zoneID . '/firewall/access_rules/rules/' . $ruleID, $options);
$body = json_decode($query->getBody());
@@ -140,7 +140,7 @@ class AccessRules implements API
'cascade' => $cascade
];
$data = $this->adapter->delete('zones/' . $zoneID . '/firewall/access_rules/rules/' . $ruleID, [], $options);
$data = $this->adapter->delete('zones/' . $zoneID . '/firewall/access_rules/rules/' . $ruleID, $options);
$body = json_decode($data->getBody());

View File

@@ -33,13 +33,13 @@ class CustomHostnames implements API
{
$options = [
'hostname' => $hostname,
'ssl' => (object)[
'ssl' => [
'method' => $sslMethod,
'type' => $sslType
]
];
$zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', [], $options);
$zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options);
$body = json_decode($zone->getBody());
return $body->result;
}
@@ -87,7 +87,7 @@ class CustomHostnames implements API
$query['direction'] = $direction;
}
$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames', $query, []);
$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames', $query);
$body = json_decode($zone->getBody());
return (object)['result' => $body->result, 'result_info' => $body->result_info];
@@ -100,7 +100,7 @@ class CustomHostnames implements API
*/
public function getHostname(string $zoneID, string $hostnameID)
{
$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], []);
$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID);
$body = json_decode($zone->getBody());
return $body->result;
@@ -128,10 +128,10 @@ class CustomHostnames implements API
}
$options = [
'ssl' => (object)$query
'ssl' => $query
];
$zone = $this->adapter->patch('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], $options);
$zone = $this->adapter->patch('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, $options);
$body = json_decode($zone->getBody());
return $body->result;
}
@@ -143,7 +143,7 @@ class CustomHostnames implements API
*/
public function deleteHostname(string $zoneID, string $hostnameID): \stdClass
{
$zone = $this->adapter->delete('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], []);
$zone = $this->adapter->delete('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID);
$body = json_decode($zone->getBody());
return $body;
}

View File

@@ -55,7 +55,7 @@ class DNS implements API
$options['priority'] = $priority;
}
$user = $this->adapter->post('zones/' . $zoneID . '/dns_records', [], $options);
$user = $this->adapter->post('zones/' . $zoneID . '/dns_records', $options);
$body = json_decode($user->getBody());
@@ -103,7 +103,7 @@ class DNS implements API
$query['direction'] = $direction;
}
$user = $this->adapter->get('zones/' . $zoneID . '/dns_records', $query, []);
$user = $this->adapter->get('zones/' . $zoneID . '/dns_records', $query);
$body = json_decode($user->getBody());
return (object)['result' => $body->result, 'result_info' => $body->result_info];
@@ -111,20 +111,20 @@ class DNS implements API
public function getRecordDetails(string $zoneID, string $recordID): \stdClass
{
$user = $this->adapter->get('zones/' . $zoneID . '/dns_records/' . $recordID, [], []);
$user = $this->adapter->get('zones/' . $zoneID . '/dns_records/' . $recordID);
$body = json_decode($user->getBody());
return $body->result;
}
public function updateRecordDetails(string $zoneID, string $recordID, array $details): \stdClass
{
$response = $this->adapter->put('zones/' . $zoneID . '/dns_records/' . $recordID, [], $details);
$response = $this->adapter->put('zones/' . $zoneID . '/dns_records/' . $recordID, $details);
return json_decode($response->getBody());
}
public function deleteRecord(string $zoneID, string $recordID): bool
{
$user = $this->adapter->delete('zones/' . $zoneID . '/dns_records/' . $recordID, [], []);
$user = $this->adapter->delete('zones/' . $zoneID . '/dns_records/' . $recordID);
$body = json_decode($user->getBody());

View File

@@ -21,7 +21,7 @@ class IPs implements API
public function listIPs(): \stdClass
{
$ips = $this->adapter->get('ips', [], []);
$ips = $this->adapter->get('ips');
$body = json_decode($ips->getBody());
return $body->result;

View File

@@ -52,7 +52,7 @@ class PageRules implements API
}
$query = $this->adapter->post('zones/' . $zoneID . '/pagerules', [], $options);
$query = $this->adapter->post('zones/' . $zoneID . '/pagerules', $options);
$body = json_decode($query->getBody());
@@ -93,7 +93,7 @@ class PageRules implements API
'match' => $match
];
$user = $this->adapter->get('zones/' . $zoneID . '/pagerules', $query, []);
$user = $this->adapter->get('zones/' . $zoneID . '/pagerules', $query);
$body = json_decode($user->getBody());
return $body->result;
@@ -101,7 +101,7 @@ class PageRules implements API
public function getPageRuleDetails(string $zoneID, string $ruleID): \stdClass
{
$user = $this->adapter->get('zones/' . $zoneID . '/pagerules/' . $ruleID, [], []);
$user = $this->adapter->get('zones/' . $zoneID . '/pagerules/' . $ruleID);
$body = json_decode($user->getBody());
return $body->result;
}
@@ -132,7 +132,7 @@ class PageRules implements API
}
$query = $this->adapter->patch('zones/' . $zoneID . '/pagerules', [], $options);
$query = $this->adapter->patch('zones/' . $zoneID . '/pagerules', $options);
$body = json_decode($query->getBody());
@@ -145,7 +145,7 @@ class PageRules implements API
public function deletePageRule(string $zoneID, string $ruleID): bool
{
$user = $this->adapter->delete('zones/' . $zoneID . '/pagerules/' . $ruleID, [], []);
$user = $this->adapter->delete('zones/' . $zoneID . '/pagerules/' . $ruleID);
$body = json_decode($user->getBody());

View File

@@ -26,7 +26,7 @@ class Railgun implements API
'name' => $name,
];
$user = $this->adapter->post('railguns', [], $query);
$user = $this->adapter->post('railguns', $query);
$body = json_decode($user->getBody());
return $body;
@@ -46,7 +46,7 @@ class Railgun implements API
$query['direction'] = $direction;
}
$user = $this->adapter->get('railguns', $query, []);
$user = $this->adapter->get('railguns', $query);
$body = json_decode($user->getBody());
return (object)['result' => $body->result, 'result_info' => $body->result_info];
@@ -55,7 +55,7 @@ class Railgun implements API
public function get(
string $railgunID
): \stdClass {
$user = $this->adapter->get('railguns/' . $railgunID, [], []);
$user = $this->adapter->get('railguns/' . $railgunID);
$body = json_decode($user->getBody());
return $body->result;
@@ -64,7 +64,7 @@ class Railgun implements API
public function getZones(
string $railgunID
): \stdClass {
$user = $this->adapter->get('railguns/' . $railgunID . '/zones', [], []);
$user = $this->adapter->get('railguns/' . $railgunID . '/zones');
$body = json_decode($user->getBody());
return (object)['result' => $body->result, 'result_info' => $body->result_info];
@@ -78,7 +78,7 @@ class Railgun implements API
'enabled' => $status
];
$user = $this->adapter->patch('railguns/' . $railgunID, [], $query);
$user = $this->adapter->patch('railguns/' . $railgunID, $query);
$body = json_decode($user->getBody());
return $body->result;
@@ -87,7 +87,7 @@ class Railgun implements API
public function delete(
string $railgunID
): bool {
$user = $this->adapter->delete('railguns/' . $railgunID, [], []);
$user = $this->adapter->delete('railguns/' . $railgunID);
$body = json_decode($user->getBody());
if (isset($body->result->id)) {

View File

@@ -30,7 +30,7 @@ class UARules implements API
'per_page' => $perPage
];
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/ua_rules', $query, []);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/ua_rules', $query);
$body = json_decode($user->getBody());
return (object)['result' => $body->result, 'result_info' => $body->result_info];
@@ -56,7 +56,7 @@ class UARules implements API
$options['description'] = $description;
}
$user = $this->adapter->post('zones/' . $zoneID . '/firewall/ua_rules', [], $options);
$user = $this->adapter->post('zones/' . $zoneID . '/firewall/ua_rules', $options);
$body = json_decode($user->getBody());
@@ -69,7 +69,7 @@ class UARules implements API
public function getRuleDetails(string $zoneID, string $blockID): \stdClass
{
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/ua_rules/' . $blockID, [], []);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/ua_rules/' . $blockID);
$body = json_decode($user->getBody());
return $body->result;
}
@@ -91,7 +91,7 @@ class UARules implements API
$options['description'] = $description;
}
$user = $this->adapter->put('zones/' . $zoneID . '/firewall/ua_rules/' . $ruleID, [], $options);
$user = $this->adapter->put('zones/' . $zoneID . '/firewall/ua_rules/' . $ruleID, $options);
$body = json_decode($user->getBody());
@@ -104,7 +104,7 @@ class UARules implements API
public function deleteRule(string $zoneID, string $ruleID): bool
{
$user = $this->adapter->delete('zones/' . $zoneID . '/firewall/ua_rules/' . $ruleID, [], []);
$user = $this->adapter->delete('zones/' . $zoneID . '/firewall/ua_rules/' . $ruleID);
$body = json_decode($user->getBody());

View File

@@ -20,7 +20,7 @@ class User implements API
public function getUserDetails(): \stdClass
{
$user = $this->adapter->get('user', [], []);
$user = $this->adapter->get('user');
$body = json_decode($user->getBody());
return $body->result;
}
@@ -37,7 +37,7 @@ class User implements API
public function updateUserDetails(array $details): \stdClass
{
$response = $this->adapter->patch('user', [], $details);
$response = $this->adapter->patch('user', $details);
return json_decode($response->getBody());
}
}

View File

@@ -41,7 +41,7 @@ class WAF implements API
$query['direction'] = $direction;
}
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages', $query, []);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages', $query);
$body = json_decode($user->getBody());
return (object)['result' => $body->result, 'result_info' => $body->result_info];
@@ -52,7 +52,7 @@ class WAF implements API
string $zoneID,
string $packageID
): \stdClass {
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID, [], []);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID);
$body = json_decode($user->getBody());
return $body->result;
@@ -80,7 +80,7 @@ class WAF implements API
if (!empty($direction)) {
$query['direction'] = $direction;
}
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules', $query, []);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules', $query);
$body = json_decode($user->getBody());
return (object)['result' => $body->result, 'result_info' => $body->result_info];
@@ -91,11 +91,7 @@ class WAF implements API
string $packageID,
string $ruleID
): \stdClass {
$user = $this->adapter->get(
'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules/' . $ruleID,
[],
[]
);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules/' . $ruleID);
$body = json_decode($user->getBody());
return $body->result;
@@ -113,7 +109,6 @@ class WAF implements API
$user = $this->adapter->patch(
'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules/' . $ruleID,
[],
$query
);
$body = json_decode($user->getBody());
@@ -146,8 +141,7 @@ class WAF implements API
$user = $this->adapter->get(
'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/groups',
$query,
[]
$query
);
$body = json_decode($user->getBody());
@@ -159,11 +153,7 @@ class WAF implements API
string $packageID,
string $groupID
): \stdClass {
$user = $this->adapter->get(
'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/groups/' . $groupID,
[],
[]
);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/groups/' . $groupID);
$body = json_decode($user->getBody());
return $body->result;
@@ -181,7 +171,6 @@ class WAF implements API
$user = $this->adapter->patch(
'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/groups/' . $groupID,
[],
$query
);
$body = json_decode($user->getBody());

View File

@@ -29,7 +29,7 @@ class ZoneLockdown implements API
'per_page' => $perPage
];
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns', $query, []);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns', $query);
$body = json_decode($user->getBody());
return (object)['result' => $body->result, 'result_info' => $body->result_info];
@@ -55,7 +55,7 @@ class ZoneLockdown implements API
$options['description'] = $description;
}
$user = $this->adapter->post('zones/' . $zoneID . '/firewall/lockdowns', [], $options);
$user = $this->adapter->post('zones/' . $zoneID . '/firewall/lockdowns', $options);
$body = json_decode($user->getBody());
@@ -68,7 +68,7 @@ class ZoneLockdown implements API
public function getLockdownDetails(string $zoneID, string $lockdownID): \stdClass
{
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID, [], []);
$user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID);
$body = json_decode($user->getBody());
return $body->result;
}
@@ -90,7 +90,7 @@ class ZoneLockdown implements API
$options['description'] = $description;
}
$user = $this->adapter->put('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID, [], $options);
$user = $this->adapter->put('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID, $options);
$body = json_decode($user->getBody());
@@ -103,7 +103,7 @@ class ZoneLockdown implements API
public function deleteLockdown(string $zoneID, string $lockdownID): bool
{
$user = $this->adapter->delete('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID, [], []);
$user = $this->adapter->delete('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID);
$body = json_decode($user->getBody());

View File

@@ -35,17 +35,17 @@ class Zones implements API
];
if (!empty($organizationID)) {
$options['organization'] = (object)['id' => $organizationID];
$options['organization'] = ['id' => $organizationID];
}
$user = $this->adapter->post('zones', [], $options);
$user = $this->adapter->post('zones', $options);
$body = json_decode($user->getBody());
return $body->result;
}
public function activationCheck(string $zoneID): bool
{
$user = $this->adapter->put('zones/' . $zoneID . '/activation_check', [], []);
$user = $this->adapter->put('zones/' . $zoneID . '/activation_check');
$body = json_decode($user->getBody());
if (isset($body->result->id)) {
@@ -86,7 +86,7 @@ class Zones implements API
$query['direction'] = $direction;
}
$user = $this->adapter->get('zones', $query, []);
$user = $this->adapter->get('zones', $query);
$body = json_decode($user->getBody());
return (object)['result' => $body->result, 'result_info' => $body->result_info];
@@ -114,7 +114,7 @@ class Zones implements API
*/
public function getAnalyticsDashboard(string $zoneID, string $since = '-10080', string $until = '0', bool $continuous = true): \stdClass
{
$response = $this->adapter->get('zones/' . $zoneID . '/analytics/dashboard', ['since' => $since, 'until' => $until, 'continuous' => var_export($continuous, true)], []);
$response = $this->adapter->get('zones/' . $zoneID . '/analytics/dashboard', ['since' => $since, 'until' => $until, 'continuous' => var_export($continuous, true)]);
return json_decode($response->getBody())->result;
}
@@ -128,7 +128,7 @@ class Zones implements API
*/
public function changeDevelopmentMode(string $zoneID, bool $enable = false): bool
{
$response = $this->adapter->patch('zones/' . $zoneID . '/settings/development_mode', [], ['value' => $enable ? 'on' : 'off']);
$response = $this->adapter->patch('zones/' . $zoneID . '/settings/development_mode', ['value' => $enable ? 'on' : 'off']);
$body = json_decode($response->getBody());
@@ -147,7 +147,7 @@ class Zones implements API
*/
public function cachePurgeEverything(string $zoneID): bool
{
$user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', [], ['purge_everything' => true]);
$user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', ['purge_everything' => true]);
$body = json_decode($user->getBody());
@@ -169,7 +169,7 @@ class Zones implements API
'tags' => $tags
];
$user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', [], $options);
$user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', $options);
$body = json_decode($user->getBody());