Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Rest Client Bundle Laravel Package

cos/rest-client-bundle

View on GitHub
Deep Wiki
Context7

RestClientBundle

Symfony Rest Client Bundle using GuzzleHttp and Ocramius/ProxyManager

Installation

To install CosRestClientBundle with Composer execute the following command:

$ composer require "cos/rest-client-bundle": "dev-master"

Now, Composer will automatically download all required files, and install them for you. All that is left to do is to update your AppKernel.php file, and register the new bundle:

<?php

// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new Cos\RestClientBundle\CosRestClientBundle(),
    // ...
);

The bundle is now installed. Lets start configure with the bundle.

Configuration reference

# config.yml
cos_rest_client:
    annotation_reader: annotation_reader # annotation reader service id
    # http clients and base URLs
    clients:
        default: { baseUri: 'https://jsonplaceholder.typicode.com' }

Usage

Define a rest resource interface

<?php

namespace AppBundle\Rest;


use Cos\RestClientBundle\Annotation\Client;
use Cos\RestClientBundle\Annotation\Endpoint;
use Cos\RestClientBundle\Annotation\Form;
use Cos\RestClientBundle\Annotation\Json;
use Cos\RestClientBundle\Annotation\Path;
use Cos\RestClientBundle\Annotation\Query;

/**
 * Client configuration  
 * @Client(name="default")
 */
interface Posts
{
    /**
     * @Path(name="id", paramName="idParam")
     * @Endpoint(uri="/posts/{id}", method="get")
     */
    public function get($idParam);

    /**
     * @Query(name="userId")
     * @Endpoint(uri="/posts")
     */
    public function getWithQuery($userId);

    /**
     * @Form(name="formData")
     * @Endpoint(uri="/posts", method="POST")
     */
    public function form(array $formData);

    /**
     * @Json(name="data")
     * @Endpoint(uri="/posts", method="POST")
     */
    public function json(array $data);
}

Create proxy for Posts

<?php
    //in controller
    $proxyFactory = $this->get('cos_rest_client.proxy_factory');
    $proxy = $proxyFactory->create(Posts::class);

Call proxy methods defined in interface

<?php
    $proxy->get(1); //request for client base uri + /posts/1
    $proxy->getWithQuery(1)->getBody()->getContents(); //request for /posts?userId=1
    $data = ['foo' => 'bar']
    $proxy->form($data) //post request where $data is sent as application/x-www-form-urlencoded
    $proxy->json($data) // send data as json

Response

Every method call from a proxy return a Psr\Http\Message\ResponseInterface

Full example

https://github.com/cosminseceleanu/RestClientBundleSample

Events

RequestEvent: dispatched before a request is executed ResponseEvent: dispatched when response is received

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai