| 1 |
<?php |
| 2 |
namespace Depicter\Services; |
| 3 |
|
| 4 |
|
| 5 |
use Averta\Core\Utility\Arr; |
| 6 |
use Averta\WordPress\Utility\JSON; |
| 7 |
use Depicter\GuzzleHttp\Client; |
| 8 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 9 |
use Psr\Http\Message\ResponseInterface; |
| 10 |
|
| 11 |
/** |
| 12 |
* Performs an HTTP request and returns its response. |
| 13 |
* |
| 14 |
* @package Depicter\Services |
| 15 |
*/ |
| 16 |
class RemoteAPIService |
| 17 |
{ |
| 18 |
/** |
| 19 |
* List of endpoints |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $endpoints = [ |
| 24 |
"1" => "https://wp-api.depicter.com/", |
| 25 |
"2" => "https://pre.wp-api.depicter.com/" |
| 26 |
]; |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Retrieves an endpoint by number |
| 31 |
* |
| 32 |
* @param int $endpointNumber |
| 33 |
* |
| 34 |
* @param string $branch The relative path to be appended to endpoint url |
| 35 |
* |
| 36 |
* @return mixed|string |
| 37 |
*/ |
| 38 |
public function endpoint( $endpointNumber = 1, $branch = '' ){ |
| 39 |
if( ! empty( $this->endpoints[ $endpointNumber ] ) ){ |
| 40 |
return $this->endpoints[ $endpointNumber ] . $branch; |
| 41 |
} |
| 42 |
return ''; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
private function isAbsoluteUrl( $url ){ |
| 47 |
return strpos($url,'://') !== false; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get default options for requests |
| 52 |
* |
| 53 |
* @return array |
| 54 |
* @throws GuzzleException |
| 55 |
*/ |
| 56 |
private function getDefaultOptions() { |
| 57 |
global $wp_version; |
| 58 |
|
| 59 |
$token = \Depicter::options()->get( 'access_token', '' ); |
| 60 |
|
| 61 |
return [ |
| 62 |
'headers' => [ |
| 63 |
'user-agent' => 'WordPress/'. $wp_version .'; '. get_home_url(), |
| 64 |
'timeout' => 30, |
| 65 |
'X-DEPICTER-CKEY' => \Depicter::options()->get( 'client_key' ), |
| 66 |
'X-DEPICTER-VER' => DEPICTER_VERSION, |
| 67 |
'Authorization' => 'Bearer ' . $token |
| 68 |
] |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Create and send an HTTP GET request to specified API endpoints. |
| 74 |
* |
| 75 |
* @param string $endpoint URI object or string. |
| 76 |
* @param array $options Request options to apply. |
| 77 |
* @param int $endpointNumber Endpoint number |
| 78 |
* |
| 79 |
* @return ResponseInterface |
| 80 |
* @throws GuzzleException |
| 81 |
*/ |
| 82 |
public function get( $endpoint, $options = [], $endpointNumber = 1 ) |
| 83 |
{ |
| 84 |
// Maybe convert branch to absolute endpoint url |
| 85 |
if( ! $this->isAbsoluteUrl( $endpoint ) ){ |
| 86 |
$endpoint = $this->endpoint( $endpointNumber, $endpoint ); |
| 87 |
} |
| 88 |
|
| 89 |
$client = new Client([ |
| 90 |
'verify' => ABSPATH . WPINC . '/certificates/ca-bundle.crt' |
| 91 |
]); |
| 92 |
|
| 93 |
$optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() ); |
| 94 |
$response = $client->get( $endpoint, $optionsWithAuth ); |
| 95 |
|
| 96 |
// try to get refresh token on failure |
| 97 |
if ( $response->getStatusCode() == 401 ) { |
| 98 |
\Depicter::options()->delete('access_token'); |
| 99 |
$optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() ); // refresh token |
| 100 |
$response = $client->get( $endpoint, $optionsWithAuth ); |
| 101 |
} |
| 102 |
|
| 103 |
return $response; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Create and send an HTTP POST request to specified API endpoints. |
| 108 |
* |
| 109 |
* @param string $endpoint URI object or string. |
| 110 |
* @param array $options Request options to apply. |
| 111 |
* @param int $endpointNumber Endpoint number |
| 112 |
* |
| 113 |
* @return ResponseInterface |
| 114 |
* @throws GuzzleException |
| 115 |
*/ |
| 116 |
public function post( $endpoint, $options = [], $endpointNumber = 1 ) |
| 117 |
{ |
| 118 |
// Maybe convert branch to absolute endpoint url |
| 119 |
if( ! $this->isAbsoluteUrl( $endpoint ) ){ |
| 120 |
$endpoint = $this->endpoint( $endpointNumber, $endpoint ); |
| 121 |
} |
| 122 |
|
| 123 |
$client = new Client([ |
| 124 |
'verify' => ABSPATH . WPINC . '/certificates/ca-bundle.crt' |
| 125 |
]); |
| 126 |
$optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() ); |
| 127 |
$response = $client->post( $endpoint, $optionsWithAuth ); |
| 128 |
|
| 129 |
// try to get refresh token on failure |
| 130 |
if ( $response->getStatusCode() == 401 ) { |
| 131 |
\Depicter::options()->delete('access_token'); |
| 132 |
$optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() ); // refresh token |
| 133 |
$response = $client->post( $endpoint, $optionsWithAuth ); |
| 134 |
} |
| 135 |
|
| 136 |
return $response; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Whether remote api address are accessible or not |
| 141 |
* |
| 142 |
* @return bool |
| 143 |
*/ |
| 144 |
public static function isAccessible() |
| 145 |
{ |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get auth token from server |
| 151 |
* |
| 152 |
* @return mixed|string |
| 153 |
* @throws GuzzleException |
| 154 |
*/ |
| 155 |
public function getAuthToken() { |
| 156 |
$client = new Client(); |
| 157 |
$response = $client->post( $this->endpoint(), $this->getDefaultOptions() ); |
| 158 |
|
| 159 |
$body = JSON::decode( $response->getBody(), true ); |
| 160 |
|
| 161 |
if ( !empty( $body['success']) ) { |
| 162 |
return $body['success']; |
| 163 |
} |
| 164 |
return ''; |
| 165 |
} |
| 166 |
} |
| 167 |
|