|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Baka\Http\Rest; |
| 4 | + |
| 5 | +use Baka\Hmac\Client as ClientSecurity; |
| 6 | +use Phalcon\Http\Response; |
| 7 | + |
| 8 | +trait SdkTrait |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @string |
| 12 | + */ |
| 13 | + protected $apiVersion; |
| 14 | + |
| 15 | + /** |
| 16 | + * @string |
| 17 | + */ |
| 18 | + protected $apiPublicKey; |
| 19 | + |
| 20 | + /** |
| 21 | + * @string |
| 22 | + */ |
| 23 | + protected $apiPrivateKey; |
| 24 | + |
| 25 | + /** |
| 26 | + * @string |
| 27 | + */ |
| 28 | + protected $apiHeaders = []; |
| 29 | + |
| 30 | + /** |
| 31 | + * Function to set the API version since the property is protected. |
| 32 | + * |
| 33 | + * @param mixed $version |
| 34 | + * |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + public function setApiVersion($version) |
| 38 | + { |
| 39 | + $this->apiVersion = $version; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Function to set the API public key since the property is protected. |
| 44 | + * |
| 45 | + * @param mixed $version |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + public function setApiPublicKey($publicKey) |
| 50 | + { |
| 51 | + $this->apiPublicKey = $publicKey; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Function to set the API private key since the property is protected. |
| 56 | + * |
| 57 | + * @param mixed $version |
| 58 | + * |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + public function setApiPrivateKey($privateKey) |
| 62 | + { |
| 63 | + $this->apiPrivateKey = $privateKey; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Function to set the API headers since the property is protected. |
| 68 | + * |
| 69 | + * @param mixed $version |
| 70 | + * |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + public function setApiHeaders($apiHeaders) |
| 74 | + { |
| 75 | + $this->apiHeaders = $apiHeaders; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Function tasked with delegating API requests to the configured API |
| 80 | + * |
| 81 | + * @todo Verify headers being received from the API response before returning the request response. |
| 82 | + * |
| 83 | + * @return \Phalcon\Http\Response |
| 84 | + */ |
| 85 | + public function transporterAction(): Response |
| 86 | + { |
| 87 | + // Get all router params |
| 88 | + $routeParams = $this->router->getParams(); |
| 89 | + |
| 90 | + // Confirm that an API version has been configured |
| 91 | + if (!array_key_exists('version', $routeParams)) { |
| 92 | + return $this->response->setJsonContent([ |
| 93 | + 'error' => 'You must specify an API version in your configuration.', |
| 94 | + ])->send(); |
| 95 | + } |
| 96 | + |
| 97 | + // Confirm that the API version matches with the configuration. |
| 98 | + if ($routeParams['version'] != $this->apiVersion) { |
| 99 | + return $this->response->setJsonContent([ |
| 100 | + 'error' => 'The specified API version is different from your configuration.', |
| 101 | + ])->send(); |
| 102 | + } |
| 103 | + |
| 104 | + // Get real API URL |
| 105 | + $apiUrl = getenv('EXT_API_URL') . str_replace('api/', '', $this->router->getRewriteUri()); |
| 106 | + |
| 107 | + // Execute the request, providing the URL, the request method and the data. |
| 108 | + $response = $this->makeRequest($apiUrl, $this->request->getMethod(), $this->getData(), $this->apiHeaders); |
| 109 | + |
| 110 | + // Set the response headers based on the response headers of the API. |
| 111 | + $this->response->setContentType($response->getHeader('Content-Type')[0]); |
| 112 | + |
| 113 | + return $this->response->setContent($response->getBody()); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Function that executes the request to the configured API |
| 118 | + * |
| 119 | + * @param string $method - The request method |
| 120 | + * @param string $url - The request URL |
| 121 | + * @param array $data - The form data |
| 122 | + * |
| 123 | + * @return JSON |
| 124 | + */ |
| 125 | + public function makeRequest($url, $method = 'GET', $data = [], $headers = []) |
| 126 | + { |
| 127 | + $client = (new ClientSecurity($this->apiPublicKey, $this->apiPrivateKey, $data, $headers))->getGuzzle(); |
| 128 | + |
| 129 | + $parse = function ($error) { |
| 130 | + if ($error->hasResponse()) { |
| 131 | + return $error->getResponse(); |
| 132 | + } |
| 133 | + |
| 134 | + return json_decode($error->getMessage()); |
| 135 | + }; |
| 136 | + |
| 137 | + try { |
| 138 | + return $client->request($method, $url, $data); |
| 139 | + } catch (\GuzzleHttp\Exception\BadResponseException $error) { |
| 140 | + return $parse($error); |
| 141 | + } catch (\GuzzleHttp\Exception\ClientException $error) { |
| 142 | + return $parse($error); |
| 143 | + } catch (\GuzzleHttp\Exception\ConnectException $error) { |
| 144 | + return $parse($error); |
| 145 | + } catch (\GuzzleHttp\Exception\RequestException $error) { |
| 146 | + return $parse($error); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Function that obtains the data as per the request type. |
| 152 | + * |
| 153 | + * @return array |
| 154 | + */ |
| 155 | + public function getData(): array |
| 156 | + { |
| 157 | + $uploads = []; |
| 158 | + //if the user is trying to upload a image |
| 159 | + if ($this->request->hasFiles()) { |
| 160 | + foreach ($this->request->getUploadedFiles() as $file) { |
| 161 | + $uploads[] = [ |
| 162 | + 'name' => $file->getKey(), |
| 163 | + 'filename' => $file->getName(), |
| 164 | + 'contents' => file_get_contents($file->getTempName()), |
| 165 | + ]; |
| 166 | + } |
| 167 | + |
| 168 | + $parseUpload = function ($request) use (&$uploads) { |
| 169 | + foreach ($request as $key => $value) { |
| 170 | + if (is_array($value)) { |
| 171 | + foreach ($value as $f => $v) { |
| 172 | + $uploads[] = ['name' => $key . '[' . $f . ']', 'contents' => $v]; |
| 173 | + } |
| 174 | + } else { |
| 175 | + $uploads[] = ['name' => $key, 'contents' => $value]; |
| 176 | + } |
| 177 | + } |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + switch ($this->request->getMethod()) { |
| 182 | + case 'GET': |
| 183 | + $queryParams = $this->request->getQuery(); |
| 184 | + unset($queryParams['_url']); |
| 185 | + return ['query' => $queryParams]; |
| 186 | + break; |
| 187 | + |
| 188 | + case 'POST': |
| 189 | + if (!$uploads) { |
| 190 | + return ['form_params' => $this->request->getPost()]; |
| 191 | + } else { |
| 192 | + $parseUpload($this->request->getPost()); |
| 193 | + return ['multipart' => $uploads]; |
| 194 | + } |
| 195 | + break; |
| 196 | + |
| 197 | + case 'PUT': |
| 198 | + if (!$uploads) { |
| 199 | + return ['form_params' => $this->request->getPut()]; |
| 200 | + } else { |
| 201 | + $parseUpload($this->request->getPut()); |
| 202 | + return ['multipart' => $uploads]; |
| 203 | + } |
| 204 | + |
| 205 | + break; |
| 206 | + default: |
| 207 | + return []; |
| 208 | + break; |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments