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