|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ©[2022] SugarCRM Inc. Licensed by SugarCRM under the Apache 2.0 license. |
| 4 | + */ |
| 5 | + |
| 6 | +namespace Sugarcrm\REST\Endpoint; |
| 7 | + |
| 8 | +use GuzzleHttp\Psr7\Request; |
| 9 | +use Sugarcrm\REST\Endpoint\Abstracts\AbstractSugarEndpoint; |
| 10 | + |
| 11 | +/** |
| 12 | + * Generic REST endpoint allowing for querying custom endpoints |
| 13 | + * usage examples: |
| 14 | + * $client->rest('custom/endpoint')->get(); |
| 15 | + * $client->rest('custom/endpoint')->post($data); |
| 16 | + * $client->rest('custom/endpoint')->put($data); |
| 17 | + * $client->rest('custom/endpoint')->delete(); |
| 18 | + * $client->rest('custom/endpoint')->patch($data); |
| 19 | + * $client->rest('custom/endpoint')->withHeaders($headers)->get(); |
| 20 | + * $client->rest('Contacts')->setData(['fields' => 'id,first_name,last_name', 'max_num' => 1])->get(); |
| 21 | + * etc. |
| 22 | + * @package Sugarcrm\REST\Endpoint |
| 23 | + */ |
| 24 | +class Rest extends Generic |
| 25 | +{ |
| 26 | + protected static array $_DEFAULT_PROPERTIES = array( |
| 27 | + self::PROPERTY_URL => '$endpoint', |
| 28 | + self::PROPERTY_AUTH => true, |
| 29 | + self::PROPERTY_HTTP_METHOD => "GET" |
| 30 | + ); |
| 31 | + |
| 32 | + protected array $headers = []; |
| 33 | + |
| 34 | + public function withHeaders(array $headers) |
| 35 | + { |
| 36 | + $this->headers = $headers; |
| 37 | + return $this; |
| 38 | + } |
| 39 | + |
| 40 | + protected function configureRequest(Request $request, $data): Request |
| 41 | + { |
| 42 | + $request = parent::configureRequest($request, $data); |
| 43 | + |
| 44 | + if (!empty($this->headers)) { |
| 45 | + foreach ($this->headers as $header => $value) { |
| 46 | + $request = $request->withHeader($header, $value); |
| 47 | + } |
| 48 | + } |
| 49 | + return $request; |
| 50 | + } |
| 51 | + |
| 52 | + public function get($data = null) |
| 53 | + { |
| 54 | + $this->setProperty(self::PROPERTY_HTTP_METHOD, 'GET'); |
| 55 | + if (!is_null($data)) { |
| 56 | + $this->setData($data); |
| 57 | + } |
| 58 | + return $this->execute(); |
| 59 | + } |
| 60 | + |
| 61 | + public function post($data = null) |
| 62 | + { |
| 63 | + $this->setProperty(self::PROPERTY_HTTP_METHOD, 'POST'); |
| 64 | + if (!is_null($data)) { |
| 65 | + $this->setData($data); |
| 66 | + } |
| 67 | + return $this->execute(); |
| 68 | + } |
| 69 | + |
| 70 | + public function put($data = null) |
| 71 | + { |
| 72 | + $this->setProperty(self::PROPERTY_HTTP_METHOD, 'PUT'); |
| 73 | + if (!is_null($data)) { |
| 74 | + $this->setData($data); |
| 75 | + } |
| 76 | + return $this->execute(); |
| 77 | + } |
| 78 | + |
| 79 | + public function delete() |
| 80 | + { |
| 81 | + $this->setProperty(self::PROPERTY_HTTP_METHOD, 'DELETE'); |
| 82 | + return $this->execute(); |
| 83 | + } |
| 84 | + |
| 85 | + public function patch($data = null) |
| 86 | + { |
| 87 | + $this->setProperty(self::PROPERTY_HTTP_METHOD, 'PATCH'); |
| 88 | + if (!is_null($data)) { |
| 89 | + $this->setData($data); |
| 90 | + } |
| 91 | + return $this->execute(); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments