cct-marketing / rest
Base library to create an API sdk for CCT Microservices
Installs: 6 649
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ~7.1 | ~8.0
- cct-marketing/collections: ~1.0
- guzzlehttp/guzzle: ~6.5|~7.0
- symfony/http-foundation: ~4.4|~5.0|~6.0
- symfony/yaml: ~4.4|~5.0|~6.0
Requires (Dev)
- jms/serializer: ^2.3|^3.28
- phpunit/phpunit: ^9.0
- symfony/property-access: ~4.0|~5.0|~6.0
- symfony/serializer: ^4.0|~5.0|~6.0
This package is auto-updated.
Last update: 2024-11-05 15:43:54 UTC
README
A REST(Representational State Transfer) library to help rapidly develop RESTful web service SDKs. Supports serialization of response and request to objects.
Installation
composer require cct-marketing/rest
Serilizer Libraries (Optional)
If no serializer is install all responses will be returned as an array
Recommended JMS Serialiser
composer require jms/serializer
Alternative Symfony Serialiser
composer require symfony/serializer composer require symfony/property-access
Usage
To use the library you just need to extend the AbstractClient and AbstractSerializerRequest or AbstractRequest classes
use CCT\Component\Rest\Config; use CCT\Component\Rest\Http\Definition\RequestHeaders; use CCT\Component\Rest\Http\Request; use CCT\Component\Rest\Serializer\Context\Context; class MyRequest extends Request { protected function setUp() { $this->config->set(Config::URI_PREFIX, '/test/'); } public function apiCall(QueryParams $queryParams = null) { $this->config->set('serialization_context', Context::create()->setGroups(['read'])); $headers = RequestHeaders::create( [ 'Accept' => 'application/json', 'X-Requested-With' => 'XMLHttpRequest', ] ); $this->setHeaders($headers); return parent::requestGet($this->getUri(), $queryParams); } }
use CCT\Component\Rest\AbstractClient; use CCT\Component\Rest\Config; class RESTClient extends AbstractClient { /** * @return ScrapeRequest */ public function myAPI(): MyRequest { $config = clone $this->config; $modelClass = TestModel::class; $serializer = $this->getBuiltSerializer($config); if ($this->shouldUseDefaultResponseTransformers() && null !== $serializer) { $this->applyDefaultResponseTransformers($config, $serializer, $modelClass); } return $this->createRequestInstance(TestRequest::class, $config, null); } }
To run:
$config = new \CCT\Component\Rest\Config([ \CCT\Component\Rest\Config::ENDPOINT => 'https://api.endpoint.com/', \CCT\Component\Rest\Config::DEBUG => true, ]); /** * Send Request */ $client = new Client($config); $query = new QueryParams(); $query->set('message', 'hello'); $response = $client->myAPI()->apiCall($query); var_dump($response->getData());