diff --git a/assets/img/debug-toolbar.png b/assets/img/debug-toolbar.png new file mode 100644 index 0000000..522a81b Binary files /dev/null and b/assets/img/debug-toolbar.png differ diff --git a/integrations/index.rst b/integrations/index.rst index 9126e84..8811b4b 100644 --- a/integrations/index.rst +++ b/integrations/index.rst @@ -6,3 +6,4 @@ HTTPlug provides the following framework integrations: .. toctree:: symfony-bundle + symfony-full-configuration diff --git a/integrations/symfony-bundle.rst b/integrations/symfony-bundle.rst index 96f6e98..edb779e 100644 --- a/integrations/symfony-bundle.rst +++ b/integrations/symfony-bundle.rst @@ -1,74 +1,64 @@ Symfony Bundle ============== -The usage documentation is split into two parts. First we explain how to configure the bundle in an application. The second part is for developing reusable Symfony bundles that depend on an HTTP client defined by the Httplug interface. - -For information how to write applications with the services provided by this bundle, have a look at the [Httplug documentation](http://docs.php-http.org). - - -Use in Applications -------------------- - -Custom services -``````````````` - -+----------------------------------+---------------------------------------------------------------------+ -| Service id | Description | -+==================================+=====================================================================+ -| httplug.message_factory | Service* that provides the `Http\Message\MessageFactory` | -+----------------------------------+---------------------------------------------------------------------+ -| httplug.uri_factory | Service* that provides the `Http\Message\UriFactory` | -+----------------------------------+---------------------------------------------------------------------+ -| httplug.stream_factory | Service* that provides the `Http\Message\StreamFactory` | -+----------------------------------+---------------------------------------------------------------------+ -| httplug.client.[name] | | This is your Httpclient that you have configured. | -| | | With the configuration below the name would be `acme_client`. | -+----------------------------------+---------------------------------------------------------------------+ -| httplug.client | This is the first client configured or a client named `default`. | -+----------------------------------+---------------------------------------------------------------------+ -| | httplug.plugin.content_length | | These are plugins that are enabled by default. | -| | httplug.plugin.decoder | | These services are not public and may only be used when configure | -| | httplug.plugin.error | | HttpClients or other services. | -| | httplug.plugin.logger | | -| | httplug.plugin.redirect | | -| | httplug.plugin.retry | | -| | httplug.plugin.stopwatch | | -+----------------------------------+---------------------------------------------------------------------+ -| | httplug.plugin.cache | | These are plugins that are disabled by default. | -| | httplug.plugin.cookie | | They need to be configured before they can be used. | -| | httplug.plugin.history | | These services are not public and may only be used when configure | -| | | HttpClients or other services. | -+----------------------------------+---------------------------------------------------------------------+ - -\* *These services are always an alias to another service. You can specify your own service or leave the default, which is the same name with `.default` appended. The default services in turn use the service discovery mechanism to provide the best available implementation. You can specify a class for each of the default services to use instead of discovery, as long as those classes can be instantiated without arguments.* - - -If you need a more custom setup, define the services in your application configuration and specify your service in the `main_alias` section. For example, to add authentication headers, you could define a service that decorates the service `httplug.client.default` with a plugin that injects the authentication headers into the request and configure `httplug.main_alias.client` to the name of your service. +This bundle integrate HTTPlug with the Symfony framework. The bundle helps to register services for all your clients and makes sure all the configuration is in one place. The bundle also feature a toolbar plugin with information about your requests. + + +Installation +```````````` + +Install the Httplug bundle with composer and enable it in your AppKernel.php. + +.. code-block:: bash + + $ composer require php-http/httplug-bundle + +.. code-block:: php + + public function registerBundles() + { + $bundles = array( + // ... + new Http\HttplugBundle\HttplugBundle(), + ); + } + +You will find all available configuration at the :doc:`full configuration ` page. + +Usage +````` .. code-block:: yaml httplug: + plugins: + logger: ~ clients: - acme_client: # This is the name of the client - factory: 'httplug.factory.guzzle6' - - main_alias: - client: httplug.client.default - message_factory: httplug.message_factory.default - uri_factory: httplug.uri_factory.default - stream_factory: httplug.stream_factory.default - classes: - # uses discovery if not specified - client: ~ - message_factory: ~ - uri_factory: ~ - stream_factory: ~ + acme: + factory: 'httplug.factory.guzzle6' + plugins: ['httplug.plugin.logger'] + config: + verify: false + timeout: 2 + +.. code-block:: php + $request = $this->container->get('httplug.message_factory')->createRequest('GET', 'http://example.com'); + $response = $this->container->get('httplug.client.acme')->sendRequest($request); -Configuration without auto discovery -```````````````````````````````````` -By default we use Puli to auto discover factories. If you do not want to use auto discovery you could use the following configuration (Guzzle): +Web Debug Toolbar +````````````````` +.. image:: /assets/img/debug-toolbar.png + :align: right + :width: 120px + +When using a client configured with HttplugBundle, you will get debug information in the web debug toolbar. It will tell you how many request were made and how many of those that were successful or not. It will also show you detailed information about each request. + +Discovery of Factory Classes +```````````````````````````` + +If you want the bundle to automatically find usable factory classes, install and enable ``puli/symfony-bundle``. If you do not want use auto discovery, you need to specify all the factory classes for you client. The following example show how you configure factory classes using Guzzle: .. code-block:: yaml @@ -80,10 +70,11 @@ By default we use Puli to auto discover factories. If you do not want to use aut stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory -Configure your client -````````````````````` -You can configure your clients with some good default options. The clients are later registered as services. +Configure Clients +````````````````` + +You can configure your clients with default options. These default values will be specific to you client you are using. The clients are later registered as services. .. code-block:: yaml @@ -109,57 +100,54 @@ You can configure your clients with some good default options. The clients are l $httpClient = $this->container->get('httplug.client.my_guzzle5'); $httpClient = $this->container->get('httplug.client.acme'); + // will be the same as ``httplug.client.my_guzzle5`` + $httpClient = $this->container->get('httplug.client'); + +The bundle has client factory services that you can use to build your client. If you need a very custom made client you could create your own factory service implementing ``Http\HttplugBudle\ClientFactory\ClientFactory``. The build in services are: + +* ``httplug.factory.guzzle5`` +* ``httplug.factory.guzzle6`` Plugins ``````` -You can configure the clients with plugins. +You can configure the clients with plugins. You can choose to use a built in plugin in the ``php-http/plugins`` package or provide a plugin of your own. The order of the specified plugin does matter. .. code-block:: yaml // services.yml acme_plugin: - class: Acme\Plugin\MyCustonPlugin - arguments: ["%api_key%"] + class: Acme\Plugin\MyCustomPlugin + arguments: ["%some_parameter%"] .. code-block:: yaml // config.yml - httpug: + httplug: plugins: cache: cache_pool: 'my_cache_pool' clients: acme: factory: 'httplug.factory.guzzle6' - plugins: ['acme_plugin', 'httplug.plugin.cache', ''httplug.plugin.retry'] - config: - base_uri: 'http://google.se/' + plugins: ['acme_plugin', 'httplug.plugin.cache', 'httplug.plugin.retry'] Authentication `````````````` +You can configure a client with authentication. Valid authentication types are ``basic``, ``bearer``, ``service`` and ``wsse``. See more examples at the :doc:`full configuration `. + .. code-block:: yaml // config.yml - httpug: + httplug: plugins: authentication: - my_basic: - type: 'basic' - username: 'my_username' - password: 'p4ssw0rd' my_wsse: type: 'wsse' username: 'my_username' password: 'p4ssw0rd' - my_brearer: - type: 'bearer' - token: 'authentication_token_hash' - my_service: - type: 'service' - service: 'my_authentication_service' clients: acme: @@ -167,10 +155,43 @@ Authentication plugins: ['httplug.plugin.authentication.my_wsse'] - -Use for Reusable Bundles ------------------------- - -Rather than code against specific HTTP clients, you want to use the Httplug `Client` interface. To avoid building your own infrastructure to define services for the client, simply `require: php-http/httplug-bundle` in your bundles `composer.json`. You SHOULD provide configuration for each of your services that needs an HTTP client to specify the service to use, defaulting to `httplug.client`. This way, the default case needs no additional configuration for your users. - -The only steps they need is `require` one of the adapter implementations in their projects `composer.json` and instantiating the HttplugBundle in their kernel. +List of Services +```````````````` + ++----------------------------------+-------------------------------------------------------------------------+ +| Service id | Description | ++==================================+=========================================================================+ +| httplug.message_factory | Service* that provides the `Http\Message\MessageFactory` | ++----------------------------------+-------------------------------------------------------------------------+ +| httplug.uri_factory | Service* that provides the `Http\Message\UriFactory` | ++----------------------------------+-------------------------------------------------------------------------+ +| httplug.stream_factory | Service* that provides the `Http\Message\StreamFactory` | ++----------------------------------+-------------------------------------------------------------------------+ +| httplug.client.[name] | There is one service per named client. | ++----------------------------------+-------------------------------------------------------------------------+ +| httplug.client | | If there is a client named "default", this service is an alias to | +| | | that client, otherwise it is an alias to the first client configured. | ++----------------------------------+-------------------------------------------------------------------------+ +| | httplug.plugin.content_length | | These are plugins that are enabled by default. | +| | httplug.plugin.decoder | | These services are private and should only be used to configure | +| | httplug.plugin.error | | clients or other services. | +| | httplug.plugin.logger | | +| | httplug.plugin.redirect | | +| | httplug.plugin.retry | | +| | httplug.plugin.stopwatch | | ++----------------------------------+-------------------------------------------------------------------------+ +| | httplug.plugin.cache | | These are plugins that are disabled by default and only get | +| | httplug.plugin.cookie | | activated when configured. | +| | httplug.plugin.history | | These services are private and should only be used to configure | +| | | clients or other services. | ++----------------------------------+-------------------------------------------------------------------------+ + +\* *These services are always an alias to another service. You can specify your own service or leave the default, which is the same name with `.default` appended.* + + +Useage for Reusable Bundles +`````````````````````````` + +Rather than code against specific HTTP clients, you want to use the Httplug ``Client`` interface. To avoid building your own infrastructure to define services for the client, simply ``require: php-http/httplug-bundle`` in your bundles ``composer.json``. You SHOULD provide a configuration option to specify the which HTTP client service to use for each of your services. This option should default to ``httplug.client``. This way, the default case needs no additional configuration for your users, but they have the option of using specific clients with each of your services. + +The only steps they need is ``require`` one of the adapter implementations in their projects ``composer.json`` and instantiating the HttplugBundle in their kernel. diff --git a/integrations/symfony-full-configuration.rst b/integrations/symfony-full-configuration.rst new file mode 100644 index 0000000..5f2c9f3 --- /dev/null +++ b/integrations/symfony-full-configuration.rst @@ -0,0 +1,76 @@ +Full configuration +================== + +This page shows an example of all configuration values provided by the bundle. + +.. code-block:: yaml + + // config.yml + httplug: + main_alias: + client: httplug.client.default + message_factory: httplug.message_factory.default + uri_factory: httplug.uri_factory.default + stream_factory: httplug.stream_factory.default + classes: + # uses discovery if not specified + client: ~ + message_factory: ~ + uri_factory: ~ + stream_factory: ~ + + plugins: + authentication: + my_basic: + type: 'basic' + username: 'my_username' + password: 'p4ssw0rd' + my_wsse: + type: 'wsse' + username: 'my_username' + password: 'p4ssw0rd' + my_bearer: + type: 'bearer' + token: 'authentication_token_hash' + my_service: + type: 'service' + service: 'my_authentication_service' + cache: + enabled: true + cache_pool: 'my_cache_pool' + stream_factory: 'httplug.stream_factory' + config: + default_ttl: 3600 + respect_cache_headers: true + cookie: + enabled: true + cookie_jar: my_cookie_jar + decoder: + enabled: true + use_content_encoding: true + history: + enabled: true + journal: my_journal + logger: + enabled: true + logger: 'logger' + formatter: null + redirect: + enabled: true + preserve_header: true + use_default_for_multiple: true + retry: + enabled: true + retry: 1 + stopwatch: + enabled: true + stopwatch: 'debug.stopwatch' + clients: + acme: + factory: 'httplug.factory.guzzle6' + plugins: ['httplug.plugin.authentication.my_wsse', 'httplug.plugin.cache', 'httplug.plugin.retry'] + config: + verify: false + timeout: 2 + # more options to the guzzle 6 constructor +