Skip to content

Commit e91dd1f

Browse files
authored
Merge pull request #12 from achodoff-sugarcrm/develop
Add generic rest endpoint class for calling custom endpoints
2 parents 20377de + 570cbe0 commit e91dd1f

File tree

10 files changed

+289
-1
lines changed

10 files changed

+289
-1
lines changed

src/Client/SugarApi.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Sugarcrm\REST\Endpoint\MLPackage;
1515
use Sugarcrm\REST\Endpoint\ModuleLoader;
1616
use Sugarcrm\REST\Endpoint\Ping;
17+
use Sugarcrm\REST\Endpoint\Rest;
1718
use Sugarcrm\REST\Endpoint\Smart;
1819
use Sugarcrm\REST\Endpoint\SugarBean;
1920
use Sugarcrm\REST\Endpoint\ModuleFilter;
@@ -48,6 +49,7 @@
4849
* @method ModuleLoader moduleLoader() -
4950
* @method MLPackage mlp(string $id = null)
5051
* @method Integrate integrate(string $module = '', string $id = '')
52+
* @method Rest rest(string $endpoint = '')
5153
*/
5254
class SugarApi extends AbstractClient implements PlatformAwareInterface
5355
{

src/Endpoint/Abstracts/AbstractSmartSugarEndpoint.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
namespace Sugarcrm\REST\Endpoint\Abstracts;
88

9+
use GuzzleHttp\Psr7\Request;
910
use MRussell\REST\Endpoint\Data\ValidatedEndpointData;
1011
use MRussell\REST\Endpoint\SmartEndpoint;
1112
use MRussell\REST\Traits\PsrLoggerTrait;
1213
use Sugarcrm\REST\Endpoint\SugarEndpointInterface;
1314
use Sugarcrm\REST\Endpoint\Traits\CompileRequestTrait;
15+
use Sugarcrm\REST\Endpoint\Traits\CustomHeadersTrait;
1416

1517
/**
1618
* Provide a smarter interface for Endpoints, to better manage passed in data
@@ -20,6 +22,12 @@ abstract class AbstractSmartSugarEndpoint extends SmartEndpoint implements Sugar
2022
{
2123
use CompileRequestTrait;
2224
use PsrLoggerTrait;
25+
use CustomHeadersTrait;
2326

2427
protected string $_dataInterface = ValidatedEndpointData::class;
28+
29+
protected function configureRequest(Request $request, $data): Request
30+
{
31+
return parent::configureRequest($this->addCustomHeadersToRequest($request), $data);
32+
}
2533
}

src/Endpoint/Abstracts/AbstractSugarBeanCollectionEndpoint.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
namespace Sugarcrm\REST\Endpoint\Abstracts;
88

9+
use GuzzleHttp\Psr7\Request;
910
use Sugarcrm\REST\Endpoint\SugarBean;
1011
use MRussell\REST\Endpoint\Abstracts\AbstractModelEndpoint;
12+
use Sugarcrm\REST\Endpoint\Traits\CustomHeadersTrait;
1113
use Sugarcrm\REST\Endpoint\Traits\FieldsDataTrait;
1214
use Sugarcrm\REST\Endpoint\Traits\ModuleAwareTrait;
1315

@@ -22,6 +24,7 @@ abstract class AbstractSugarBeanCollectionEndpoint extends AbstractSugarCollecti
2224
{
2325
use FieldsDataTrait;
2426
use ModuleAwareTrait;
27+
use CustomHeadersTrait;
2528

2629
public const SUGAR_ORDERBY_DATA_PROPERTY = 'order_by';
2730

@@ -97,6 +100,11 @@ protected function configurePayload(): mixed
97100
return $this->configureFieldsDataProps($data);
98101
}
99102

103+
protected function configureRequest(Request $request, $data): Request
104+
{
105+
return parent::configureRequest($this->addCustomHeadersToRequest($request), $data);
106+
}
107+
100108
/**
101109
* Add module to url options
102110
* @inheritdoc

src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Sugarcrm\REST\Endpoint\Integrate;
2323
use Sugarcrm\REST\Endpoint\SugarEndpointInterface;
2424
use Sugarcrm\REST\Endpoint\Traits\CompileRequestTrait;
25+
use Sugarcrm\REST\Endpoint\Traits\CustomHeadersTrait;
2526
use Sugarcrm\REST\Endpoint\Traits\FieldsDataTrait;
2627
use Sugarcrm\REST\Endpoint\Traits\IntegrateSyncKeyTrait;
2728
use Sugarcrm\REST\Endpoint\Traits\ModuleAwareTrait;
@@ -53,6 +54,7 @@ abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarE
5354
use FieldsDataTrait;
5455
use FileUploadsTrait;
5556
use IntegrateSyncKeyTrait;
57+
use CustomHeadersTrait;
5658

5759
public const MODEL_ACTION_VAR = 'action';
5860

@@ -173,7 +175,7 @@ protected function configureRequest(Request $request, $data): Request
173175
$data = $this->configureFieldsDataProps($data);
174176
}
175177

176-
return parent::configureRequest($request, $data);
178+
return parent::configureRequest($this->addCustomHeadersToRequest($request), $data);
177179
}
178180

179181
/**

src/Endpoint/Abstracts/AbstractSugarCollectionEndpoint.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
namespace Sugarcrm\REST\Endpoint\Abstracts;
88

9+
use GuzzleHttp\Psr7\Request;
910
use MRussell\REST\Exception\Endpoint\InvalidRequest;
1011
use GuzzleHttp\Psr7\Response;
1112
use MRussell\REST\Endpoint\Data\AbstractEndpointData;
1213
use MRussell\REST\Endpoint\CollectionEndpoint;
1314
use MRussell\REST\Traits\PsrLoggerTrait;
1415
use Sugarcrm\REST\Endpoint\SugarEndpointInterface;
1516
use Sugarcrm\REST\Endpoint\Traits\CompileRequestTrait;
17+
use Sugarcrm\REST\Endpoint\Traits\CustomHeadersTrait;
1618

1719
/**
1820
* Provides access to a multi-bean collection retrieved from Sugar 7 REST Api
@@ -23,6 +25,7 @@ abstract class AbstractSugarCollectionEndpoint extends CollectionEndpoint implem
2325
{
2426
use CompileRequestTrait;
2527
use PsrLoggerTrait;
28+
use CustomHeadersTrait;
2629

2730
public const SUGAR_OFFSET_PROPERTY = 'offset';
2831

@@ -82,6 +85,11 @@ protected function configurePayload(): mixed
8285
return $data;
8386
}
8487

88+
protected function configureRequest(Request $request, $data): Request
89+
{
90+
return parent::configureRequest($this->addCustomHeadersToRequest($request), $data);
91+
}
92+
8593
/**
8694
* Get the configured offset
8795
*/

src/Endpoint/Abstracts/AbstractSugarEndpoint.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
namespace Sugarcrm\REST\Endpoint\Abstracts;
88

9+
use GuzzleHttp\Psr7\Request;
910
use MRussell\REST\Endpoint\Endpoint;
1011
use MRussell\REST\Traits\PsrLoggerTrait;
1112
use Sugarcrm\REST\Endpoint\SugarEndpointInterface;
1213
use Sugarcrm\REST\Endpoint\Traits\CompileRequestTrait;
14+
use Sugarcrm\REST\Endpoint\Traits\CustomHeadersTrait;
1315

1416
/**
1517
* Base Sugar API Endpoint for the simplest of REST functionality
@@ -19,4 +21,10 @@ abstract class AbstractSugarEndpoint extends Endpoint implements SugarEndpointIn
1921
{
2022
use CompileRequestTrait;
2123
use PsrLoggerTrait;
24+
use CustomHeadersTrait;
25+
26+
protected function configureRequest(Request $request, $data): Request
27+
{
28+
return parent::configureRequest($this->addCustomHeadersToRequest($request), $data);
29+
}
2230
}

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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Sugarcrm\REST\Endpoint\Traits;
4+
5+
use GuzzleHttp\Psr7\Request;
6+
7+
trait CustomHeadersTrait
8+
{
9+
protected array $customHeaders = [];
10+
11+
/**
12+
* Set a custom header to be sent with the request
13+
* @param string $name
14+
* @param string $value
15+
* @return $this
16+
*/
17+
public function addCustomHeader(string $name, string $value): static
18+
{
19+
$normalized = strtolower($name);
20+
$this->customHeaders[$normalized] = [$name, $value];
21+
return $this;
22+
}
23+
24+
/**
25+
* Remove a custom header from the request
26+
* @param string $name
27+
* @return $this
28+
*/
29+
public function removeCustomHeader(string $name): static
30+
{
31+
$normalized = strtolower($name);
32+
if (isset($this->customHeaders[$normalized])) {
33+
unset($this->customHeaders[$normalized]);
34+
}
35+
return $this;
36+
}
37+
38+
protected function addCustomHeadersToRequest(Request $request): Request
39+
{
40+
if (!empty($this->customHeaders)) {
41+
foreach ($this->customHeaders as $headerNormalized => $values) {
42+
$request = $request->withHeader($values[0], $values[1]);
43+
}
44+
}
45+
return $request;
46+
}
47+
}

0 commit comments

Comments
 (0)