Skip to content
2 changes: 2 additions & 0 deletions src/Client/SugarApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sugarcrm\REST\Endpoint\MLPackage;
use Sugarcrm\REST\Endpoint\ModuleLoader;
use Sugarcrm\REST\Endpoint\Ping;
use Sugarcrm\REST\Endpoint\Rest;
use Sugarcrm\REST\Endpoint\Smart;
use Sugarcrm\REST\Endpoint\SugarBean;
use Sugarcrm\REST\Endpoint\ModuleFilter;
Expand Down Expand Up @@ -48,6 +49,7 @@
* @method ModuleLoader moduleLoader() -
* @method MLPackage mlp(string $id = null)
* @method Integrate integrate(string $module = '', string $id = '')
* @method Rest rest(string $endpoint = '')
*/
class SugarApi extends AbstractClient implements PlatformAwareInterface
{
Expand Down
8 changes: 8 additions & 0 deletions src/Endpoint/Abstracts/AbstractSmartSugarEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

namespace Sugarcrm\REST\Endpoint\Abstracts;

use GuzzleHttp\Psr7\Request;
use MRussell\REST\Endpoint\Data\ValidatedEndpointData;
use MRussell\REST\Endpoint\SmartEndpoint;
use MRussell\REST\Traits\PsrLoggerTrait;
use Sugarcrm\REST\Endpoint\SugarEndpointInterface;
use Sugarcrm\REST\Endpoint\Traits\CompileRequestTrait;
use Sugarcrm\REST\Endpoint\Traits\CustomHeadersTrait;

/**
* Provide a smarter interface for Endpoints, to better manage passed in data
Expand All @@ -20,6 +22,12 @@ abstract class AbstractSmartSugarEndpoint extends SmartEndpoint implements Sugar
{
use CompileRequestTrait;
use PsrLoggerTrait;
use CustomHeadersTrait;

protected string $_dataInterface = ValidatedEndpointData::class;

protected function configureRequest(Request $request, $data): Request
{
return parent::configureRequest($this->addCustomHeadersToRequest($request), $data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

namespace Sugarcrm\REST\Endpoint\Abstracts;

use GuzzleHttp\Psr7\Request;
use Sugarcrm\REST\Endpoint\SugarBean;
use MRussell\REST\Endpoint\Abstracts\AbstractModelEndpoint;
use Sugarcrm\REST\Endpoint\Traits\CustomHeadersTrait;
use Sugarcrm\REST\Endpoint\Traits\FieldsDataTrait;
use Sugarcrm\REST\Endpoint\Traits\ModuleAwareTrait;

Expand All @@ -22,6 +24,7 @@ abstract class AbstractSugarBeanCollectionEndpoint extends AbstractSugarCollecti
{
use FieldsDataTrait;
use ModuleAwareTrait;
use CustomHeadersTrait;

public const SUGAR_ORDERBY_DATA_PROPERTY = 'order_by';

Expand Down Expand Up @@ -97,6 +100,11 @@ protected function configurePayload(): mixed
return $this->configureFieldsDataProps($data);
}

protected function configureRequest(Request $request, $data): Request
{
return parent::configureRequest($this->addCustomHeadersToRequest($request), $data);
}

/**
* Add module to url options
* @inheritdoc
Expand Down
4 changes: 3 additions & 1 deletion src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Sugarcrm\REST\Endpoint\Integrate;
use Sugarcrm\REST\Endpoint\SugarEndpointInterface;
use Sugarcrm\REST\Endpoint\Traits\CompileRequestTrait;
use Sugarcrm\REST\Endpoint\Traits\CustomHeadersTrait;
use Sugarcrm\REST\Endpoint\Traits\FieldsDataTrait;
use Sugarcrm\REST\Endpoint\Traits\IntegrateSyncKeyTrait;
use Sugarcrm\REST\Endpoint\Traits\ModuleAwareTrait;
Expand Down Expand Up @@ -53,6 +54,7 @@ abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarE
use FieldsDataTrait;
use FileUploadsTrait;
use IntegrateSyncKeyTrait;
use CustomHeadersTrait;

public const MODEL_ACTION_VAR = 'action';

Expand Down Expand Up @@ -173,7 +175,7 @@ protected function configureRequest(Request $request, $data): Request
$data = $this->configureFieldsDataProps($data);
}

return parent::configureRequest($request, $data);
return parent::configureRequest($this->addCustomHeadersToRequest($request), $data);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Endpoint/Abstracts/AbstractSugarCollectionEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

namespace Sugarcrm\REST\Endpoint\Abstracts;

use GuzzleHttp\Psr7\Request;
use MRussell\REST\Exception\Endpoint\InvalidRequest;
use GuzzleHttp\Psr7\Response;
use MRussell\REST\Endpoint\Data\AbstractEndpointData;
use MRussell\REST\Endpoint\CollectionEndpoint;
use MRussell\REST\Traits\PsrLoggerTrait;
use Sugarcrm\REST\Endpoint\SugarEndpointInterface;
use Sugarcrm\REST\Endpoint\Traits\CompileRequestTrait;
use Sugarcrm\REST\Endpoint\Traits\CustomHeadersTrait;

/**
* Provides access to a multi-bean collection retrieved from Sugar 7 REST Api
Expand All @@ -23,6 +25,7 @@ abstract class AbstractSugarCollectionEndpoint extends CollectionEndpoint implem
{
use CompileRequestTrait;
use PsrLoggerTrait;
use CustomHeadersTrait;

public const SUGAR_OFFSET_PROPERTY = 'offset';

Expand Down Expand Up @@ -82,6 +85,11 @@ protected function configurePayload(): mixed
return $data;
}

protected function configureRequest(Request $request, $data): Request
{
return parent::configureRequest($this->addCustomHeadersToRequest($request), $data);
}

/**
* Get the configured offset
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Endpoint/Abstracts/AbstractSugarEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

namespace Sugarcrm\REST\Endpoint\Abstracts;

use GuzzleHttp\Psr7\Request;
use MRussell\REST\Endpoint\Endpoint;
use MRussell\REST\Traits\PsrLoggerTrait;
use Sugarcrm\REST\Endpoint\SugarEndpointInterface;
use Sugarcrm\REST\Endpoint\Traits\CompileRequestTrait;
use Sugarcrm\REST\Endpoint\Traits\CustomHeadersTrait;

/**
* Base Sugar API Endpoint for the simplest of REST functionality
Expand All @@ -19,4 +21,10 @@ abstract class AbstractSugarEndpoint extends Endpoint implements SugarEndpointIn
{
use CompileRequestTrait;
use PsrLoggerTrait;
use CustomHeadersTrait;

protected function configureRequest(Request $request, $data): Request
{
return parent::configureRequest($this->addCustomHeadersToRequest($request), $data);
}
}
6 changes: 6 additions & 0 deletions src/Endpoint/Provider/SugarEndpointProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Sugarcrm\REST\Endpoint\Ping;
use Sugarcrm\REST\Endpoint\Note;
use Sugarcrm\REST\Endpoint\Generic;
use Sugarcrm\REST\Endpoint\Rest;
use Sugarcrm\REST\Endpoint\Smart;

/**
Expand Down Expand Up @@ -161,5 +162,10 @@ class SugarEndpointProvider extends VersionedEndpointProvider
self::ENDPOINT_CLASS => Integrate::class,
self::ENDPOINT_PROPERTIES => [],
],
[
self::ENDPOINT_NAME => 'rest',
self::ENDPOINT_CLASS => Rest::class,
self::ENDPOINT_PROPERTIES => [],
],
];
}
74 changes: 74 additions & 0 deletions src/Endpoint/Rest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* ©[2022] SugarCRM Inc. Licensed by SugarCRM under the Apache 2.0 license.
*/

namespace Sugarcrm\REST\Endpoint;

use GuzzleHttp\Psr7\Request;
use Sugarcrm\REST\Endpoint\Abstracts\AbstractSugarEndpoint;

/**
* Generic REST endpoint allowing for querying custom endpoints
* usage examples:
* $client->rest('custom/endpoint')->get();
* $client->rest('custom/endpoint')->post($data);
* $client->rest('custom/endpoint')->put($data);
* $client->rest('custom/endpoint')->delete();
* $client->rest('custom/endpoint')->patch($data);
* $client->rest('custom/endpoint')->withHeaders($headers)->get();
* $client->rest('Contacts')->setData(['fields' => 'id,first_name,last_name', 'max_num' => 1])->get();
* etc.
*/
class Rest extends Generic
{
protected static array $_DEFAULT_PROPERTIES = [
self::PROPERTY_URL => '$endpoint',
self::PROPERTY_AUTH => true,
self::PROPERTY_HTTP_METHOD => "GET",
];

public function get(mixed $data = null): static
{
$this->setProperty(self::PROPERTY_HTTP_METHOD, 'GET');
if (!is_null($data)) {
$this->setData($data);
}
return $this->execute();
}

public function post(mixed $data = null): static
{
$this->setProperty(self::PROPERTY_HTTP_METHOD, 'POST');
if (!is_null($data)) {
$this->setData($data);
}
return $this->execute();
}

public function put(mixed $data = null): static
{
$this->setProperty(self::PROPERTY_HTTP_METHOD, 'PUT');
if (!is_null($data)) {
$this->setData($data);
}
return $this->execute();
}

public function delete(): static
{
$this->setProperty(self::PROPERTY_HTTP_METHOD, 'DELETE');
return $this->execute();
}

public function patch(mixed $data = null): static
{
$this->setProperty(self::PROPERTY_HTTP_METHOD, 'PATCH');
if (!is_null($data)) {
$this->setData($data);
}
return $this->execute();
}

}
47 changes: 47 additions & 0 deletions src/Endpoint/Traits/CustomHeadersTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Sugarcrm\REST\Endpoint\Traits;

use GuzzleHttp\Psr7\Request;

trait CustomHeadersTrait
{
protected array $customHeaders = [];

/**
* Set a custom header to be sent with the request
* @param string $name
* @param string $value
* @return $this
*/
public function addCustomHeader(string $name, string $value): static
{
$normalized = strtolower($name);
$this->customHeaders[$normalized] = [$name, $value];
return $this;
}

/**
* Remove a custom header from the request
* @param string $name
* @return $this
*/
public function removeCustomHeader(string $name): static
{
$normalized = strtolower($name);
if (isset($this->customHeaders[$normalized])) {
unset($this->customHeaders[$normalized]);
}
return $this;
}

protected function addCustomHeadersToRequest(Request $request): Request
{
if (!empty($this->customHeaders)) {
foreach ($this->customHeaders as $headerNormalized => $values) {
$request = $request->withHeader($values[0], $values[1]);
}
}
return $request;
}
}
Loading