Skip to content

Commit 2a63b5b

Browse files
Add generic rest endpoint class for calling custom endpoints
1 parent 9cb0e23 commit 2a63b5b

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/Endpoint/Provider/SugarEndpointProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Sugarcrm\REST\Endpoint\Ping;
2525
use Sugarcrm\REST\Endpoint\Note;
2626
use Sugarcrm\REST\Endpoint\Generic;
27+
use Sugarcrm\REST\Endpoint\Rest;
2728
use Sugarcrm\REST\Endpoint\Smart;
2829

2930
/**
@@ -161,5 +162,10 @@ class SugarEndpointProvider extends VersionedEndpointProvider
161162
self::ENDPOINT_CLASS => Integrate::class,
162163
self::ENDPOINT_PROPERTIES => [],
163164
],
165+
[
166+
self::ENDPOINT_NAME => 'rest',
167+
self::ENDPOINT_CLASS => Rest::class,
168+
self::ENDPOINT_PROPERTIES => [],
169+
]
164170
];
165171
}

src/Endpoint/Rest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)